我想计算回归残差的聚类误差。我知道如何在R中运行随机效应模型。
set.seed(50)
data <- data.frame(id=rep(1:10,10),income=rnorm(100),education=rnorm(100))
data$id <- factor(data$id)
library(nlme)
reg <- lme(income ~ education -1,data = data, random = ~ 1 | id)
我不确定如何将标准错误聚集在id级别。
答案 0 :(得分:0)
您可以使用plm
包。详见here。但这里是如何指定随机效应模型:
set.seed(50)
data <- data.frame(id=rep(1:10,10),income=rnorm(100),education=rnorm(100))
data$id <- factor(data$id)
library(plm)
reg <- plm(income ~ education,
data= data,
index = c('id'),
effect = "individual",
model ='random')
summary(reg)
Oneway (individual) effect Random Effect Model
(Swamy-Arora's transformation)
Call:
plm(formula = income ~ education, data = data, effect = "individual",
model = "random", index = c("id"))
Balanced Panel: n = 10, T = 10, N = 100
Effects:
var std.dev share
idiosyncratic 0.98161 0.99076 0.973
individual 0.02766 0.16630 0.027
theta: 0.1167
Residuals:
Min. 1st Qu. Median 3rd Qu. Max.
-2.777437 -0.626498 0.082894 0.602743 2.732686
Coefficients:
Estimate Std. Error t-value Pr(>|t|)
(Intercept) -0.116959 0.112068 -1.0436 0.2992
education -0.033914 0.101471 -0.3342 0.7389
Total Sum of Squares: 95.329
Residual Sum of Squares: 95.221
R-Squared: 0.0011386
Adj. R-Squared: -0.0090539
F-statistic: 0.111707 on 1 and 98 DF, p-value: 0.73892
并获得聚类标准错误:
coeftest(reg, vcov=vcovHC(reg,type="HC0",cluster="group"))