[R-sig-Geo] SGDF loop processing time

Roger Bivand Roger.Bivand at nhh.no
Wed Jun 25 22:51:56 CEST 2008


On Wed, 25 Jun 2008, Ilona Naujokaitis-Lewis wrote:

> Dear Roger, and List-Serve,
>
> I am posting here a .RData file(IlonaQuesRgeo.RData) to help with my
> previous query (see original email below) as per Roger's request. Please let
> me know if the attachment does not work.

Hi,

It reached me, not sure about the list - the posting guide on R-help sets 
out the attachments policy.

library(sp)
load("IlonaQuesRgeo.RData")

Patchmap$HSvalue <- HSmap$HSvalue
Patchmap_orig <- Patchmap
fullgrid(Patchmap) <- FALSE
gridded(Patchmap) <- FALSE
# drop back to points to split on ID

Patchmap0 <- Patchmap[Patchmap$ID==0,]
Patchmap1 <- Patchmap[Patchmap$ID==1,]
# split subsets

Patchmap0$NPHS <- ifelse(Patchmap0$HSvalue < HSthreshold,
   Patchmap0$HSvalue, NA)
# vectorised ID==0 (is the else value correct?)

Patchmap1$NPHS <- round(rnorm(length(Patchmap1$ID), mean_HS,
   sd_HS),digits=3)
Patchmap1$NPHS <- ifelse(Patchmap1$NPHS < new_HSthreshold,
   Patchmap1$HSvalue, Patchmap1$NPHS)
# vectorised ID==1 (is the else value correct?)

Patchmap_res <- rbind(Patchmap0, Patchmap1)
gridded(Patchmap_res) <- TRUE
fullgrid(Patchmap_res) <- TRUE
# put back together, re-sorting to get back in place

all.equal(Patchmap_res$ID, Patchmap_orig$ID)
# sanity check on IDs

Patchmap_orig$NPHS <- Patchmap_res$NPHS
# assign back if need be and display image
image(Patchmap_orig, "NPHS")

There must be better ways, the workflow is a little contorted, but it may 
be worth trying. Maybe a set.seed() to anchor the rnorm()?

Hope this helps,

Roger

>
> Cheers,
> Ilona
>
>
>
> Ilona Naujokaitis-Lewis
> Centre for Applied Conservation Research
> Forest Sciences Department
> University of British Columbia
> 3041-2424 Main Mall
> Vancouver, BC V6T 1Z4
>
> phone: 604 822.4382
> email: ilonan at interchange.ubc.ca
>
> -----Original Message-----
> From: Roger Bivand [mailto:Roger.Bivand at nhh.no]
> Sent: Wednesday, June 25, 2008 10:18 AM
> To: Ilona Naujokaitis-Lewis
> Cc: r-sig-geo at stat.math.ethz.ch
> Subject: Re: [R-sig-Geo] SGDF loop processing time
>
> On Wed, 25 Jun 2008, Ilona Naujokaitis-Lewis wrote:
>
>>
>>
>> Dear List-serve,
>>
>> I have written a loop that creates a Spatial Grid Dataframe from 2 other
>> SGDFs. I am interested in finding out if there is a way to decrease the
>> processing time of the entire loop. I am aware that loops in R are not the
>> most efficient way to go, but based on my knowledge, I am not sure how to
>> get around this. To avoid problems with memory allocation I have added
> gc()
>> command throughout the loop.
>>
>> The system.time() results for the loop (see code below) with a small
> (16x12)
>> grid is:
>> user  system elapsed
>> 17.08    0.02   17.22
>>
>> The problem is when I work with larger grids (i.e. greater than
> dimensions:
>> 900x1200). The processing time is in the order of 24 hours.
>>
>> I have checked threads for help on both decreasing the memory size, and
>> writing efficient code. Any suggestions on how I might decrease the
>> processing time, as this is the biggest concern right now (both using and
>> not using R, but the former preferred), would be greatly appreciated.
>>
>> Here is the code with a brief description of the objects.
>>
>>
>> Objects:
>> prePatchmap: SGDF, binary (0,1)
>> mean_HS: mean habitat suitability value of original habitat suitability
> map
>> sd_HS: standard deviation of original habitat suitability map
>> new_HSthreshold: new habitat suitability threshold
>> HSthreshold: original habitat suitability threshold
>> HSmap: SGDF of original habitat suitability values
>
> Could you provide an online example - save() the objects you need here and
> post the RData file? This does look vectorisable, at least in two chunks.
>
> Roger
>
>>
>> for (i in 1:length(prePatchmap$patch))
>>    {
>>    if (prePatchmap$patch[i]==1)
>>       {
>>       prePatchmap$NPHS[i]<-round(rnorm(1, mean_HS, sd_HS),digits=3)
>>       gc()
>>          {
>>          if (prePatchmap$NPHS[i]<new_HSthreshold)
>>            {
>>            prePatchmap$NPHS[i]<-new_HSthreshold
>>            gc()
>>            }
>>          }
>>       } else
>>       {
>>       if (HSmap$HSvalue[i]<HSthreshold)
>>            {
>>            prePatchmap$NPHS[i]<-HSmap$HSvalue[i]
>>            }
>>       gc()
>>       }
>>     }
>>
>>
>> Cheers,
>>
>> Ilona Naujokaitis-Lewis
>>
>>
>> Ilona Naujokaitis-Lewis
>> Centre for Applied Conservation Research
>> Forest Sciences Department
>> University of British Columbia
>> 3041-2424 Main Mall
>> Vancouver, BC V6T 1Z4
>>
>> phone: 604 822.4382
>> email: ilonan at interchange.ubc.ca
>>
>> _______________________________________________
>> 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