Create lead and lag year dummies for regression in R

拜拜、爱过 提交于 2020-01-15 10:12:04

问题


This is an example data frame, where PRE5_id1,POST5_id1, PRE5_id2, POST5_id2 are the variables that I would like to get. I am looking for a lead and lag value which will have five values of 1 in the years before natural death (PRE5) and 5 years after the year of natural death (POST5). I am not sure how to stay within the group of country when creating these PRE and POST variables, in which case the PRE and POST variables go to +5 and -5 only within the same country.

I am planning to do a separate regressions for each ID (there are overall 69 natural deaths in my dataset and hence up to ID69) and to include PRE5 and POST5 for every regression, something like this: lm(gdp.growth.rate~country+year+PRE5_id1+POST5_id1) so if there is anyway to create these PRE and POST dummies in the regression that could also work.

> df <- data.frame(country = rep("Angola",length(20)), year=c(1940:1959), leader = c("David", "NA", "NA", "NA","Henry","NA","Tom","NA","Chris","NA","NA","NA","NA","Alia","NA","NA","NA","NA","NA","NA"), natural.death = c(0,NA,NA,NA,0,NA,1,NA,0,NA,NA,NA,NA,1,NA,NA,NA,NA,NA,NA),gdp.growth.rate=c(1:20),
+                    id1=c(0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0),
+                  id2=c(0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0),
+                  PRE5_id1=c(0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
+                  PRE5_id2=c(0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0),
+                  POST5_id1=c(0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0),
+                  POST5_id2=c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0))
> df
   country year leader natural.death gdp.growth.rate id1 id2 PRE5_id1 PRE5_id2 POST5_id1 POST5_id2
1   Angola 1940  David             0               1   0   0        0        0        0        0
2   Angola 1941     NA            NA               2   0   0        1        0        0        0
3   Angola 1942     NA            NA               3   0   0        1        0        0        0
4   Angola 1943     NA            NA               4   0   0        1        0        0        0
5   Angola 1944  Henry             0               5   0   0        1        0        0        0
6   Angola 1945     NA            NA               6   0   0        1        0        0        0
7   Angola 1946    Tom             1               7   1   0        0        0        0        0
8   Angola 1947     NA            NA               8   0   0        0        0        1        0
9   Angola 1948  Chris             0               9   0   0        0        1        1        0
10  Angola 1949     NA            NA              10   0   0        0        1        1        0
11  Angola 1950     NA            NA              11   0   0        0        1        1        0
12  Angola 1951     NA            NA              12   0   0        0        1        1        0
13  Angola 1952     NA            NA              13   0   0        0        1        0        0
14  Angola 1953   Alia             1              14   0   1        0        0        0        0
15  Angola 1954     NA            NA              15   0   0        0        0        0        1
16  Angola 1955     NA            NA              16   0   0        0        0        0        1
17  Angola 1956     NA            NA              17   0   0        0        0        0        1
18  Angola 1957     NA            NA              18   0   0        0        0        0        1
19  Angola 1958     NA            NA              19   0   0        0        0        0        1
20  Angola 1959     NA            NA              20   0   0        0        0        0        0

Any help will be appreciated. Thanks!

After trying one of the answers from below and modified the original df to the following (see below), I get the following output.df (see below):

> df <- data.frame(country=c("Angola","Angola","Angola","Angola",
+                            "Angola","Angola","Angola","Angola",
+                            "Angola","Angola","US","US","US","US",
+                            "US","US","US","US","US","US"), 
+                  year=c(1940:1949,1940:1949), 
+                  leader = c("David", "NA", "NA", "NA","Henry","NA",
+                             "Tom","NA","Chris","NA","NA","NA","NA",
+                             "Alia","NA","NA","NA","NA","NA","NA"), 
+                  natural.death = c(0,NA,NA,NA,0,NA,1,NA,0,NA,NA,NA,NA,1,NA,NA,NA,NA,NA,NA),gdp.growth.rate=c(1:20),
+                    id1=c(0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0),
+                  id2=c(0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0))

> output.df
          country year leader natural.death gdp.growth.rate id1 id2 id1.PRE
