我试图在函数中使用任意长度的向量作为参数,该函数使用data.table语法。向量包含要用作回归的列的名称。
data <- data.table(id = c(1,2,3,1,2,3), prd = c(1,1,1,2,2,2), y=rnorm(6), int=1, x1=rnorm(6), x2=rnorm(6))
regressors <- c("x1", "x2")
regressors1 <- c("int", "x1", "x2")
> data
id prd y int x1 x2
1: 1 1 -0.04855 1 -1.27990 0.46696
2: 2 1 -0.39557 1 0.07829 -0.96699
3: 3 1 -0.23865 1 -0.39837 -1.12087
4: 1 2 -0.16012 1 -0.12919 0.22381
5: 2 2 0.21071 1 0.40768 -0.02047
6: 3 2 -0.55270 1 -0.87323 2.43326
所需的输出是
output <- data[, as.list(lm.fit(cbind(int, eval(as.name(regressors[1])), eval(as.name(regressors[2]))), data.matrix(y))$coefficients), by = prd]
> output
prd int
1: 1 -0.42284 -0.3119 -0.05346
2: 2 -0.08158 0.7202 0.06486
我使用lm.fit()而不是lm()来加快计算速度。 我想直接在函数中使用向量“回归变量”,使其具有任意长度。因此,我想要类似
output <- data[, as.list(lm.fit(cbind(lapply(regressors1, eval(as.name))), data.matrix(y))$coefficients), by = prd]
, 但它不起作用。在Applying function row-wise in a data.table; passing column names as a vector之后,我尝试
失败。output <- data[, as.list(lm.fit(cbind(unlist(mget(regressors1))), data.matrix(y))$coefficients), by = prd]