提取GAM估算值 [英] Extract estimates of GAM

查看:153
本文介绍了提取GAM估算值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对R并不陌生,目前正在阅读一本书"Generalized Additive Models",这是Wood by R的简介(2006年),并进行了一些练习,特别是有关空气污染和死亡的部分,这是我的专长.兴趣.使用mgcv软件包,我运行以下模型.

I am fairly new to R and presently reading a book "Generalized Additive Models", an Introduction with R by Wood (2006) and going through some of the exercises, particularly the part on air pollution and death which is my area of interest. Using the mgcv package I run the following model.

library(gamair) 
library(mgcv) 
data(chicago) 

ap1<-gam(death ~ pm10median + so2median + o3median +s(time,bs="cr",k=200)+ s(tmpd,bs="cr"), data=chicago,family=poisson)

如何提取pm10median和x的95%CI的效果估计并将输出导出为CSV或任何其他选项?

How can I extract the effect estimates of pm10median and 95% CI of x and export the output to CSV or any other option?

推荐答案

保存模型摘要

summary_model <- summary(ap1)

您想要的部分(对于线性项)在p.table元素中

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

您可以手动计算95%CI,并根据需要添加这些值. (由于DF较高,将使用Z分数)

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\


根据评论进行编辑

如果您有许多游戏模型,例如ap1ap2ap3,并且希望系统地处理它们,那么R ish方法是将它们放在列表中并使用


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.

这篇关于提取GAM估算值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