# [R] Loop through variables and estimate effects on several outcomes

arun smartpink111 at yahoo.com
Mon Jun 10 03:34:34 CEST 2013

```Hi,
I assume this is what you wanted:
library(gamair)
data(chicago)
library(mgcv)
library(splines)
chicago1<- chicago
chicago1\$date<- seq(from=as.Date("1987-01-01"),to=as.Date("2000-12-31"),length=5114)
chicago1\$trend<-seq(dim(chicago1)[1])
names(chicago1) [2] <-"pm10"
names(chicago1) [3] <-"pm25"
names(chicago1) [4] <-"ozone"
names(chicago1) [5] <-"so2"
names(chicago1) [7]   <-"temp"
chicago1\$cv<-rpois(5114, lambda = 35)
chicago1\$resp<-rpois(5114, lambda = 13)
chicago1\$trend<-seq(dim(chicago1)[1])
chicago1\$year<-as.numeric(format(chicago1\$date,"%Y"))
m1<-glm(death ~ pm10 + pm25+ ns(trend,35) + ns(temp, 6), poisson , na.action = na.omit , data =chicago1)
m4<-gam(death ~ pm10 + pm25+ s(trend,k=35)  + s(temp, k=6), quasipoisson , na.action = na.omit , data =chicago1)

####Extracting estimates

coef(summary(m1))[-1,1][!grepl("ns",names(coef(summary(m1))[-1,1]))]
#        pm10         pm25
# 0.001772681 -0.001481798

summary(m4)\$p.coeff[-1]
#       pm10         pm25
#0.001346275 -0.001182925

varlist<-names(chicago1)[c(1,10:11)]
varlist
#[1] "death" "cv"    "resp"

####glm
fun2glm<- function(varName){
res<- sapply(varName,function(x){
model1<- glm(get(x)~ pm10 + pm25+ ns(trend,35)  + ns(temp, 6) , poisson , na.action = na.omit , data =chicago1)
sM<- coef(summary(model1))[-1,1]
indx<- grepl("ns",names(sM))
sM1<- sM[!indx]
})
res
}

fun2glm(varlist)
#            death            cv          resp
#pm10  0.001772681  0.0003505286 -0.0006736290
#pm25 -0.001481798 -0.0004193602  0.0005567137

####gam
fun2gam<- function(varName){
res<- sapply(varName,function(x){
model1<- gam(get(x)~pm10+pm25+s(trend,k=35)+s(temp,k=6),quasipoisson,na.action=na.omit,data=chicago1)
sM<- summary(model1)\$p.coeff[-1]
})
res
}
fun2gam(varlist)
#            death            cv          resp
#pm10  0.001346275  0.0003902584 -0.0008447569
#pm25 -0.001182925 -0.0007072599  0.0002807046

A.K.

________________________________
From: Gustav Sigtuna <gsigtuna at gmail.com>
To: arun <smartpink111 at yahoo.com>
Sent: Friday, June 7, 2013 5:56 PM
Subject: Re: Loop through variables and estimate effects on several outcomes

Hi Arun,

Thanks for your response. I use GLM and GAM for Poisson distributed data. I have attached a sample code and data. In the summary I am interested to extract the linear estimates and not the spline terms.

G

On Fri, Jun 7, 2013 at 12:01 AM, <smartpink111 at yahoo.com> wrote:

<quote author='decoder'>
>HI,
>No problem.
>Are you using the same dataset or different?  If you are using poisson or other distribution, the summary output table may be a bit different.  If you can show the codes and an example dataset (if different), I can take a look.
>
>
>Thanks A.K.
>
>The code works for lm and thanks for that. I have some outcomes which are
>counts and wanted to run GLM with the same code and got the error message
>below.
>
>> fun2(varlist)
>Error in glm.fit(x = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  :
>  (subscript) logical subscript too long
>
>Can you explain what went wrong with the GLM code and how to rectify it?
>
>Thanks
></quote>
>Quoted from:
>http://r.789695.n4.nabble.com/Loop-through-variables-and-estimate-effects-on-several-outcomes-tp4668814p4668875.html
>
>
>_____________________________________
>Sent from http://r.789695.n4.nabble.com
>
>

```