Reputation: 13
I am want to run a loop regression for fund1 till fund10 based on the the LIQ-factor. I want to do this regression:
lm(fund1S~ LIQ, data = Merge_Liq_Size)
but for all of the funds simultaneously.
I have attached some picture of the dataset to show you the setup. The dataset has 479 observation/rows. Can anyoune help me to sturcture a code? Sorry if this question is phrased in a wrong way.
Upvotes: 1
Views: 62
Reputation: 887951
If it is lm
, we can do this without lapply
as well i.e. create a matrix
as the dependent variable and construct the formula
lm(as.matrix(Merge_Liq_Size[paste0("fund", 1:10, "S")]) ~ LIQ, data = Merge_Liq_Size)
Using a small reproducible example
> data(mtcars)
> lm(as.matrix(mtcars[1:3]) ~ vs, data = mtcars)
Call:
lm(formula = as.matrix(mtcars[1:3]) ~ vs, data = mtcars)
Coefficients:
mpg cyl disp
(Intercept) 16.617 7.444 307.150
vs 7.940 -2.873 -174.693
Upvotes: 0
Reputation: 16998
Perhaps this is what you are looking for:
my_models <- lapply(paste0("fund", 1:10, "S ~ LIQ"), function(x) lm(as.formula(x), data = Merge_Liq_Size))
You can access each model by my_models[[1]]
to my_models[[10]]
.
Upvotes: 1