使用felm() - 包(lfe)
我有10个变量,并且想要自己的和互动的效果。
然而,在构建可重现的数据框架时,我能够按如下方式运行脚本:
library(lfe)
## build data frame
set.seed(1)
data <- data.frame(
x1 = rnorm(1:10000),x2=rnorm(1:10000),x3=rnorm(1:10000),x4=rnorm(1:10000),x5=rnorm(1:10000),
z1 = rnorm(1:10000),z2=rnorm(1:10000),z3=rnorm(1:10000),z4=rnorm(1:10000),z5=rnorm(1:10000),
f1 = as.factor(sample(c("yes","no","maybe"),10000,replace=TRUE)),
f2 = as.factor(sample(c("high","med","low"),10000,replace=TRUE)),
y = rnorm(1:10000))
## build matrices
x <- select(data,starts_with("x"))
z <- select(data,starts_with("z"))
## run model
model <- felm(y ~ as.matrix(x)*as.matrix(z) | factor(f1)+factor(f2) | 0 | f1, data=data)
summary(model)
这很好地执行。但是,在我的实际数据中使用这个确切的结构,我返回错误:
[[<-.data.frame
中的错误(*tmp*
,i,值= c(1L,1L,1L,1L,1L,1L,: 替换有367060行,数据有73412
似乎是使用矩阵的产品:
n = 73,412; p = 5; np = 73,412x5 = 367,060
我的模特:
new <- select(snow,starts_with("new")) ##results in object 5x73,412
base <- select(snow,starts_with("base")) ##results in object 5x73,412
model <- felm(price ~ as.matrix(new)*as.matrix(base) | factor(ID) | 0 | ID, data=snow)
select()
产生的对象在所需尺寸(5 x 73,412)
答案 0 :(得分:1)
使用此结构,我发现工作的解决方案是data.matrix()
## build matrices
x <- select(data,starts_with("x"))
z <- select(data,starts_with("z"))
## run model using matrix notation
model <- felm(y ~ data.matrix(x)*data.matrix(z) | factor(f1)+factor(f2) | 0 | f1, data=data)