r-spatial / spdep

Spatial Dependence: Weighting Schemes and Statistics
https://r-spatial.github.io/spdep/
121 stars 26 forks source link

Error when calculating impacts after spBreg_lag #18

Closed giacfalk closed 5 years ago

giacfalk commented 5 years ago

I am trying to calculate impacts after running

spbreg<-spBreg_lag(formula, data=data, listw=wm_reg1, type="Durbin")

But I get the following error message.

Error in impacts.MCMC_sar_g(spbreg, listw = wm_reg1, evalues = evalues, : non-matched coefficient pairs

What does it mean?

rsbivand commented 5 years ago

Do provide complete reproducible examples - nobody can tell what this means without a complete reproducible example. You may upload both code and data here, or just code and use a built-in data set. I have no idea what you are doing wrong based on the code you provided.

giacfalk commented 5 years ago

Ok, thanks and sorry for the incompleteness.

Please find enclosed a data file (including data, weights matrix and regression specification), and the code I am running.

spdep_issue.zip

rsbivand commented 5 years ago

Your formula includes country, but country is not in data:

> spbreg<-spBreg_lag(formula, data=data, listw=wm_reg1, type="Durbin")
Error in is.factor(x) : object 'country' not found
> formula
[1] "lightint~as.factor(ghs_cat)*as.numeric(altitude)+as.factor(ghs_cat)*as.numeric(tri)+as.factor(ghs_cat)*as.numeric(traveltime)+as.factor(ghs_cat)*as.numeric(shoredistt)+as.factor(ghs_cat)*as.numeric(roadistanc)+as.factor(ghs_cat)*as.numeric(griddist)+as.factor(ghs_cat)*as.numeric(subdist)+as.factor(ghs_cat)*as.numeric(pop)+as.numeric(y)+as.numeric(x)+as.factor(country)"
> names(data)
 [1] "altitude"   "distance2w" "tri"        "traveltime" "crops"     
 [6] "shoredistt" "roadistanc" "griddist"   "subdist"    "GID_0"     
[11] "GID_1"      "pop"        "lightint"   "ghs_cat"    "X"         
[16] "Y"          "Country"    "ID"         "X_ID"       "X_CX"      
[21] "X_CY"       "X_est_est1" "X_est_est2"

Using as.factor and as.numeric in the formula is not good practice at all.

giacfalk commented 5 years ago

I had deleted and reposted the attatchement in the time span of 5 minutes, I guess you downloaded it exactly at that time. Please check the new version.

rsbivand commented 5 years ago

The new version had exactly the same issues (capitals in data, lower case in formula). Now I have data with 105660 rows, but the weights objects have 105658. Please try your provided data in a new R session locally before posting.

rsbivand commented 5 years ago

The underlying reason for your issues is that lag.as.factor(country)MDG Madagascar was aliased and dropped from the regression, so as.factor(country)MDG did not have a coefficient pair. as.factor(country)MDG takes 1 for all observations on Madagaskar, so do their spatially lagged values, hence the aliasing.

giacfalk commented 5 years ago

The underlying reason for your issues is that lag.as.factor(country)MDG Madagascar was aliased and dropped from the regression, so as.factor(country)MDG did not have a coefficient pair. as.factor(country)MDG takes 1 for all observations on Madagaskar, so do their spatially lagged values, hence the aliasing.

Thanks. Is thus possible to control for country fixed-effects and calculating impacts in some other way in the specific spBreg_lag regression context?

rsbivand commented 5 years ago

You are not using simply a spatial lag model, you are including spatial lags of all the X variables, including your "fixed effects" - which largely destroy the logic of spatial regression anyway. Do you need to do that? Do you really need to include Wy in the regression - does it make sense? Do the observed agents influence each other simultaneously like prices in a market?

rsbivand commented 5 years ago

The development version of spdep on github now supports a formula Durbin argument in all spatial econometrics model fitting functions. My guess is that an SDEM or SLX model is appropriate, not SDM, and without Wx of country dummies in any case (best without country dummies anyway, possibly with a country iid random effect).