【问题标题】:Ensure knots in spline regression are placed at correct positions确保样条回归中的节点放置在正确的位置
【发布时间】:2016-10-21 05:28:43
【问题描述】:

我有时间序列数据:

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

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

【问题讨论】:

    标签: r time-series regression spline cubic-spline


    【解决方案1】:

    你有点走错路了。看来您想按时间回归观察,因此您应该真正将时间索引而不是观察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"
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-29
      • 1970-01-01
      • 2017-07-29
      • 1970-01-01
      • 2013-07-19
      • 1970-01-01
      相关资源
      最近更新 更多