Extract estimates of GAM

与世无争的帅哥 提交于 2019-12-04 08:21:33

Save the summary of the model

summary_model <- summary(ap1)

The part you want (for the linear terms) is in the p.table element

summary_model$p.table
                Estimate   Std. Error      z value    Pr(>|z|)
(Intercept) 4.7457425965 1.480523e-03 3205.4510971 0.000000000
pm10median  0.0002551498 9.384003e-05    2.7189871 0.006548217
so2median   0.0008898646 5.543272e-04    1.6053056 0.108426561
o3median    0.0002212612 2.248015e-04    0.9842516 0.324991826


write.csv(summary_model$p.table, file = 'p_table.csv')

If you want the spline terms, then this is

summary_model$s.table
               edf     Ref.df    Chi.sq       p-value
s(time) 167.327973 187.143378 1788.8201 4.948832e-259
s(tmpd)   8.337121   8.875807  110.5231  1.412415e-19

You can calculate the 95% CI by hand and add these If you wish. (Will use Z score due to high DF)

p_table <- data.frame(summary_model$p.table)
p_table <- within(p_table, {lci <- Estimate - qnorm(0.975) * Std..Error
                            uci <- Estimate + qnorm(0.975) * Std..Error})
p_table
               Estimate   Std..Error      z.value    Pr...z..          uci           lci
(Intercept) 4.7457425965 1.480523e-03 3205.4510971 0.000000000 4.7486443674  4.742841e+00
pm10median  0.0002551498 9.384003e-05    2.7189871 0.006548217 0.0004390729  7.122675e-05
so2median   0.0008898646 5.543272e-04    1.6053056 0.108426561 0.0019763260 -1.965968e-04
o3median    0.0002212612 2.248015e-04    0.9842516 0.324991826 0.0006618641 -2.193416e-04\

Edit in light of comments

If you have a number of gam models, say ap1, ap2, ap3 and you wish to deal with them systematically, an Rish approach is to put them in a list and use lapply

# create list
model_list <- list(ap1, ap2, ap3)
# give the elements useful names
names(model_list) <- c('ap1','ap2','ap3')

# get the summaries using `lapply

summary_list <- lapply(model_list, summary)

# extract the coefficients from these summaries

 p.table_list <- lapply(summary_list, `[[`, 'p.table')

 s.table_list <- lapply(summary_list, `[[`, 's.table')

the lists you have created now the relevant components.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!