You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从面板模型的coeftest()中获取完整聚类调整协方差矩阵

Extract Cluster-Adjusted Covariance Matrix from coeftest for plm Models

First, let's break down what's happening in your workflow: when you pass vcov = vcovHC(panelmodel, type = "sss") and cluster = "id" to coeftest, the function takes the heteroskedasticity-robust HC-SSS matrix and adjusts it for clustering on your id variable. To get this final cluster-adjusted covariance matrix for your后续 calculations, you have two simple, reliable options:

Option 1: Extract directly from the coeftest result

The coeftest object stores the exact covariance matrix it uses to compute adjusted standard errors. You can pull it out easily using the vcov() function after saving the coeftest output:

# Load required packages
library("plm")
library("lmtest")

# Your original model setup
Data <- data.frame(id = c(rep("a",50), rep("b", 50)), y = rnorm(100), x = c(rnorm(50), rnorm(50, sd = 5)), z = c(rnorm(50), rnorm(50, sd = 3)))
panelmodel <- plm(y ~ x + z, data = Data, effect = "individual", model = "within", index = "id")

# Run coeftest and save the result
ct_result <- coeftest(panelmodel, vcov = vcovHC(panelmodel, type = "sss"), cluster = "id")

# Extract the cluster-adjusted covariance matrix
clustered_hcov_matrix <- vcov(ct_result)

# Inspect the matrix
print(clustered_hcov_matrix)

Option 2: Generate the matrix directly with vcovHC (no coeftest needed)

You don’t have to go through coeftest to create this matrix. The vcovHC() function in plm has a built-in cluster parameter that lets you specify clustering on individual groups (your id variable). This generates the same cluster-adjusted HC-SSS covariance matrix directly:

# Generate the cluster-adjusted HC-SSS matrix directly
clustered_hcov_matrix <- vcovHC(panelmodel, type = "sss", cluster = "group")

# Note: "group" refers to the individual effect dimension (your `id` variable)
# You can also explicitly use `cluster = "id"` for clarity if preferred

This works because cluster = "group" targets the individual/group level of your panel (matching your effect = "individual" specification). The output will be identical to the matrix used in your coeftest call.

Verify consistency

To confirm both methods produce the same matrix, run this check:

all.equal(vcov(ct_result), vcovHC(panelmodel, type = "sss", cluster = "group"))

It should return TRUE, confirming the matrices are identical.


内容的提问来源于stack exchange,提问作者Jakob

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 13:37:38