Convert mgcv or gamm4 gam/bam output to dataframe

741 views Asked by At

The broom package has a great tidy() function for the summary results of simple linear models such as those generated by lm(). However, tidy() does not work for mgcv::bam(), mgcv::gam() or gamm4::gamm4. The bam below produces the following:

library(mgcv)
set.seed(3)
dat <- gamSim(1,n=25000,dist="normal",scale=20)
bs <- "cr";k <- 12
b <- bam(y ~ s(x0,bs=bs)+s(x1,bs=bs)+s(x2,bs=bs,k=k)+
           s(x3,bs=bs),data=dat)
summary(b)
tidy(b)
glance(b)

Output of above code:

> summary(b)

Family: gaussian 
Link function: identity 

Formula:
y ~ s(x0, bs = bs) + s(x1, bs = bs) + s(x2, bs = bs, k = k) + 
    s(x3, bs = bs)

Parametric coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)   7.8918     0.1275   61.88   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Approximate significance of smooth terms:
        edf Ref.df      F  p-value    
s(x0) 3.113  3.863  6.667 3.47e-05 ***
s(x1) 2.826  3.511 63.015  < 2e-16 ***
s(x2) 8.620  9.905 52.059  < 2e-16 ***
s(x3) 1.002  1.004  3.829   0.0503 .  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

R-sq.(adj) =  0.0295   Deviance explained = 3.01%
fREML = 1.1057e+05  Scale est. = 406.15    n = 25000
> tidy(b)
data frame with 0 columns and 0 rows
> glance(b)
Error in `$<-.data.frame`(`*tmp*`, "logLik", value = -110549.163197452) : 
  replacement has 1 row, data has 0

How can I convert the summary to a dataframe so I can access outputs like the coefficients?

0

There are 0 answers