Example:
df <- data.frame(A=1:5, B=2:6, C=3:7,D=4:8,E=5:9,F=6:10)
I want make a regression loop lm(y,x) using like y the col 1 and 2 and like x the rest of the cols.
my idea:
lmf <- function (y,x) {
f <- lm(y ~ x, data=df)
cbind(summary(f)$r.squared,summary(f)$coefficients)
}
for(y in 1:3)
{
R<- apply(df[,3:6], 2, lmf(y,x)); R
}
error: Error in model.frame.default(formula = y ~ x, data = df, drop.unused.levels = TRUE) : variable lengths differ (found for 'x')
I give this example very small but my data are 50 cols for the y and 300 cols for the x.
What I want is the same to do: lm(df$1~df$3, data=df); lm(df$1~df$4, data=df),[...], lm(df$2~df$3, data=df)... but in automatic way. Moreover I want to extract the results $coefficients and $r.squared.