I am currently trying to write my first loop for lagged regressions on 30 variables. Variables are labeled as rx1, rx2.... rx3, and the data frame is called my_num_data.
I have created a loop that looks like this:
z <- zoo(my_num_data)
for (i in 1:30)
{dyn$lm(my_num_data$rx[i] ~ lag(my_num_data$rx[i], 1)
+ lag(my_num_data$rx[i], 2))
}
But I received an error message:
Error in model.frame.default(formula = dyn(my_num_data$rx[i] ~ lag(my_num_data$rx[i], : invalid type (NULL) for variable 'my_num_data$rx[i]'
Can anyone tell me what the problem is with the loop?
Thanks!
First problem, I'm pretty sure the function you're looking for is
dynlm()
, without the$
character. Second, using$rx[i]
doesn't concatenaterx
and the contents ofi
, it selects the (single) element in$rx
with indexi
. Try this... edited I don't have your data, so I can't test it on my machine:and then list element
results[[1]]
will be the results from the first regresssion, and so on.Note that this assumes your
my_num_data
data.frame ONLY consists of columnsrx1
,rx2
, etc.This produces a list,
L
, whose ith component has the name of the ith column ofz
and whose content is the regression of the ith column ofz
on its first two lags.Lag
is same aslag
except for a reversal of argument k's sign.I am not super familiar with R, but it appears you are trying to increase the index of rx. Is rx a vector with values at different indices? If not the solution my be to concatenate a string
Again, I may be way off here, as this if my first post and R is still pretty new to me.