arun
2013-Jun-06 13:41 UTC
[R] 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 >Theexample 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)>})