0

我有时间序列数据:

library(xts)
library(splines)
set.seed(123)
time <- seq(as.POSIXct("2015-09-01"),as.POSIXct("2015-09-01 23:59:59"),by="hour")
ob <- xts(rnorm(length(time),150,5),time))

该对象ob是每小时时间序列对象。现在,我想对其进行样条回归。我想在早上 7 点和下午 4 点打结 以下语句是否R确保这一点

ns(ob,knots = c(7,16)) # 7 corresponds to 7 AM and 16 corresponds to 4 PM

另外,我应该如何交叉检查是否在上述时间放置了结?

4

1 回答 1

2

你有点走错路了。看来您想按时间回归观察,因此您应该真正将时间索引而不是观察ob提供给ns.

y <- as.vector(ob)    ## observations
x <- 1:24    ## 24 hourse

然后考虑一个模型:

y ~ ns(x, knots = c(7, 16))

如您所见,这里确实没有必要使用“xts”对象。


ns生成设计矩阵。检查一下

X <- ns(x, knots = c(7, 16))

您将看到属性:

#attr(,"degree")
#[1] 3
#attr(,"knots")
#[1]  7 16
#attr(,"Boundary.knots")
#[1]  1 24
#attr(,"intercept")
#[1] FALSE
#attr(,"class")
#[1] "ns"     "basis"  "matrix"

“结”字段为您提供有关内部结位置的信息。

于 2016-10-21T05:50:45.740 回答