0

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

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))

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

4

1 回答 1

1

不是好多了,但是

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

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

于 2016-08-09T12:27:55.753 回答