使用样条曲线从gam中提取结值

时间:2014-10-22 22:01:12

标签: r extract spline gam

我在许多样本中运行GAM,并从结果中提取系数/ t值/ r平方,如下所示。对于背景,我使用的是自然样条曲线,因此常规的lm()在这里工作正常,也许这就是为什么这个方法工作正常。

tvalsm93exf=ldply(fitsm93exf, function(x) as.data.frame(t(coef(summary(x))[,'t value', drop=FALSE]))) 
r2m93exf=ldply(fitsm93exf, function(x) as.data.frame(t(summary(x))[,'r.squared', drop=FALSE]))

我还想提取每个样本集的结点位置(df = 4且没有截距,因此有三个内部节点和边界)。我已尝试过上述命令的几种变体,但无法将其编入索引。这样做的常规方法如下,所以我试图把它放到上面的表格中。但我不确定摘要函数是否包含这些值,或者是否还有其他结果我应该包括在内。

attr(terms(fits),"predvars") 

http://www.inside-r.org/r-doc/splines/ns

注意:这个问题与下面的问题有关,如果这有帮助,虽然它的解决方案无法帮助我解决我的问题: Extract estimates of GAM

1 个答案:

答案 0 :(得分:0)

在您链接到的帮助页面上的示例中调用ns函数时,节点是固定的,因此您可以在不进入模型对象的情况下提取节点。但是......你没有提供GAM模型创建的代码,所以我们只能猜测你可能做了什么。仅仅因为在“ns-help-page”和文档中都使用了“spline”这个词并不意味着它们是相同的。您链接到的另一个页面中的模型有s函数构造的两个“平滑”术语。

  .... + s(time,bs="cr",k=200) + s(tmpd,bs="cr")

gam调用的结果有一个名为“smooth”的列表节点,当用str()查看时,第一个看起来像这样:

str(ap1$smooth)
List of 2
 $ :List of 22
  ..$ term          : chr "time"
  ..$ bs.dim        : num 200
  ..$ fixed         : logi FALSE
  ..$ dim           : int 1
  ..$ p.order       : logi NA
  ..$ by            : chr "NA"
  ..$ label         : chr "s(time)"
  ..$ xt            : NULL
  ..$ id            : NULL
  ..$ sp            : Named num -1
  .. ..- attr(*, "names")= chr "s(time)"
  ..$ S             :List of 1
  .. ..$ : num [1:199, 1:199] 5.6 -5.475 2.609 -0.577 0.275 ...
  ..$ rank          : num 198
  ..$ null.space.dim: num 1
  ..$ df            : num 199
  ..$ xp            : Named num [1:200] -2556 -2527 -2502 -2476 -2451 ...
  .. ..- attr(*, "names")= chr [1:200] "0.0000000%" "0.5025126%" "1.0050251%" "1.5075377%" ...
  ..$ F             : num [1:40000] 0 0 0 0 0 0 0 0 0 0 ...
  ..$ plot.me       : logi TRUE
  ..$ side.constrain: logi TRUE
  ..$ S.scale       : num 9.56e-05
  ..$ vn            : chr "time"
  ..$ first.para    : num 5
  ..$ last.para     : num 203
  ..- attr(*, "class")= chr [1:2] "cr.smooth" "mgcv.smooth"
  ..- attr(*, "qrc")=List of 4
  .. ..$ qr   : num [1:200, 1] -0.0709 0.0817 0.0709 0.0688 0.0724 ...
  .. ..$ rank : int 1
  .. ..$ qraux: num 1.03
  .. ..$ pivot: int 1
  .. ..- attr(*, "class")= chr "qr"
  ..- attr(*, "nCons")= int 1

因此,在200个点中的每个点处评估平滑,并且多项式函数适合于该网格上的数据。如果你强迫结在三个内部位置,那么它们将处于极端和极端之间的均匀间隔位置。