[R] Loop through columns of outcomes
Kuma Raj
pollaroid at gmail.com
Tue Nov 12 14:28:59 CET 2013
Thanks for the script which works perfectly. I am interested to do
model checking and also interested to extract the coefficients for
linear and spline terms. For model checkup I could run this script
which will give different plots to test model fit: gam.check(m2[[1]]).
Thanks to mnel from SO I could also extract the linear terms with the
following script:
m2 <- unlist(m1, recursive = FALSE) ## unlist
First extract the model elements:
mod1<-m2[[1]]
mod2<-m2[[2]]
mod3<-m2[[3]]
mod4<-m2[[4]]
mod5<-m2[[5]]
mod6<-m2[[6]]
And run the following:
mlist <- list(mod1, mod2, mod3,mod4,mod5,mod6) ## Creates a list of models
names(mlist) <- list("mod1", "mod2", "mod3","mod4","mod5","mod6")
slist <- lapply(mlist, summary) ## obtain summaries
plist <- lapply(slist, `[[`, 'p.table') ## list of the coefficients
linear terms
For 6 models this is relatively easy to do, but how could I shorten
the process if I have large number of models?
Thanks
On 12 November 2013 12:32, Rui Barradas <ruipbarradas at sapo.pt> wrote:
> Hello,
>
> Use nested lapply(). Like this:
>
>
>
> m1 <- lapply(varlist0,function(v) {
> lapply(outcomes, function(o){
> f <- sprintf("%s~ s(time,bs='cr',k=200)+s(temp,bs='cr') +
> Lag(%s,0:6)", o, v)
>
> gam(as.formula(f),family=quasipoisson,na.action=na.omit,data=df)
> })})
>
> m1 <- unlist(m1, recursive = FALSE)
> m1
>
>
> Hope this helps,
>
> Rui Barradas
>
>
> Em 12-11-2013 09:53, Kuma Raj escreveu:
>>
>> I have asked this question on SO, but it attracted no response, thus I am
>> cross- posting it here with the hope that someone would help.
>>
>> I want to estimate the effect of pm10 and o3 on three outcome(death, cvd
>> and resp). What I want to do is run one model for each of the main
>> predictors (pm10 and o3) and each outcome(death, cvd and resp). Thus I
>> expect to obtain 6 models. The script below works for one outcome (death)
>> and I wish to use it for more dependent variables.
>>
>>
>>
>> library(quantmod)
>> library(mgcv)
>> library(dlnm)
>> df <- chicagoNMMAPS
>> outcomes<- c("death", "cvd", "resp ")
>> varlist0 <- c("pm10", "o3")
>>
>> m1 <- lapply(varlist0,function(v) {
>> f <- sprintf("death~ s(time,bs='cr',k=200)+s(temp,bs='cr') +
>> Lag(%s,0:6)",v)
>> gam(as.formula(f),family=quasipoisson,na.action=na.omit,data=df)
>> })
>>
>> Thanks
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
More information about the R-help
mailing list