I am running a generalized additive model (GAM), but when I check the model diagnostics (using gam.check()) I run into problems. I have tried adjusting the value of k, but this isn't working. What am I missing?

Note that gam.check() lets you see if the number of basis functions (k) is appropriate (as per this OP). In my case, it shows that it is significant. I tried different values of k to see what value of k would give me a non-significant p-value, but it is always significant. Does this mean that the k is appropriate or is there a different test to see if the k value is appropriate for the smoothing term?

I'm using R, version 3.6.1; mgcv version 1.8-34; gamm4 version 0.2-6.

The data (called prime) I used is available here.

Here is how my data frame is currently structured (first 6 rows):

ID       DaysBeforeMigration  AdultMass  OffspringMass
A00001   59                   59.52239   57.18145
A00002   61                   143.8808   61.75128
A00002   63                   147.7373   68.80916
A00002   61                   143.8808   41.82444
A00002   63                   147.7373   61.4211
A00002   61                   143.8808   61.64883

Here is the code and packages to run my generalized additive model:

prime <- read.csv("MyData.csv”)
mod1 <- gamm4(OffspringMass ~ DaysBeforeMigration + s(AdultMass, 
            k=9), random=~(1|ID), data=prime) 


> summary(mod1$gam) 
Family: gaussian
Link function: identity

OffspringMass ~ DaysBeforeMigration + s(AdultMass, k = 9)
Parametric coefficients:
                    Estimate Std. Error t value Pr(>|t|)   
(Intercept)         53.73208    3.88430  13.833   <2e-16 ***
DaysBeforeMigration  0.09548    0.07662   1.246    0.215   
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Approximate significance of smooth terms:
               edf Ref.df     F p-value   
s(AdultMass) 1.935  1.935 13.86 5.8e-06 ***
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
R-sq.(adj) =  0.231  
lmer.REML = 830.06  Scale est. = 63.323    n = 109

Diagnostic output:

> gam.check(mod1$gam)
'gamm' based fit - care required with interpretation.
Checks based on working residuals may be misleading.
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(AdultMass) 8.00 1.94    0.77   0.005 **
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Diagnostic figure code:

plot.gam(mod1$gam, residuals = TRUE, pch =1, cex = 1, shade = 
         TRUE, shade.col = "lightblue", seWithMean = TRUE, 
         pages = 1, all.terms = TRUE)

The diagnostic figures:

enter image description here enter image description here


1 Answer 1


It's not a huge problem in your case.

The best I can find on it is p.330-331 of this guide by Simon Wood.

Because the edf is way below the basis dimension I don't see it as a problem (quote from Wood on page 331: "The...test still gives a low p-value, but since the edf is...below the basis dimension, and increasing the basis dimension barely changes the fitted values,...stick with k=100...").

So, fitting more basis functions by setting higher k would not make much logical sense given the size of the data set.

The book also talks about finding the best k, but in the case of your data allowing the default seems fine as the model will converge to a straight line (edf = 1).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.