Loop through variables and estimate effects on several outcomes

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Loop through variables and estimate effects on several outcomes

decoder
This post has NOT been accepted by the mailing list yet.
I want to estimate the effects of an exposure on several outcomes. The example in this link provides how to loop though variables which are explanatory variables.  http://www.ats.ucla.edu/stat/r/pages/looping_strings.htm
The example below estimates the effects of several variables on read.  But I want to estimate the effect of  "female" , "race"  ,  "ses"  on  "write" ,  "math"    "science"   one at a time using the hsb data set.  How can I loop through these outcomes?
varlist <- names(hsb2)[8:11]
models <- lapply(varlist, function(x) {
  lm(substitute(read ~ i, list(i = as.name(x))), data = hsb2)
})
Reply | Threaded
Open this post in threaded view
|

Re: Loop through variables and estimate effects on several outcomes

arun kirshna
Hi,
Try:
hsb2 <- read.csv("http://www.ats.ucla.edu/stat/data/hsb2.csv")

varlist<-names(hsb2)[8:10]
fun2<- function(varName){
    res<- sapply(varName,function(x){
              model1<- lm(substitute(cbind(female,race,ses)~i,list(i=as.name(x))),data=hsb2)
                  sM<- summary(model1)
              sapply(sM,function(x) x$coef[2,1])          
             })
            res
                        }        

 fun2(varlist)
#                     write         math      science
#Response female 0.01350896 -0.001563341 -0.006441112
#Response race   0.02412624  0.022474213  0.033622966
#Response ses    0.01585530  0.021064315  0.020692042

A.K.

>This post has NOT been accepted by the mailing list yet.
>I want to estimate the effects of an exposure on several outcomes. The example in this link provides how to loop though variables which are
>explanatory variables.  http://www.ats.ucla.edu/stat/r/pages/looping_strings.htm
>The
 example below estimates the effects of several variables on read.  But I
 want to estimate the effect of  "female" , "race"  ,  "ses"  on
 "write" ,  >"math"    "science"   one at a time using the hsb data set.
 How can I loop through these outcomes?
>varlist <- names(hsb2)[8:11]
>models <- lapply(varlist, function(x) {
 > lm(substitute(read ~ i, list(i = as.name(x))), data = hsb2)
>})

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Loop through variables and estimate effects on several outcomes

decoder
This post has NOT been accepted by the mailing list yet.
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
Reply | Threaded
Open this post in threaded view
|

Re: Loop through variables and estimate effects on several outcomes

arun kirshna
In reply to this post by decoder
Hi,
I assume this is what you wanted:
#########Your code
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 <[hidden email]>
To: arun <[hidden email]>
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.

Thanks for your help
G



On Fri, Jun 7, 2013 at 12:01 AM, <[hidden email]> 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
>
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Loop through variables and estimate effects on several outcomes

decoder
This post has NOT been accepted by the mailing list yet.
Thanks Arun. The code works fine.