1

以下脚本中的 gam.check 将诊断信息输出到控制台(以及绘图):

library(mgcv)
set.seed(0)
dat <- gamSim(1,n=200)
b<-gam(y~s(x0)+s(x1)+s(x2)+s(x3),data=dat)
gam.check(b,pch=19,cex=.3)

上述代码中 gam.check 语句到控制台的输出是:

Method: GCV   Optimizer: magic
Smoothing parameter selection converged after 8 iterations.
The RMS GCV score gradient at convergence was 0.00001072609 .
The Hessian was positive definite.
Model rank =  37 / 37 

Basis dimension (k) checking results. Low p-value (k-index<1) may
indicate that k is too low, especially if edf is close to k'.

         k'   edf k-index p-value
s(x0) 9.000 2.318   0.996    0.44
s(x1) 9.000 2.306   0.969    0.32
s(x2) 9.000 7.655   0.961    0.24
s(x3) 9.000 1.233   1.037    0.66

我想将诊断的输出保存到列表中(或仅将表保存到数据框)而不输出任何图形。

事情,我考虑过:

  1. 下面的代码返回一个空对象。

    x<-gam.check(b,pch=19,cex=.3)

  2. 查看 gam.check 的代码,似乎我想“获取”结果

    kchck <- k.check(b, subsample = k.sample, n.rep = k.rep)

    不幸的是,直接运行上述代码行会产生“找不到函数“k.check”。

  3. 我可以使用 sink 将输出保存到控制台,但这不会关闭绘图。

  4. Gavin Simpson为在此处提取绘图提供了一个很好的答案,但我没有看到任何有助于解决我的问题的东西。

4

1 回答 1

1

上面评论中的user20650答案是...

对于您的选项二,请使用包名称...即 mgcv:::k.check 这样就可以使用 f <- function(b, k.sample = 5000, k.rep = 200) printCoefmat(mgcv:::k .check(b, subsample = k.sample, n.rep = k.rep), 数字 = 3)

...出于我的目的,我放弃了 printCoefmat

f <- function(b, k.sample = 5000, k.rep = 200) {
  mgcv:::k.check(b, subsample = k.sample, n.rep = k.rep)
}

(basis <- f(b))
于 2017-02-04T19:40:07.877 回答