It seems like you have a column with space in its name. Thus, you need quotes as I show below:
# create a data set
set.seed(1)
finalmev <- data.frame(ODR = 1:4,
`Overnight.Deposit 1` = rnorm(4),
`Overnight.Deposit 2` = rnorm(4),
check.names = FALSE)
# reproduce the error
predictorlist <- colnames(finalmev)[2:NCOL(finalmev)]
for (i in predictorlist){
model <- summary(lm(paste("ODR ~", i[[1]]), data=finalmev))
}
#R> Error in str2lang(x) : <text>:1:25: unexpected numeric constant
#R> 1: ODR ~ Overnight.Deposit 1
#R> ^
# fix the error using quotes
for (i in predictorlist)
model <- summary(lm(sprintf("ODR ~ `%s`", i[[1]]), data=finalmev))
# actually save all the output as pointed out by Ronak Shah
res <- lapply(
tail(colnames(finalmev), -1),
function(x) eval(bquote(summary(lm(.(sprintf("ODR ~ `%s`", x)),
data=finalmev)))))
# show the result
res
#R> [[1]]
#R>
#R> Call:
#R> lm(formula = "ODR ~ `Overnight.Deposit 1`", data = finalmev)
#R>
#R> Residuals:
#R> 1 2 3 4
#R> -0.9534 -0.5809 1.2087 0.3256
#R>
#R> Coefficients:
#R> Estimate Std. Error t value Pr(>|t|)
#R> (Intercept) 2.4386 0.5950 4.098 0.0547 .
#R> `Overnight.Deposit 1` 0.7746 0.6213 1.247 0.3387
#R> ---
#R> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#R>
#R> Residual standard error: 1.186 on 2 degrees of freedom
#R> Multiple R-squared: 0.4374, Adjusted R-squared: 0.156
#R> F-statistic: 1.555 on 1 and 2 DF, p-value: 0.3387
#R>
#R>
#R> [[2]]
#R>
#R> Call:
#R> lm(formula = "ODR ~ `Overnight.Deposit 2`", data = finalmev)
#R>
#R> Residuals:
#R> 1 2 3 4
#R> -1.6293 0.3902 0.2308 1.0083
#R>
#R> Coefficients:
#R> Estimate Std. Error t value Pr(>|t|)
#R> (Intercept) 2.3372 0.7282 3.209 0.0849 .
#R> `Overnight.Deposit 2` 0.8865 1.1645 0.761 0.5260
#R> ---
#R> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#R>
#R> Residual standard error: 1.392 on 2 degrees of freedom
#R> Multiple R-squared: 0.2247, Adjusted R-squared: -0.163
#R> F-statistic: 0.5795 on 1 and 2 DF, p-value: 0.526
#R>
I use eval(bquote(...))
to get a nice output. Notice that you can change colnames(finalmev)[2:ncol(finalmev)]
to tail(colnames(finalmev), -1)
. As remarked above, Ronak Shah shows that you actually only save the last output in your for loop.
Two other alternatives are:
# move out sprintf
res1 <- lapply(sprintf("ODR ~ `%s`", tail(colnames(finalmev), -1)),
function(frm) eval(bquote(summary(lm(.(frm), data = finalmev)))))
# in R 4.1.0 or greater
res2 <- tail(colnames(finalmev), -1) |>
sprintf(fmt = "ODR ~ `%s`") |>
lapply(\(frm) eval(bquote(summary(lm(.(frm), data = finalmev)))))
# we get the same
all.equal(res, res2)
#R> [1] TRUE
all.equal(res1, res2)
#R> [1] TRUE