spg - 1 year ago 71

R Question

I've been trying hard to recreate this model in R:

Model

(FARHANI 2012)

I've tried many things, such as a cumsum paste - however that would not work as I could not assign strings the correct variable as it kept thinking that L was a function.

I tried to do it manually, I'm only looking for p,q = 1,2,3,4,5 however after starting I realized how inefficient this is.

This is essentially what I am trying to do

`model5 <- vector("list",20)`

#p=1-5, q=0

model5[[1]] <- dynlm(DLUSGDP~L(DLUSGDP,1))

model5[[2]] <- dynlm(DLUSGDP~L(DLUSGDP,1)+L(DLUSGDP,2))

model5[[3]] <- dynlm(DLUSGDP~L(DLUSGDP,1)+L(DLUSGDP,2)+L(DLUSGDP,3))

model5[[4]] <- dynlm(DLUSGDP~L(DLUSGDP,1)+L(DLUSGDP,2)+L(DLUSGDP,3)+L(DLUSGDP,4))

model5[[5]] <- dynlm(DLUSGDP~L(DLUSGDP,1)+L(DLUSGDP,2)+L(DLUSGDP,3)+L(DLUSGDP,4)+L(DLUSGDP,5))

I'm also trying to do this for regressing DLUSGDP on DLWTI (my oil variable's name) for when p=0, q=1-5 and also p=1-5, q=1-5

cumsum would not work as it would sum the variables rather than treating them as independent regresses.

My goal is to run these models and then use IC to determine which should be analyzed further.

I hope you understand my problem and any help would be greatly appreciated.

Answer Source

I think this is what you are looking for:

```
reformulate(paste0("L(DLUSGDP,", 1:n,")"), "DLUSGDP")
```

where `n`

is some order you want to try. For example,

```
n <- 3
reformulate(paste0("L(DLUSGDP,", 1:n,")"), "DLUSGDP")
# DLUSGDP ~ L(DLUSGDP, 1) + L(DLUSGDP, 2) + L(DLUSGDP, 3)
```

Then you can construct your model fitting by

```
model5 <- vector("list",20)
for (i in 1:20) {
form <- reformulate(paste0("L(DLUSGDP,", 1:i,")"), "DLUSGDP")
model5[[i]] <- dynlm(form)
}
```