r语言 - 通过 id 创建滞后变量时,无法将类型'closure'强制到类型列表的向量



我想使用我在这个平台上找到的代码创建RD_expenses的滞后变量:

library(data.table)
TandCtable[, RDexp_pre1:=c(NA, "RD_expenses"[-.N]), by=id]

然而,当我运行代码时,我得到了这个错误:

Error in as.vector(x, "list") : cannot coerce type 'closure' to vector of type 'list'

它可能与数据类型有关,但我不知道是什么。

我需要将滞后变量作为协变量添加到我的固定效应模型中:

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])

非常感谢您的帮助!

编辑:对不起,这是数据表!

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>)

不要在列名周围使用引号。

library(data.table)
TandCtable[, RDexp_pre1 := c(NA, RD_expenses[-.N]), by=id]

如果您想要不同偏移长度的滞后,最好使用@Waldi建议的shift,而不是附加NA的s。

TandCtable[, RDexp_pre1 := shift(RD_expenses, 2), by=id]

相关内容

最新更新