Edit: Trying to implement the solution provided in the answer below.
I'm providing new sample data as it fits very well my data.
> head(Grunfeld, 25)
firm year inv value capital
1 1 1935 317.6 3078.5 2.8
2 1 1936 391.8 4661.7 52.6
3 1 1937 410.6 5387.1 156.9
4 1 1938 257.7 2792.2 209.2
5 1 1939 330.8 4313.2 203.4
6 1 1940 461.2 4643.9 207.2
7 1 1941 512.0 4551.2 255.2
8 1 1942 448.0 3244.1 303.7
9 1 1943 499.6 4053.7 264.1
10 1 1944 547.5 4379.3 201.6
11 1 1945 561.2 4840.9 265.0
12 1 1946 688.1 4900.9 402.2
13 1 1947 568.9 3526.5 761.5
14 1 1948 529.2 3254.7 922.4
15 1 1949 555.1 3700.2 1020.1
16 1 1950 642.9 3755.6 1099.0
17 1 1951 755.9 4833.0 1207.7
18 1 1952 891.2 4924.9 1430.5
19 1 1953 1304.4 6241.7 1777.3
20 1 1954 1486.7 5593.6 2226.3
21 2 1935 209.9 1362.4 53.8
22 2 1936 355.3 1807.1 50.5
23 2 1937 469.9 2676.3 118.1
24 2 1938 262.3 1801.9 260.2
25 2 1939 230.4 1957.3 312.7
library(plm)
data("Grunfeld", package="plm")
Grunfeld$firm <- as.factor(Grunfeld$firm)
#adding lagged variable (+1)
Grunfeld$inv.plus1 <- NA
for (f in levels(Grunfeld$firm)) {
Grunfeld[which(Grunfeld$firm == f),]$inv.plus1 <- c(Grunfeld[which(Grunfeld$firm == f),]$inv[-1],NA)
}
#adding lagged variable (+2)
Grunfeld$inv.plus2 <- NA
for (f in levels(Grunfeld$firm)) {
Grunfeld[which(Grunfeld$firm == f),]$inv.plus2 <- c(Grunfeld[which(Grunfeld$firm == f),]$inv[-c(1,2)],NA)
}
#adding lagged variable (-1)
Grunfeld$inv.minus1 <- NA
for (f in levels(Grunfeld$firm)) {
Grunfeld[which(Grunfeld$firm == f),]$inv.minus1 <- c(Grunfeld[which(Grunfeld$firm == f),]NA,$inv[-1],)
}
While it works for the (+1) variable I'm unable to derive the correct code for (+2) or (-1). What am i doing wrong?
I'm using the plm package and I would like to regress the following: lm(inv(t+1) ~ inv(t) + other variables(t)) as well as lm("inv(t+2)" ~ inv(t) + other variables(t)) and lm("inv(t+3)" ~ inv(t) + other variables(t))
Is there a convenient way in how to add lagged variables in both directions (i.e. inv(t+1), inv(t-1) for a horizon of up to 3 years? My data is in a balanced format, although there are quite many "NA". I don't know if it is still considered as a balanced panel. Is there any package or formula? Thank you in advance for your help.
Edit: I tried to do the same as in the answer provided below:
dd$earnings.plus1 <- c(dd$earnings[-1], NA)
dd$earnings.plus2 <- c(dd$earnings[-c(1:2)], NA, NA)
but instead i'm trying to define dd$earnings.minus1
z<- nrows(set)
dd$earnings.minus1 <- c(NA, dd$earnings[-z])
but it is not working properly as the last value from firm 1 is moved to firm 2. This doesn't seem to happen with the solution above. what's the difference here?