[R] How to get variable name while doing series of regressions in an automated manner?

Ravi Varadhan ravi.varadhan at jhu.edu
Tue Oct 27 16:19:51 CET 2015


Hi,

I am running through a series of regression in a loop as follows:

results <- vector("list", length(mydata$varnames))

for (i in 1:length(mydata$varnames)) {
results[[i]] <- summary(lm(log(eval(parse(text=varnames[i]))) ~ age + sex + CMV.status, data=mydata))
}

Now, when I look at the results[i]] objects, I won't be able to see the original variable names.  Obviously, I will only see the following:

Call:
lm(formula = log(eval(parse(text = varnames[i]))) ~ age + sex + CMV.status,
    data = mydata)


Is there a way to display the original variable names on the LHS?  In addition, is there a better paradigm for doing these type of series of regressions in an automatic fashion?

Thank you very much,
Ravi

Ravi Varadhan, Ph.D. (Biostatistics), Ph.D. (Environmental Engg)
Associate Professor,  Department of Oncology
Division of Biostatistics & Bionformatics
Sidney Kimmel Comprehensive Cancer Center
Johns Hopkins University
550 N. Broadway, Suite 1111-E
Baltimore, MD 21205
410-502-2619


	[[alternative HTML version deleted]]



More information about the R-help mailing list