将mgcv或gamm4 gam / bam输出转换为数据帧

时间:2017-08-31 04:14:17

标签: r lm tidyr gam broom

broom包具有很好的tidy()函数,可用于简单线性模型的摘要结果,例如由lm()生成的模型。但是,tidy()不适用于mgcv::bam()mgcv::gam()gamm4::gamm4。下面的bam会产生以下结果:

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)

输出上述代码:

> 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

如何将摘要转换为数据框,以便可以访问系数等输出?

0 个答案:

没有答案