【问题标题】:extract matrix from bs (b spline) function output in R从R中的bs(b样条)函数输出中提取矩阵
【发布时间】:2016-08-09 20:27:55
【问题描述】:

x = c(1, 2, 3) 成为一个向量。我在Rsplines 包中使用bs 函数来生成在x 处评估的B 样条矩阵。

require(splines)
x <- c(1, 2, 3)
bs.x <- bs(x, knots = c(1.5, 2.5))

输出bs.x如下,

              1         2         3          4 5
[1,] 0.00000000 0.0000000 0.0000000 0.00000000 0
[2,] 0.05555556 0.4444444 0.4444444 0.05555556 0
[3,] 0.00000000 0.0000000 0.0000000 0.00000000 1
attr(,"degree")
[1] 3
attr(,"knots")
[1] 1.5 2.5
attr(,"Boundary.knots")
[1] 1 3
attr(,"intercept")
[1] FALSE
attr(,"class")
[1] "bs"     "basis"  "matrix"

显然,除了基矩阵,bs.x 还有其他属性。 我的问题是如何摆脱这些属性。我需要这样做,因为最终我需要运行Matrix(bs.x),这会引发以下错误消息。

Error in as(x, "matrix") : 
internal problem in as(): “bs” is(object, "matrix") is 
TRUE, but the metadata asserts that the 'is' relation is FALSE

我猜这是因为matrixbs.x 所属的类之一。此刻,我做了以下愚蠢的事情。

bs.x <- matrix(as.numeric(bs.x), nr = nrow(bs.x))

还有更好的选择吗?提前致谢。

【问题讨论】:

  • 您可以保存昏暗,删除所有属性,然后重新分配昏暗。这应该是相当有效的并且避免复制。类似dims &lt;- dim(bs.x) ; attributes(bs.x) &lt;- NULL ; dim(bs.x) &lt;- dims

标签: r matrix bspline


【解决方案1】:

没有好很多,但是

attributes(bs.x) <- attributes(bs.x)["dim"]

似乎有效。 (将bs.x 的属性重新分配为 dim 属性。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-27
    • 2023-03-23
    • 2021-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-02
    • 1970-01-01
    相关资源
    最近更新 更多