I want to create a lag variable of RD_expenses
by using this code I found on this platform:
library(data.table)
TandCtable[, RDexp_pre1:=c(NA, "RD_expenses"[-.N]), by=id]
However, when i run the code i get this error:
Error in as.vector(x, "list") : cannot coerce type 'closure' to vector of type 'list'
It probably has to do something with the data types, but I can't figure out what.
I need the lag variable to add as a covariate to my fixed effects model:
m5a <- felm(ihs(RD_expenses) ~ merger_it_pre1 + merger_it + merger_it_post1 + merger_it_post2 + merger_it_post3 + factor(year),
data = TandC,
subset = RD_expenses > pcts[1] & RD_expenses < pcts[2])
Help is much much much appreciated!
Edit: sorry, here is the data table!
structure(list(company = c("ALLERGAN PUBLIC LIMITED COMPANY",
"ALLERGAN PUBLIC LIMITED COMPANY", "ALLERGAN PUBLIC LIMITED COMPANY",
"ALLERGAN PUBLIC LIMITED COMPANY", "ALLERGAN PUBLIC LIMITED COMPANY",
"ALLERGAN PUBLIC LIMITED COMPANY", "ALLERGAN PUBLIC LIMITED COMPANY",
"ALPINE ELECTRONICS, INC.", "ALPINE ELECTRONICS, INC.", "ALPINE ELECTRONICS, INC.",
"ALPINE ELECTRONICS, INC.", "ALPINE ELECTRONICS, INC.", "ALPINE ELECTRONICS, INC.",
"ALPS ALPINE CO.,LTD.", "ALPS ALPINE CO.,LTD.", "ALPS ALPINE CO.,LTD.",
"ALPS ALPINE CO.,LTD.", "ALPS ALPINE CO.,LTD.", "ALPS ALPINE CO.,LTD.",
"ANICURA STOCKHOLMS REGIONDJURSJUKHUS AB"), year = c(2013L, 2014L,
2015L, 2016L, 2017L, 2018L, 2019L, 2015L, 2016L, 2017L, 2018L,
2019L, 2020L, 2015L, 2016L, 2017L, 2018L, 2019L, 2020L, 2015L
), case_code = c("M.8193", "M.8193", "M.8193", "M.8193", "M.8193",
"M.8193", "M.8193", "M.8833", "M.8833", "M.8833", "M.8833", "M.8833",
"M.8833", "M.8833", "M.8833", "M.8833", "M.8833", "M.8833", "M.8833",
"M.9019"), merger = c(2016, 2016, 2016, 2016, 2016, 2016, 2016,
2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
2018, 2018), acquirer = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L), .Label = c("0",
"1"), class = "factor"), country = c("IE", "IE", "IE", "IE",
"IE", "IE", "IE", "JP", "JP", "JP", "JP", "JP", "JP", "JP", "JP",
"JP", "JP", "JP", "JP", "SE"), industry_firm = c(2120, 2120,
2120, 2120, 2120, 2120, 2120, 6420, 6420, 6420, 6420, 6420, 6420,
2611, 2611, 2611, 2611, 2611, 2611, 7500), industry_firm_2 = c(21,
21, 21, 21, 21, 21, 21, 64, 64, 64, 64, 64, 64, 26, 26, 26, 26,
26, 26, 75), industry_merger = c(4646, 4646, 4646, 4646, 4646,
4646, 4646, 453, 453, 453, 453, 453, 453, 453, 453, 453, 453,
453, 453, 1092), industry_merger_2 = c(46, 46, 46, 46, 46, 46,
46, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 10), simpl_proc = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 1L), .Label = c("0", "1"), class = "factor"), commitments = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L), .Label = c("0", "1"), class = "factor"), cross_border = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L), .Label = c("0", "1"), class = "factor"), duration = c(25,
25, 25, 25, 25, 25, 25, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
29, 29, 149), employees = c(384, 432, 624, 334, 356, 338, 348,
1122, 1110, 1492, 1317, NA, NA, 39588, 42053, 42289, 41840, 40443,
NA, 285), profit_margin = c(-0.14684, -0.85298, -0.58792, -0.38872,
-1.30312, -0.86866, -0.6363, -1.925, 0.567, 3.984, 4.01, NA,
NA, 8.763, 6.572, 7.599, 4.838, 1.921, NA, 8.049), RD_expenses = c(8946.414554,
9977.75638, 43326.90616, 48870.14658, 35022.10866, 39584.25952,
32259.2173, 6303.95, 6812.46, 14993.39, 21925.5, NA, NA, 259971.9513,
269457.2012, 227757.0765, 263893.2745, 316285.6753, NA, 7053.5
), sales = c(125844.3945, 111010.5704, 276862.329, 276455.8596,
265833.4972, 275762.3064, 286432.2966, 1260609.732, 1217111.106,
1094409.478, 2057945, NA, NA, 6036362.168, 6288047.037, 6560212.444,
6831502.438, 6806267.552, NA, 27113.77546), period = structure(c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L,
5L, 6L, 1L), .Label = c("-3", "-2", "-1", "0", "1", "2", "3"), class = "factor"),
merger_it = c(0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
0, 1, 0, 0, 0), merger_it_pre1 = c(0, 0, 1, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0), merger_it_post3 = c(0,
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
merger_it_post2 = c(0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 0, 0, 1, 0), merger_it_post1 = c(0, 0, 0, 0, 1,
0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0), RD_intensity = c(7.10910850621956,
8.98811378416267, 15.6492601635234, 17.6773777378817, 13.1744528168514,
14.3544852219875, 11.2624231565094, 0.500071500320608, 0.559723756230354,
1.36999818636439, 1.06540748173542, NA, NA, 4.30676530109749,
4.28522877794115, 3.47179422075435, 3.86288780389073, 4.6469768178164,
NA, 26.0144516222235), age = c(36, 36, 36, 36, 36, 36, 36,
54, 54, 54, 54, 54, 54, 73, 73, 73, 73, 73, 73, 19), industry = c(46,
46, 46, 46, 46, 46, 46, 45, 45, 45, 45, 45, 45, 45, 45, 45,
45, 45, 45, 10), treated = c("1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1"), id = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L)), row.names = c(NA,
-20L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x000001e028101ef0>)
Don't use quotes around the column name.
library(data.table)
TandCtable[, RDexp_pre1 := c(NA, RD_expenses[-.N]), by=id]
If you want lag of different offset length it is better to use shift
as suggested by @Waldi instead of appending NA
's.
TandCtable[, RDexp_pre1 := shift(RD_expenses, 2), by=id]