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
statistics for ragged arrays: loop to vector
2 messages · Elena Moltchanova, Roger Bivand
On Mon, 22 Oct 2001, Elena Moltchanova wrote:
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?
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
Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Breiviksveien 40, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 93 93 e-mail: Roger.Bivand at nhh.no and: Department of Geography and Regional Development, University of Gdansk, al. Mar. J. Pilsudskiego 46, PL-81 378 Gdynia, Poland. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._