I am trying to loop over a set of variables in a lm formula. The code I have tried is this one:
parametric_1km_control <- list()
parametric_1km_control_fx <- list()
for(i in c(1:4)){
for(t in c(1997:2013)){
parametric_1km_control[[str_c(i, t)]] <- lm(log(0.01 + as.formula(str_c("dm", t))) ~
as.factor(as.formula(str_c("t", t - 1))) + poly(I(dist_p / 1000), i) + a + b + c +
factor(ID), data = merge_rasters_dataframes_bw[["1000"]])
parametric_1km_control_fx[[str_c(i, t)]] <- lm(log(0.01 + as.formula(str_c("dm", t))) ~
as.factor(as.formula(str_c("t", t - 1))) + poly(I(dist_p / 1000), i) + a + b + c +
factor(ID), data = merge_rasters_dataframes_bw[["1000"]])
}
}
The idea is to try all the dm + t and t + t-1 (that t-1 is lagged) variables into the regression formula. I know that loops are not the most efficient way of doing things in R, but I am short of imagination when it comes to formulas and not all the cases can be reduced to functional loop as lapply family.
Thanks in advanced!
