Skip to content

statistics for ragged arrays: loop to vector

2 messages · Elena Moltchanova, Roger Bivand

#
Dear R users,

I am currently writing a MCMC algorithm for spatial Poisson model. One
problem which I need to solve is as follows:

I have a vector X[1:J] which describes characteristics of cells of a
rectangular grid. Further, for each cell I have a list of adjascent
cells adj[] and the vector listing numbed of adjascent cells num[].

Thus if I want to list cells adjascent to cell i I write

begin.n[i]<-cumsum(c(0,adj)[1:i])+1
end.n[i]<-cumsum(adj[1:i])

and if I want to calculate some function, say mean, over the adjascent
cells I write

f.X[i]<-mean(X[begin.n[i]:end.n[i]])

This works fine, but if I need the function to be calculated for each
cell of the field a couple of times for each MCMC iteration, the amount
of time which is used by the loop is too large. My question is: can this
loop be written in some vector form?

Thanks for any comments

Elena Moltchanova
-------------- next part --------------
A non-text attachment was scrubbed...
Name: emor.vcf
Type: text/x-vcard
Size: 270 bytes
Desc: Card for Elena Moltchanova
Url : https://stat.ethz.ch/pipermail/r-help/attachments/20011022/ce59b19a/emor.vcf
#
On Mon, 22 Oct 2001, Elena Moltchanova wrote:

            
This is what lag.listw() in the sptests package does, using a list of
weights (here 1/(number of neighbours of i)) created by nb2listw() in the
spatial weights manipulation package spweights. You may find the
neighbours list structure used there in function cell2nb() of interest
too. I'd be grateful for feedback on the packages, and to hear more of
your progress with spatial probits.

Roger