# Loop through variables and estimate effects on several outcomes

5 messages
Open this post in threaded view
|

## Loop through variables and estimate effects on several outcomes

 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.htmThe 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) })
Open this post in threaded view
|

## Re: Loop through variables and estimate effects on several outcomes

 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-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Loop through variables and estimate effects on several outcomes

 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