I'm trying to write a for loop that runs the same regression (same dependent and independent variables) 4 times separately over the 4 different levels of a factor variable. I then want to save the output of each linear regression Each level has approx ~ 500 rows of data.
My initial thought was to do something like this, but I am new to R and the different methods of iteration.
Regressionresults <- list()
for (i in levels(mydata$factorvariable)) {
Regressionresults[[i]] <- lm(dependent ~ ., data = mydata)
}
I suspect that this is quite easy to do but I don't know how.
If you could also direct me to any help documentation or other resource where I can learn how to write these types of loops so I don't have to ask similar questions again, I'd be grateful.
Many thanks in advance!