Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract p-value from gam.check in R

Tags:

r

p-value

gam

When I run gam.check(my_spline_gam), I get the following output.

Method: GCV   Optimizer: magic
Smoothing parameter selection converged after 9 iterations.
The RMS GCV score gradiant at convergence was 4.785628e-06 .
The Hessian was positive definite.
The estimated model rank was 25 (maximum possible: 25)
Model rank =  25 / 25 

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(x) 24.000 22.098   0.849    0.06

My question is whether I can extract this p-value separately to a table.

like image 911
a_geo Avatar asked Nov 20 '18 09:11

a_geo


2 Answers

Looks like you cannot store the result in an object the normal way. You could use capture.output to store the console output in an object, and then subsequently use str_split to get the correct value. So for the example in the help file this would be:

library(mgcv)
set.seed(0)
dat <- gamSim(1,n=200)
b <- gam(y~s(x0)+s(x1)+s(x2)+s(x3),data=dat)
r <- capture.output(gam.check(b))
p <- strsplit(r[12], " ")[[1]][11]

But because the p-value is just a string you wouldn't get the exact p-value this way.

Edit: user20650's answer will give you the proper output:

r <- k.check(b)
r[,'p-value']
like image 161
Ravi Avatar answered Oct 17 '22 08:10

Ravi


Use capture.output coupled with a little string manipulation -

gam_obj <- capture.output(gam.check(b,pch=19,cex=.3))
gam_tbl <- gam_obj[12:length(gam_obj)]
str_spl = function(x){
  p_value <- strsplit(x, " ")[[1]]
  output_p <- as.numeric(p_value[length(p_value)])
}
p_values <- data.frame(sapply(gam_tbl, str_spl))

Output

enter image description here

like image 1
Vivek Kalyanarangan Avatar answered Oct 17 '22 06:10

Vivek Kalyanarangan