Search code examples
rgammgcv

How to save edf from mgcv::gam.check and skip the plots


gam.check in the below script outputs diagnostics to the console (as well as plots):

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)

The output to the console from gam.check statement in above code is:

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

I would like to save the output from the diagnostics to a list (or just the table to a data frame) and not output any of the graphics.

Things, I've considered:

  1. The below code returns a null object.

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

  2. Viewed the code for gam.check, it seems like I would want to 'get' the results from

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

    Unfortunately, running the above line of code directly, yields a 'could not find function "k.check".

  3. I could use sink to save output to the console, but that would not turn off the plotting.

  4. Gavin Simpson provided a great answer for extracting plots here but I didn't see anything there that would help solve my question.


Solution

  • user20650 answer in above comment is spot on ...

    For your option two, use the package name... ie mgcv:::k.check so then can use f <- function(b, k.sample = 5000, k.rep = 200) printCoefmat(mgcv:::k.check(b, subsample = k.sample, n.rep = k.rep), digits = 3)

    ...for my purpose i dropped the printCoefmat

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