R 中的四部分公式语法

Four-part formula syntax in R

我在 R 中使用 lfe 软件包实现高维固定效果。我在没有协变量的情况下尝试 运行 时遇到了麻烦。也就是说,只有固定效果。我的代码是:

library(lfe)
data=read.csv("path_to//my_data.csv")

y <- cbind(col1)
x <- cbind(col2)

est <- felm(y ~ 0|x, data)

但是最后一行报错:

Error in model.frame.default(terms(formula, lhs = lhs, rhs = rhs, data = data,  : 
  variable lengths differ (found for 'x')

请注意,我根据四部分公式格式使用正确的语法调用它,如 documentation 第 20 页所示:

The formula specification is a response variable followed by a four part formula. The first part consists of ordinary covariates, the second part consists of factors to be projected out. The third part is an IV-specification. The fourth part is a cluster specification for the standard errors. I.e. something like y ~ x1 + x2 | f1 + f2 | (Q|W ~ x3+x4) | clu1 + clu2 where y is the response, x1,x2 are ordinary covariates, f1,f2 are factors to be projected out, Q and W are covariates which are instrumented by x3 and x4, and clu1,clu2 are factors to be used for computing cluster robust standard errors. Parts that are not used should be specified as 0, except if it’s at the end of the formula, where they can be omitted.

原来是语法问题,正如@lmo所说。如果我这样做:

est <- felm(col1 ~ 0|col1data)

然后就没有错误了,就可以了