jeudi 4 juin 2015

Add regressor in a for loop using the update function in R

I want to run a distributed lag modell with different lag length using the dynlm function. Here is the data and the package I am using

require(dynlm)

Time <- 1950:1993

Y <- c(5820, 5843, 5917, 6054, 6099, 6365, 6440, 6465, 6449, 6658,  6698, 6740, 6931, 
       7089, 7384, 7703, 8005, 8163, 8506, 8737, 8842, 9022, 9425,  9752, 9602, 9711, 
       10121, 10425, 10744, 10876, 10746, 10770, 10782, 11179, 11617, 12015, 12336, 
       12568, 12903, 13029, 13093, 12899, 13110, 13391)

X <- c(6284, 6390, 6476, 6640, 6628, 6879, 7080, 7114, 7113, 7256, 7264, 7382, 7583, 7718,  
       8140, 8508, 8822, 9114, 9399, 9606, 9875, 10111, 10414, 11013, 10832, 10906, 11192, 
       11406, 11851, 12039, 12005, 12156, 12146, 12349, 13029, 13258, 13552, 13545, 13890, 
       14005, 14101, 14003, 14279, 14341)

data <- data.frame(Time, Y, X)
data_ts <- ts(data, start = 1950, end = 1993, frequency = 1)

and this is the for loop I was trying.

m <- 1:10
models <- list() 
for (i in m){
   if (i == 1){
     models[[i]] <- dynlm(log(Y) ~ log(X) + log(L(X, i)), data = data_ts)
  } else {
     models[[i]] <- update(models[[i-1]], . ~ . + log(L(X, i)))
  }
}

I was hoping it would simply add one regressor with lag length i to the previous regression. However, it is not doing that and I cant figure out why. Any suggestions?


Remark:

If anyone knows a better/more elegant way of doing what I want to do, please let me know!

Aucun commentaire:

Enregistrer un commentaire