k.check {mgcv}R Documentation

Checking smooth basis dimension


Takes a fitted gam object produced by gam() and runs diagnostic tests of whether the basis dimension choises are adequate.


k.check(b, subsample=5000, n.rep=400)



a fitted gam object as produced by gam().


above this number of data, testing uses a random sub-sample of data of this size.


how many re-shuffles to do to get p-value for k testing.


The test of whether the basis dimension for a smooth is adequate (Wood, 2017, section 5.9) is based on computing an estimate of the residual variance based on differencing residuals that are near neighbours according to the (numeric) covariates of the smooth. This estimate divided by the residual variance is the k-index reported. The further below 1 this is, the more likely it is that there is missed pattern left in the residuals. The p-value is computed by simulation: the residuals are randomly re-shuffled n.rep times to obtain the null distribution of the differencing variance estimator, if there is no pattern in the residuals. For models fitted to more than subsample data, the tests are based of subsample randomly sampled data. Low p-values may indicate that the basis dimension, k, has been set too low, especially if the reported edf is close to k\', the maximum possible EDF for the term. Note the disconcerting fact that if the test statistic itself is based on random resampling and the null is true, then the associated p-values will of course vary widely from one replicate to the next. Currently smooths of factor variables are not supported and will give an NA p-value.

Doubling a suspect k and re-fitting is sensible: if the reported edf increases substantially then you may have been missing something in the first fit. Of course p-values can be low for reasons other than a too low k. See choose.k for fuller discussion.


A matrix contaning the output of the tests described above.


Simon N. Wood simon.wood@r-project.org


Wood S.N. (2017) Generalized Additive Models: An Introduction with R (2nd edition). Chapman and Hall/CRC Press.


See Also

choose.k, gam, gam.check


dat <- gamSim(1,n=200)

[Package mgcv version 1.8-31 Index]