justaneconomist
justaneconomist

Reputation: 3

Add a column with lagged (or differenced) value in panel dataset for Panel Local Projection in R

I want to use the Panel Local Projections method in R, using the "lpirfs" package and particularly the "lp_lin_panel()" command. However, I suspect my shock to be endogeneous so I want to create a new column with the lagged value of the shock.

I give an example with the "Parity" data in the package plm :

require(lpirfs)
require(dplyr)
require(plm)
data("Parity")
View(Parity)

p <- Parity %>% dplyr::select(country, time, everything())


lp_panel <- lp_lin_panel(p, 
                                    endog_data="lp", 
                                    shock="is", 
                                    diff_shock=F, 
                                    confint=1.96, 
                                    hor=10, 
                                    cumul_mult=T, 
                                    c_exog_data="ls",
                                    l_exog_data=c("ls", "lp"), 
                                    lags_exog_data=1, 
                                    panel_model="within", panel_effect = "individual")
plot(lp_panel)

Thid code works. However, say that I want to create a new column, with the lagged value of is (the same problem arise with the diff() function !) :

pp <- p %>% 
  mutate(lagged_is = lag(is))

When I check my database, the new column is the exact same one as the original (is). If i want to add the differenced value of is (or any other column), i obtain the following Error :

"Error in mutate(): ! Problem while computing lagged_is = diff(is). x lagged_is must be size 1768 or 1, not 1767."

So is there a way to create a new column with the diff/lagged value of a variable in a panel dataset, and what is the simplest way (I've been thinking about it and all solutions are long as I need to recreate a dataset and withdraw the first column, so to go from long to wide...) ?

Upvotes: 0

Views: 98

Answers (1)

justaneconomist
justaneconomist

Reputation: 3

I finally found the answer, which is really simple ! the lag() function exists in many packages, including in base R. The lag() function in dplyr is the most efficient one here. Also, considering I have a panel dataset, I had to group by country before adding the lag, so the final (working) code is the following :

pp <- p %>%
 group_by(country) %>%
 mutate(lagged_is <- dplyr::lag(is))

Upvotes: 0

Related Questions