R语言报错求助:Lapack routine dgesv系统精确奇异U[10,10]=0
Troubleshooting the "Lapack routine dgesv: system is exactly singular" Error in
pgmm() Hey there, let's break down why you're hitting this error and walk through practical fixes to get your GMM estimation working.
Why This Error Happens
This error means the matrix the estimation routine is trying to invert is singular (it has no inverse), which usually stems from one or more of these issues with your setup:
- Too few cross-sectional units: You only have 4 countries, which is a very small sample for GMM. GMM relies on having enough variation across units to estimate the moment conditions, and small N can lead to underdetermined systems where the matrix can't be inverted.
- Overloaded set of instruments/regressors: Your model includes a lot of terms (lags of
log(y2),EU_IM,Non_EU_IM, pluslog(GDP)), and thegmm.instargument adds even more instruments from lags oflog(y2). When the number of instruments or regressors approaches (or exceeds) the effective number of observations, the matrix becomes singular. - Perfect multicollinearity: Some of your regressors might be perfectly linearly related. For example, maybe
EU_IMandNon_EU_IM(or their lags) are perfectly correlated, or a lagged term is a linear combination of other variables in the model. - Zero-variance variables: If any variable in your model has no variation (e.g., one country's
EU_IMis the same every quarter), this will also cause the matrix to lose rank and become singular.
Fixes to Try
Here are targeted steps to resolve the issue:
- Trim your instrument set: Reduce the number of instruments by tightening the
lag.gmmspecification. Instead of using lags 2 and 3 oflog(y2)as instruments, try a narrower range likelag.gmm = list(c(2,2))(only the 2nd lag). For small N, fewer instruments mean a more stable matrix. - Simplify your model: Drop regressors that aren't theoretically necessary or show low explanatory power. Start with a parsimonious version (e.g., only include 1 lag of
log(y2), currentEU_IM/Non_EU_IM, andlog(GDP)), then add terms one by one to see which is causing the singularity. - Check for multicollinearity: Run
cor(df[, c("log(y2)", "EU_IM", "Non_EU_IM", "log(GDP)")])to spot variables with near-perfect correlation (correlation coefficient close to ±1). If you find any, remove one of the correlated variables or combine them (e.g., total imports instead of splitting into EU/Non-EU). - Validate your data: Use
summary(df)to check for variables with zero variance. If you find any, either drop that variable or check if there's a data entry error causing the lack of variation. - Try the two-step GMM: Switch to
model = "twostep"instead of"onestep". The two-step routine uses a different weight matrix calculation that might be more robust to small samples (though you should still address instrument/regressor bloat first).
内容的提问来源于stack exchange,提问作者R.M
相关产品推荐
相关产品推荐

