I've just started out learning R and can't seem to get this loop to work. I have a data frame containing 250 rows and 503 columns (y) and another data frame containing 250 rows and 1 column (x).
I am trying to get a loop to run 503 separate regressions without having to type it in individually ie.
(output_1 <- lm(y$1st column ~ x))
(output_2 <- lm(y$2nd column ~ x))
across all 250 rows in each regression.
I tried this loop:
for (i in 1:503) {
output_loop <- lm(y[,i]~x)
}
output_total <- cbind(output$coefficients)
but this only gave me one intercept and one coefficient, as opposed to 503 intercepts and 503 coefficients.
The rows of each data frame have time markers that are aligned in the format yyyy-mm-dd but I don't believe this affects the regression as the intercept and coefficients output sought are independent of time.
I have also tried using a basic lm:
(output <- lm(y~x))
output_total <- cbind(output$coefficients)
and this gives 503 intercepts and 503 coefficients, however the output is wrong when I spot checked the output against some columns (running individual regression as above).
Any help on this loop is much appreciated!
Thank you
I'm not sure you're approaching this the best way, but here's something that I think will achieve what you described.