I am trying to run this code from this post: looping with iterations over two lists of variables for a multiple regression in R with modified variable and data frame names, because it seems to do exactly what I want and uses a very similar dataset. However, it keeps giving me an error and I don't know why, so I would really appreciate if someone could help me to understand the error or the corresponding line of code so I could try to figure out what's wrong.
for(i in 1:n) {
vars = names(output)[names(output) %in% paste0(c(".PRE", ".POST"), i)]
models[[as.character(i)]] = lm(paste("growth_rate ~ ", paste(vars, collapse=" + ")),
data = output)
}
Error in parse(text = x, keep.source = FALSE) :
<text>:2:0: unexpected end of input
1: growth_rate ~
^
My dataset looks almost like the one given in the above mentioned post besides the fact that my "RDPI_T" and "DRY_T" variables are in an alternating order (which I dont think matters in this case). The analogous variables I have are 69 PRE variables called id1.PRE, id2.PRE ... id69.PRE and also 69 POST variables called id1.POST, id2.POST ... id69.POST in the output dataset. Also, growth_rate is in the same dataset called output.
Additionally, I would also like to add 2 more independent variables that are regular and do not come from a list: country and year but I am unsure how to incorporate that here?
Any help would be appreciated. Thank you!