[R-sig-Geo] How to build a spatial weights list for pooled data
Roger Bivand
Roger.Bivand at nhh.no
Tue Oct 19 19:03:16 CEST 2010
On Tue, 19 Oct 2010, Millo Giovanni wrote:
>
> Dear Roger, dear list:
>
> regarding the simplest possible interpretation of Angela's question
> below (Roger's first guess in the original email): making the proximity
> matrix for a pooled model in "stacked cross-sections" notation, as (I_T
> kronecker W), where W is the original, cross-sectional proximity matrix,
> in order to
> - estimate the pooled model by lagsarlm() or errorsarlm()
> - perform Moran's tests etc. on the pooled OLS model,
> I'd like to add some considerations and ask for your insights. I
> apologize in advance if I'm going out of topic.
>
> It is fairly straightforward to transform W in 'listw' form to 'matrix',
> do a kronecker product and retransform it back, like this:
>
> library(spdep)
> ## Columbus data, as in example(errorsarlm)
> data(oldcol)
> COL.listw <- nb2listw(COL.nb, style="W")
> ## transform into 'matrix'
> COL.mat <- listw2mat(COL.listw)
> ## make pooled W, e.g. T=3
> COL.mat.p3 <- kronecker(diag(1,3), COL.mat)
> ## back to 'listw'
> COL.listw.p3 <- mat2listw(COL.mat.p3)
>
> so that a simple function that does the job might be:
>
>
> poolW <- function(w, t) {
> return(mat2listw(kronecker(diag(1,t), listw2mat(w))))
> }
>
> The problem is, the 'style' of the 'listw' object is changed from "W" to
> generic "M", as can readily be seen:
>
> COL.listw$style ## this is "W"
> COL.listw.p3$style ## ...and this is the generic style "M"
>
> This change in the 'style' has consequences when trying to use the
> advanced/fast options for the calculation of the Jacobian's determinant
> (method="spam" or method="Matrix"), which do work only with 'listw's of
> style "B", "C", "W" and "S". For this reason, it is for example not
> possible to do
>
> errorsarlm(formula, data, listw=COL.listw.p3, method="spam") ## not run
> (*)
>
> without getting a (rather verbose) error because of the listw being
> transformed into an inappropriate kind of matrix.
>
> Yet the weights obtained by the above procedure **seem** to be
> consistent with the "W" style as well, despite 'mat2listw' not knowing
> what happened upstream and thus assigning the most general "M" style.
mat2listw() can be passed an arbitrary square matrix, so it cannot set the
style component unless it runs nb2listw() internally with a given style.
The function could be edited to do this, but the user can also do it
externally. If you'd prefer to have a:
mat2listw <- function(x, row.names=NULL, style="M") {
...
where setting the style= argument to something other than "M" would lead
to nb2listw() being called, please let me know.
Hope this helps,
Roger
> In my current work on spatial panels I needed to transform a (huge)
> matrix to listw with 'style="W"' (without having the original
> shapefiles, or 'nb' etc.), and I had some success by using mat2lisw()
> and then manually setting the $style to "W" and setting the $d
> component of the 'weights' attribute to the appropriate (?) value by
> hand, like this:
>
> COL.listw.p3$style <- "W"
> attr(COL.listw.p3$weights,
> "comp")$d<-unlist(lapply(COL.listw.p3$weights, length))
>
> after which the above code line (*) looks like working fine, despite
> some very small differences in the estimated lambda parameter, possibly
> inherent to the different method? I still wonder whether this very dirty
> procedure really gives reliable results...
>
> All in all, though, **if**, as Roger put it,
> - Angela means an NxT by NxT matrix
> - N and T are small enough for the Jacobian to be treated with plain
> vanilla methods
> - and there are no spatial correlations between the different
> cross-sections,
> the above code might solve her problem.
>
> I'll be grateful for any insights re: coercion to "W" of the $style
> attribute.
> Cheers,
> Giovanni
>
> Giovanni Millo
> Research Dept.,
> Assicurazioni Generali SpA
> Via Machiavelli 4,
> 34132 Trieste (Italy)
> tel. +39 040 671184
> fax +39 040 671160
>
> -------------- Original message ----------------
>
> Message: 5
> Date: Mon, 18 Oct 2010 08:58:15 +0200 (CEST)
> From: Roger Bivand <>
> To: Angela Parenti <>
> Cc: r-sig-geo at stat.math.ethz.ch, Davide Fiaschi
> <dfiaschi at ec.unipi.it>, Mario A Lavezzi <lavezzi at unipa.it>
> Subject:
> Message-ID: <alpine.LRH.2.00.1010180849050.3192 at reclus.nhh.no>
> Content-Type: TEXT/PLAIN; format=flowed; charset=US-ASCII
>
> On Thu, 14 Oct 2010, Angela Parenti wrote:
>
>>
>> Hello Roger and everybody else.
>>
>> I have another question about Weight Matrices. For one of my models I
>> estimate a pooled regression. Then, in order run the Moran test on the
> pooled
>> regression residuals I need to construct a spatial weights list of the
> same
>> dimension.
>> I already found the way for the spatial matrix built on the inverse
> distance
>> between neighbours. Here the code for inverse distance matrix:
>>
>> #####################
>> # Moran's Test for spatial effects
>> sampleCentroid.Pooled <-
>>
> rbind(samplecentroid.europeReorderedCamEcon,samplecentroid.europeReorder
> edCamEcon)
>> eu.regions.nb_pooled <- dnearneigh(sampleCentroid.Pooled[iii,], 0,
>> max(distanceMatrix), row.names=NULL, longlat=TRUE)
>> coords_pooled <- sampleCentroid.Pooled[iii,]
>> dlist_pooled <- nbdists(eu.regions.nb_pooled, coords_pooled,
> longlat=TRUE)
>>
>> dlist1_pooled <- lapply(dlist_pooled, function(x) 1/(x^2))
>> eu.regions.w1_pooled <- nb2listw(eu.regions.nb_pooled,
> glist=dlist1_pooled,
>> style="W", zero.policy=TRUE)
>>
>> moranTestmodelI_w1 <- lm.morantest(modelI,
>> eu.regions.w1_pooled,zero.policy=TRUE, alternative="two.sided")
>> print(rbind(c("Global Moran's I for Model I","w1_EK","bivand",""),
>> c("Observed Moran's I", "EI", "VI", "p-value"),
>>
> c(as.numeric(moranTestmodelI_w1$estimate[1]),as.numeric(moranTestmodelI_
> w1$estimate[2]),
>>
> as.numeric(moranTestmodelI_w1$estimate[3]),moranTestmodelI_w1$p.value)
>> ))
>> ######################
>>
>> Differently, I didn't understand yet how to build a spatial weights
> list for
>> pooled data in case of first-order contiguity matrix. Can you help me?
>
> By "pooled", do you mean an NxT by NxT matrix? Is your data
> spatio-temporal in that case? How large is N, and how large T? Did your
> IDW procedure induce "spatial" relations above and below the block
> diagonal, or do you just need T replicated N by N weights matrices down
> the block diagonal? You may have a result from IDW, but is it what you
> think it is - should d_{itjt} == d_{itj(t-1)} == d_{itj(t+1)} and so on?
>
> To create a contiguity matrix, you can use graph-based methods listed
> under ?graph2nb or contiguity using ?poly2nb, for which you do need the
> boundaries of your regions as for example a shapefile.
>
> Roger
>
>>
>> Thank you in advance,
>> Angela Parenti
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at stat.math.ethz.ch
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>
>
--
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no
More information about the R-sig-Geo
mailing list