【发布时间】:2016-08-09 20:27:55
【问题描述】:
让x = c(1, 2, 3) 成为一个向量。我在R 的splines 包中使用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
我猜这是因为matrix 是bs.x 所属的类之一。此刻,我做了以下愚蠢的事情。
bs.x <- matrix(as.numeric(bs.x), nr = nrow(bs.x))
还有更好的选择吗?提前致谢。
【问题讨论】:
-
您可以保存昏暗,删除所有属性,然后重新分配昏暗。这应该是相当有效的并且避免复制。类似
dims <- dim(bs.x) ; attributes(bs.x) <- NULL ; dim(bs.x) <- dims