Skip to content
Prev 26907 / 29559 Next

spgm

On Wed, 3 Oct 2018, Roger Bivand wrote:

            
My cut at an example:

library(splm)
data(Produc, package="plm")
data(usaww)
matrix1 <- kronecker(diag(length(unique(Produc$year))), usaww)
listw1 <- mat2listw(matrix1, style="W")
tr <- trW(as(listw1, "CsparseMatrix"), m=100)
GM <- spgm(fm, data=Produc, listw = usaww, moments="within", spatial.error 
= FALSE, lag = TRUE)
impacts(GM, listw=listw1)
impacts(GM, tr=tr)
GMi <- spgm(update(fm, . ~ . - unemp), data=Produc, listw = usaww, 
moments="within", spatial.error = FALSE, lag = TRUE, endog = ~ unemp, 
instruments = ~ hwy + water + util)
summary(GMi)
GMii <- spgm(update(fm, . ~ . - unemp - log(emp)), data=Produc, listw = 
usaww, moments="within", spatial.error = FALSE, lag = TRUE, endog = ~ 
unemp + log(emp), instruments = ~ hwy + water + util)
summary(GMii)

The summary objects show the coefficients, etc. for endogeneous variables 
first, before the spatial coefficient. spdep::impacts.stsls() expects the 
spatial coefficient listed before the variables (dropping the intercept). 
If you use:

put_endog_last <- function(stsls) {
   if (is.null(stsls$endog)) stop("no endogenous variables in fitted 
model")
   n_endog <- length(all.vars(stsls$endog))
   coefs <- stsls$coefficients
   n_coefs <- length(coefs)
   flip <- c((n_endog+1):n_coefs, 1:n_endog)
   stsls$coefficients <- coefs[flip]
   stsls$var <- stsls$var[flip, flip]
   stsls
}

to flip the orderings in the splm::spgm() output object, impacts can be 
calculated using the spdep::impacts.stsls() method. I've added the splm 
maintainer to CC - I'm unsure whether other functions in splm (or other 
argument settings when lag = TRUE) would be affected by patching 
splm::spgm() itself.

Hope this helps,

Roger