I am running regressions over a list() of items using a for loop and I would like to replace the reference to the given list input with the name of that variable rather than its index. For example:
frame <- data.frame(y = rnorm(10), x1 = rnorm(10), x2 = rnorm(10), x3 = rnorm(10) )
x.list <- list(frame$x1,frame$x2,frame$x3)
fit <- list()
for(i in 1:length(x.list)){ fit[[i]] <- summary(lm(frame$y ~ x.list[[i]]))}
fit
I would like each item in fit to refer to "x1", "x2", "x3" instead of "x.list[[i]]". Thanks for any thoughts on this.
Don't define x.list, just iterate over the names:
fit <- vector("list",3)
for ( i in c("x1","x2","x3") ) fit[[i]] <- summary(lm(frame$y ~ frame[[i]]))
Save lms instead. It's likely that you'll want more than the summary, so just save the lms:
xs <- c("x1","x2","x3")
xs <- setNames(xs,xs)
fit_lm <- lapply(xs,function(i)lm(frame$y ~ frame[[i]]))
You can look at summary with lapply(fit_lm,summary), but also look at coefficients, with
sapply(fit_lm,`[[`,"coefficients")
# x1 x2 x3
# (Intercept) 0.1417501 0.2974165 0.25085281
# frame[[i]] 0.2318912 -0.1468433 -0.08783857
If you want a return value, lapply is preferable:
xs <- names(frame)[-1]
setNames(
lapply(xs, function(x, dat) {
f <- as.formula(paste("y", x, sep = "~"))
summary(lm(f, data = dat))
}, dat = frame),
xs)
However, the same strategy would work with a for loop.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With