[R] How to get variable name while doing series of regressions in an automated manner?
Marc Schwartz
marc_schwartz at me.com
Tue Oct 27 18:07:33 CET 2015
> On Oct 27, 2015, at 10:19 AM, Ravi Varadhan <ravi.varadhan at jhu.edu> wrote:
>
> 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,
Something like this, using the 'iris' dataset might be helpful as an example:
# Define the response variables
VarNames <- c("Sepal.Length", "Sepal.Width", "Petal.Length")
# Create the formulae
> paste0("log(", VarNames, ") ~ Petal.Width + Species")
[1] "log(Sepal.Length) ~ Petal.Width + Species"
[2] "log(Sepal.Width) ~ Petal.Width + Species"
[3] "log(Petal.Length) ~ Petal.Width + Species"
# Create a list of model summary objects
# The result of paste0() will be coerced to a formula by lm()
# if a valid formula, so no need to call as.formula()
MODS <- lapply(paste0("log(", VarNames, ") ~ Petal.Width + Species"),
function(x) summary(lm(x, data = iris)))
You can either use the original 'VarNames' vector for the source response variables, or consider:
> as.character(formula(MODS[[1]]))
[1] "~" "log(Sepal.Length)"
[3] "Petal.Width + Species"
> sapply(MODS, function(x) formula(x)[[2]])
[[1]]
log(Sepal.Length)
[[2]]
log(Sepal.Width)
[[3]]
log(Petal.Length)
Regards,
Marc Schwartz
More information about the R-help
mailing list