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)
How can I extract the effect estimates of pm10median and 95% CI of x and export the output to CSV or any other option?
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\
If you have a number of gam models, say ap1
, ap2
, ap3
and you wish to deal with them systematically, an R
ish 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With