rcrannon-linear-regressionr-zeligsystemfit

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.

I tried using systemfit and its wrapper Zelig. But I am not able to understand how to specify factors to be projected out (i.e., add fixed effects) and cluster the standard errors, like we do in 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.

If we use felm(), the specification is

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.

Any help would be much appreciated. Thank You!


Solution

  • I think now I get it how to implement Fixed Effect correctly to SUR Model,

    1. 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

      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

    2. Check Closely the value of sample_data2$X_var13, and sample_data2$X_var23

      sample_data2$X_var13

      [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

    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

    1. 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)

    2. Fit the SUR Model and make summary:

      fitsur <- systemfit(formula = formula, data=sample_data2, method = "SUR")

      summary(fitsur)