spgm
On Wed, 3 Oct 2018, Roger Bivand wrote:
Please provide a reproducible example using the Produc dataset, so that it is clearer what the problem is. I do not know that there is any literature on such impacts, please provide references.
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
Roger
Roger Bivand
Norwegian School of Economics
Bergen, Norway
On Wed, Oct 3, 2018 at 11:21 AM +0200, "Hul?nyi Martin" <martin.hulenyi at vlada.gov.sk<mailto:martin.hulenyi at vlada.gov.sk>> wrote:
Thank you very much !
I have one more question regarding the output. I have also one endogenous variable in the model. Your code worked, but it did not show me the indirect and direct effects for the
endogenous varibale. Here is my regex:
spd_01 <- spgm(gdppcgr~lefpayr+lpopgr+linvr+lagwgipca + laglgdppc,
data=esifpdata, listw=dm1.lw,
model="within", lag=TRUE, spatial.error= FALSE, endog = ~ lefpayr,
instruments=~areaprop,
method="w2sls")
matrix1 <- kronecker(diag(length(unique(esifpdata$years))), dm1)
listw1 <- mat2listw(matrix1, style="W")
tr <- trW(as(listw1, "CsparseMatrix"), m=100)
impacts(spd_01, listw=listw1)
impacts(spd_01, tr=tr)
summary(impacts(spd_01, tr=tr, R=1000), zstats=TRUE, short=TRUE)
Best,
MArtin Hul?nyi
________________________________________
Od: Roger Bivand
Odoslan?: 29. septembra 2018 14:52
Komu: Hul?nyi Martin
K?pia: r-sig-geo at r-project.org
Predmet: Re: [R-sig-Geo] spgm
On Sat, 29 Sep 2018, Hul?nyi Martin wrote:
Dear all,
I would like to ask if there is a possibility to apply something
similiar to the "impacts" from spdep package for SAR regressions using
the spgm function from the splm package.
A reprex would have helped. Here is mine:
data(Produc, package = "plm")
data(usaww) # dense row-standardised weights matrix
GM <- spgm(log(gsp)~log(pcap)+log(pc)+log(emp)+unemp, data=Produc,
listw = usaww, moments="fullweights", lag=TRUE, spatial.error = FALSE)
class(GM)
?impacts.stsls # spdep method for stsls objects
head(Produc)
length(unique(Produc$year)) # T
big <- kronecker(diag(length(unique(Produc$year))), usaww)
listw <- mat2listw(big, style="W")
tr <- trW(as(listw, "CsparseMatrix"), m=100)
impacts(GM, listw=listw)
impacts(GM, tr=tr)
summary(impacts(GM, tr=tr, R=1000), zstats=TRUE, short=TRUE)
The splm:::impacts.splm() method cannot dispatch on stsls objects, so they
try to use the spdep:::impacts.stsls() method, but there the data rows are
n x T but listw is only of n rows. Looking inside splm:::impacts.splm(),
you see that a sparse kronecker product matrix is constructed - either do
the same if your n x T is large, or follow the above using a dense
kronecker product and cast back to listw representation to create the
trace vector.
Hope this clarifies,
Roger
Best regards,
Martin Hul???nyi ?
[eco.jpg] Pred vytla???en???m tohto mailu zv???te pros???m vplyv na ???ivotn??? prostredie. ???akujeme.
Please consider the environment before printing this e-mail. Thanks
[[alternative HTML version deleted]]
--
Roger Bivand
Department of Economics, Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; e-mail: Roger.Bivand at nhh.no
http://orcid.org/0000-0003-2392-6140
https://scholar.google.no/citations?user=AWeghB0AAAAJ&hl=en
[eco.jpg] Pred vytla?en?m tohto mailu zv??te pros?m vplyv na ?ivotn? prostredie. ?akujeme.
Please consider the environment before printing this e-mail. Thanks
[[alternative HTML version deleted]]
_______________________________________________
R-sig-Geo mailing list
R-sig-Geo at r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Roger Bivand Department of Economics, Norwegian School of Economics, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; e-mail: Roger.Bivand at nhh.no http://orcid.org/0000-0003-2392-6140 https://scholar.google.no/citations?user=AWeghB0AAAAJ&hl=en