[英]How to estimate an SUR model in R with factors to be projected out and clustered standard errors?
I want to estimate an SUR (Seemingly Unrelated Regressions) model.我想估计一个 SUR(看似无关的回归)模型。
I tried using systemfit
and its wrapper Zelig
.我尝试使用systemfit
及其包装器Zelig
。 But I am not able to understand how to specify factors to be projected out (ie, add fixed effects) and cluster the standard errors, like we do in felm()
.但我无法理解如何指定要投影的因子(即添加固定效应)并像我们在felm()
所做的那样对标准误差进行聚类。
Also, if I simply add the fixed effect variables to my regression equations, then I get the following error:此外,如果我只是将固定效应变量添加到我的回归方程中,则会出现以下错误:
Error in LU.dgC(a) : cs_lu(A) failed: near-singular A (or out of memory)
Thank you so much for your help!非常感谢你的帮助!
I am adding a data sample from my data:我正在从我的数据中添加一个数据样本:
Y_var1 <- c(0.45, 0.40, 0.30, 0.40, 0.15, 0.35, 0.50, 0.55, 0.10, 0.15, 0.30, 0.10)
Y_var2 <- c(0.40, 0.25, 0.45, 0.30, 0.35, 0.25, 0.15, 0.25, 0.35, 0.30, 0.20, 0.15)
X_var1 <- c(0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0)
X_var2 <- c(0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0)
X_var3 <- c(0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1)
X_var4 <- c(0.18, 0.18, 0.18, 0.20, 0.20, 0.20, 0.22, 0.22, 0.22, 0.24, 0.24, 0.24)
X_var5 <- c(0.08, 0.08, 0.08, 0.06, 0.06, 0.06, 0.04, 0.04, 0.04, 0.02, 0.02, 0.02)
X_var6 <- c(-0.25, -0.25, -0.25, 1.30, 1.30, 1.30, 1.80, 1.80, 1.80, 2.25, 2.25, 2.25)
X_var7 <- c(1000, 1000, 1000, 1500, 1500, 1500, 2000, 2000, 2000, 2500, 2500, 2500)
X_var8 <- c('ABC', 'ABC', 'ABC', 'MNO', 'MNO', 'MNO', 'DEF', 'DEF', 'DEF', 'XYZ', 'XYZ', 'XYZ')
X_var9 <- c(2000, 2010, 2020, 2000, 2010, 2020, 2000, 2010, 2020, 2000, 2010, 2020)
sample_data <- data.frame(Y_var1, Y_var2, X_var1, X_var2, X_var3, X_var4, X_var5, X_var6, X_var7, X_var8, X_var9)
library(systemfit)
formula <- list(mu1 = Y_var1 ~ X_var1*X_var3 + X_var2*X_var3 + X_var4 + X_var5 + X_var6 + log(X_var7),
mu2 = Y_var2 ~ X_var1*X_var3 + X_var2*X_var3 + X_var4 + X_var5 + X_var6 + log(X_var7))
fitsur <- systemfit(formula = formula, data=sample_data, method = "SUR")
fitols <- systemfit(formula = formula, data=sample_data, method = "OLS")
(Since this is a sample dataset, thus, the above two regressions will give an error I have mentioned above, but are working fine on my actual data.) (由于这是一个示例数据集,因此,上述两个回归将给出我上面提到的错误,但在我的实际数据上运行良好。)
However, what I am interested in is estimating the above formula using SUR, with X_var8
and X_var9
fixed effects and standard errors clustered at X_var8
level.但是,我感兴趣的是使用 SUR 估计上述公式,其中X_var8
和X_var9
固定效应和标准误差聚集在X_var8
级别。
If we use felm()
, the specification is如果我们使用felm()
,则规范是
felm(mu1 = Y_var1 ~ X_var1*X_var3 + X_var2*X_var3 + X_var4 + X_var5 + X_var6 + log(X_var7) | X_var8 + X_var9 | 0 | X_var8)
However, as my standard errors are correlated across equations, I need to use SUR.但是,由于我的标准误差与方程相关,因此我需要使用 SUR。
Any help would be much appreciated.任何帮助将非常感激。 Thank You!谢谢!
I think now I get it how to implement Fixed Effect correctly to SUR Model,我想现在我明白了如何正确地对 SUR 模型实施固定效果,
we need to transform the X_var8 to numeric first with one hot encoding , and also I make new variable based by your interaction formula above我们需要首先使用一个热编码将X_var8转换为数字,并且我还根据您上面的交互公式创建了新变量
library(mltools)
sample_data2 <- as.data.frame(one_hot(as.data.table(sample_data)))
sample_data2$X_var13 <- sample_data2$X_var1 * sample_data2$X_var3
sample_data2$X_var23 <- sample_data2$X_var2 * sample_data2$X_var3
Check Closely the value of sample_data2$X_var13, and sample_data2$X_var23仔细检查sample_data2$X_var13和sample_data2$X_var23的值
sample_data2$X_var13
[1] 0 0 0 0 0 0 0 0 0 0 0 0 [1] 0 0 0 0 0 0 0 0 0 0 0 0
sample_data2$X_var23
[1] 0 0 0 0 0 1 0 0 0 0 0 0 [1] 0 0 0 0 0 1 0 0 0 0 0 0
Since for the desired sample data all sample_data2$X_var13
is 0, it will also effecting an error of Error in LU.dgC(a) : cs_lu(A) failed: near-singular A (or out of memory)
since it doesn't have any meaningful value, we can discard it, but feel free to use it to real data由于对于所需的样本数据,所有sample_data2$X_var13
都是 0,因此它还会影响Error in LU.dgC(a) : cs_lu(A) failed: near-singular A (or out of memory)
的Error in LU.dgC(a) : cs_lu(A) failed: near-singular A (or out of memory)
因为它没有有任何有意义的价值,我们可以丢弃它,但可以随意将其用于真实数据
Make Formula with added fixed effects:使用添加的固定效果制作公式:
formula <- list(mu1 = Y_var1 ~ X_var23 + X_var4 + X_var5 + X_var6 + log(X_var7) + X_var8_ABC + X_var8_DEF + X_var8_MNO + X_var8_XYZ + X_var9, mu2 = Y_var2 ~ X_var23 + X_var4 + X_var5 + X_var6 + log(X_var7) + X_var8_ABC + X_var8_DEF + X_var8_MNO + X_var8_XYZ + X_var9)
Fit the SUR Model and make summary:拟合 SUR 模型并进行总结:
fitsur <- systemfit(formula = formula, data=sample_data2, method = "SUR")
summary(fitsur)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.