Calculating between and within variance (mean squares) in mgcv::gam

Viewed 184

I would like to calculate the between and within variability of a parametric term (or mean squares of parametric term and residuals) in a mgcv::gam, but can't figure out how to do that with mgcv.

Below is an example. I've created a gam object. Then used the summary and anova.gam functions, but they only provide F-values. I assume the F-value of the parametric term can be interpreted like its linear model equivalent - as the mean sq of group divided by the mean sq of residuals.

library(mgcv)
demo <- read.csv("https://stats.idre.ucla.edu/stat/data/demo3.csv")

## Convert variables to factor
demo <- within(demo, {
  group <- factor(group)
  time <- factor(time)
  id <- factor(id)
})

gam1 <- gam(pulse ~ group + s(id, bs="re"), method="ML", data = demo)
summary(gam1)

Family: gaussian 
Link function: identity 

Formula:
pulse ~ group + s(id, bs = "re")

Parametric coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)   23.583      3.425   6.886 6.48e-07 ***
group2        18.417      4.844   3.802 0.000976 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Approximate significance of smooth terms:
            edf Ref.df F p-value
s(id) 7.944e-05      6 0       1

R-sq.(adj) =  0.369   Deviance explained = 39.7%
-ML = 92.376  Scale est. = 140.77    n = 24
anova.gam(gam1)

Family: gaussian 
Link function: identity 

Formula:
pulse ~ group + s(id, bs = "re")

Parametric Terms:
      df     F  p-value
group  1 14.46 0.000976

Approximate significance of smooth terms:
            edf    Ref.df F p-value
s(id) 7.944e-05 6.000e+00 0       1

I would like to calculate the mean squares. With a simple linear model, like in the example below, I can use the anova function and get the mean squares. How can I calculate the mean squares in a gam using mgcv?

library(stats)
lm1 <- lm(pulse ~ group + id, data = demo)
anova(lm1)

Analysis of Variance Table

Response: pulse
          Df  Sum Sq Mean Sq F value   Pr(>F)   
group      1 2035.04 2035.04  10.636 0.004903 **
id         6   35.58    5.93   0.031 0.999829   
Residuals 16 3061.33  191.33                    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
0 Answers
Related