Skip to content

How to build a spatial weights list for pooled data

3 messages · Millo Giovanni, Roger Bivand, Angela Parenti

#
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. 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:

            
pooled
same
distance
rbind(samplecentroid.europeReorderedCamEcon,samplecentroid.europeReorder
edCamEcon)
longlat=TRUE)
glist=dlist1_pooled,
c(as.numeric(moranTestmodelI_w1$estimate[1]),as.numeric(moranTestmodelI_
w1$estimate[2]),
as.numeric(moranTestmodelI_w1$estimate[3]),moranTestmodelI_w1$p.value)
list for
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

  
    
#
On Tue, 19 Oct 2010, Millo Giovanni wrote:

            
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