Angola.1   Angola 1940  David             0               1   0   0       0
Angola.2   Angola 1941     NA            NA               2   0   0       1
Angola.3   Angola 1942     NA            NA               3   0   0       1
Angola.4   Angola 1943     NA            NA               4   0   0       1
Angola.5   Angola 1944  Henry             0               5   0   0       1
Angola.6   Angola 1945     NA            NA               6   0   0       1
Angola.7   Angola 1946    Tom             1               7   1   0       0
Angola.8   Angola 1947     NA            NA               8   0   0       0
Angola.9   Angola 1948  Chris             0               9   0   0       0
Angola.10  Angola 1949     NA            NA              10   0   0       0
US.1           US 1940     NA            NA              11   0   0       0
US.2           US 1941     NA            NA              12   0   0       0
US.3           US 1942     NA            NA              13   0   0       0
US.4           US 1943   Alia             1              14   0   1       0
US.5           US 1944     NA            NA              15   0   0       0
US.6           US 1945     NA            NA              16   0   0       0
US.7           US 1946     NA            NA              17   0   0       0
US.8           US 1947     NA            NA              18   0   0       0
US.9           US 1948     NA            NA              19   0   0       0
US.10          US 1949     NA            NA              20   0   0       0
          id1.POST id2.PRE id2.POST
Angola.1         0       0        0
Angola.2         0       0        1
Angola.3         0       0        1
Angola.4         0       0        1
Angola.5         0       0        1
Angola.6         0       0        1
Angola.7         0       0        0
Angola.8         1       0        0
Angola.9         1       0        0
Angola.10        1       0        0
US.1             0       1        0
US.2             1       1        0
US.3             1       1        0
US.4             1       0        0
US.5             1       0        1
US.6             1       0        1
US.7             0       0        1
US.8             0       0        1
US.9             0       0        1
US.10            0       0        0

回答1:


One approach using base R. We create a function generate_dummy which returns two columns for each "id" column with PRE and POST data.

generate_dummy <- function(x) {
   inds <- which(x == 1)
   if(length(inds) == 1) {
     vec <- seq_along(x)
     data.frame(PRE = +(vec > (inds - 6) & vec < (inds)),
               POST = +(vec > (inds) & vec < (inds + 6)))
     }
     else  data.frame(PRE = rep(0, length(x)),POST = rep(0, length(x)))
}


#Columns which start with id
cols <- grep("^id", names(df), value = TRUE)

To apply it for each country we split the data by country and apply generate_dummy function to each one of them and combine the results.

output <- cbind(df, do.call(rbind, lapply(split(df, df$country), function(x) 
                       do.call(cbind, lapply(x[cols], generate_dummy)))))
row.names(output) <- NULL  

output
#   country year leader natural.death gdp.growth.rate id1 id2 id1.PRE id1.POST id2.PRE id2.POST
#1   Angola 1940  David             0               1   0   0       0        0       0        0
#2   Angola 1941     NA            NA               2   0   0       1        0       0        0
#3   Angola 1942     NA            NA               3   0   0       1        0       0        0
#4   Angola 1943     NA            NA               4   0   0       1        0       0        0
#5   Angola 1944  Henry             0               5   0   0       1        0       0        0
#6   Angola 1945     NA            NA               6   0   0       1        0       0        0
#7   Angola 1946    Tom             1               7   1   0       0        0       0        0
#8   Angola 1947     NA            NA               8   0   0       0        1       0        0
#9   Angola 1948  Chris             0               9   0   0       0        1       1        0
#10  Angola 1949     NA            NA              10   0   0       0        1       1        0
#11  Angola 1950     NA            NA              11   0   0       0        1       1        0
#12  Angola 1951     NA            NA              12   0   0       0        1       1        0
#13  Angola 1952     NA            NA              13   0   0       0        0       1        0
#14  Angola 1953   Alia             1              14   0   1       0        0       0        0
#15  Angola 1954     NA            NA              15   0   0       0        0       0        1
#16  Angola 1955     NA            NA              16   0   0       0        0       0        1
#17  Angola 1956     NA            NA              17   0   0       0        0       0        1
#18  Angola 1957     NA            NA              18   0   0       0        0       0        1
#19  Angola 1958     NA            NA              19   0   0       0        0       0        1
#20  Angola 1959     NA            NA              20   0   0       0        0       0        0

data

df <- data.frame(country = rep("Angola",length(20)), year=c(1940:1959), 
       leader = c("David", "NA", "NA", "NA","Henry","NA","Tom","NA","Chris","NA",
       "NA","NA","NA","Alia","NA","NA","NA","NA","NA","NA"), 
      natural.death = c(0,NA,NA,NA,0,NA,1,NA,0,NA,NA,NA,NA,1,NA,NA,NA,NA,NA,NA),
      gdp.growth.rate=c(1:20),
      id1=c(0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0),
      id2=c(0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0))


来源:https://stackoverflow.com/questions/59150321/create-lead-and-lag-year-dummies-for-regression-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!