【问题标题】:Extract the knots from a ns object从 ns 对象中提取结
【发布时间】:2012-02-16 20:42:13
【问题描述】:
library(splines)
x <- runif(100000)
spline <- ns(x, df=5)

我如何知道这个脊椎对象的结在哪里?

> spline ['knots']
[1] NA

str 让我接近我需要的东西,但我希望能够提取样条线矢量,对其进行格式化,然后将其打印到 Sweave 文档中。

> str(sp)
 ns [1:117542, 1:5] 0.527 0.474 0.455 0.472 0.498 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:5] "1" "2" "3" "4" ...
 - attr(*, "degree")= num 3
 - attr(*, "knots")= Named num [1:4] 1.03 1.55 1.99 2.7
  ..- attr(*, "names")= chr [1:4] "20%" "40%" "60%" "80%"
 - attr(*, "Boundary.knots")= num [1:2] 0.0214 4.9999
 - attr(*, "intercept")= logi FALSE
 - attr(*, "class")= chr [1:3] "ns" "basis" "matrix"

【问题讨论】:

    标签: r regression splines


    【解决方案1】:

    嗯,这样的东西绝对可以提取东西:

    > attr(spline, "knots")
          20%       40%       60%       80% 
    0.1966951 0.3973261 0.5982386 0.7993613 
    

    【讨论】:

    • 谢谢。我试过spline$knotsspline@knotsspline['knots']spline[['knots']]。我忘了attr...
    • @Zach,从您显示的 str() 输出中可以清楚地看到 attrs 的使用。所以,不要蛮力,而只是看看输出:)
    • @Anton-- 我之前没有遇到过attr() 函数,所以我没想到str 告诉我的是这个。
    猜你喜欢
    • 1970-01-01
    • 2016-02-09
    • 2017-04-22
    • 2016-11-26
    • 1970-01-01
    • 2019-12-30
    • 1970-01-01
    • 2017-08-12
    • 1970-01-01
    相关资源
    最近更新 更多