Hi Melanie,

In my previous email, I recommended that for each individual 95% and 50% kernels should be generated for each combination of locations. IF done properly it should be quite simple to extract the mean 95% and 50% ud area using the 100 permutations.

As for generating an average contour, I would try Anthony Fischbach's suggestion of converting your UDs to a Spatial Pixels Data Frame and summing/normalising across pixel values for your 100 permutations to extract a mean volumetric contour.

It would be interesting to compare the mean areas calculated using my technique with the area of the contour using Tony's to see how they differ.

To write this as a shapefile object, convert extract the vertice of interest from the estUD object. Next try the spCbind and writeOGR functions [maptools] to combine the mean 95% and 50% ud area with a spatialpolygonsdataframe object. Finally use writeOGR with the ESRI Shapefile driver to open this in ArcGIS.

Hope this helps,

Ross

From: Melanie Shapiera [mailto:m.shapiera22@gmail.com]
Sent: Friday, 27 April 2012 10:40 AM
To: Ross Dwyer
Subject: Re: [R-sig-Geo] averaging home range contours

Hi Ross

Thanks so much for your reply! I really appreciate you taking the time to consider my question.

I agree, the loop isn't super efficient - I'm still a beginner in R. I wasn't aware that there was an ID option in SpatialPointsDataFrame- I will be looking into adding an ID field for my iterations. I actually have files for multiple individuals, where I want to do my 100 samples, so this could save A LOT of time. Any tips on setting up an ID field?

I saw that the function kernel.area() actually calculates the area of the two contour types I want to eventually average. As I said, I'm still a novice, so I used the print() option to print out the areas, which I could average myself.
But if the ID field in the SPDF can be used to just run getverticeshr for each replicate, that is awesome!

Either way, I feel that I can get the average number value of the area, but I would also like to get an average contour that I could write to a shapefile to view in Arc. That was actually my initial question, though I suppose I didn't word things properly. I have never read/learned anything about creating an "average" polygon, either in Arc or R. It would be nice to have this for a figure, but I'm not sure if it is possible!

Thanks again Ross for your reply!

Melanie


On Thu, Apr 26, 2012 at 9:00 PM, Ross Dwyer <ross.dwyer@uq.edu.au<mailto:ross.dwyer@uq.edu.au>> wrote:
Hi Melanie,

I think you're going about this the wrong way. My advice is to...
1. Get the data into the correct format for a spatial points data frame (X, Y, ID)
       Where ID = replicate number
2. Run kernelUD to generate UDs for the 100 replicates
3. Use ver95 <- getverticeshr(kud, lev = 95) and ver50 <- getverticeshr(kud, lev = 50) to extract your vertices for each replicate. These can then be easily saved as a .shp.
4. Run the following to extract your areas
Area95 <- sapply(slot(ver95, "polygons"), function(i) slot(i, "area")))
Area50 <- sapply(slot(ver50, "polygons"), function(i) slot(i, "area")))
5. Then you can simply run mean(Area95) etc to extract the mean of all replicates for each contour.

Best to avoid the loop as I don't think it's really needed here.

Cheers, Ross


-----Original Message-----
From: r-sig-geo-bounces@r-project.org<mailto:r-sig-geo-bounces@r-project.org> [mailto:r-sig-geo-bounces@r-project.org<mailto:r-sig-geo-bounces@r-project.org>] On Behalf Of Melanie Shapiera
Sent: Wednesday, 25 April 2012 11:07 PM
To: r-sig-geo@r-project.org<mailto:r-sig-geo@r-project.org>
Subject: [R-sig-Geo] averaging home range contours

Hello all

I am trying to run some Monte Carlo simulations on single individuals in R.

So far, I have sampled 100 X and Y coordinates from known positions of an individual, 100 times. This results in a list of 200. I then convert this list into a dataframe that is more condusive to eventual functions I want to run for each sample (kernel.area,kernelUD).
So I had a data frame with 200 columns, and I wanted to perform the kernel.area function (adehabitatHR), using each successive pair of columns (which represent a resampling). I ended up writing a loop that allows me to calculate the kernel.area for each of the simulations, as well as the 95% volume contour, which is saved as a Spatial Polygon.

I now want to take the average of these 95% volume contours, to get an average 95% volume contour that I can view in Arc. However, I am unsure how to do this. Any advice would be greatly appreciated!
I can't replicate my data very well here, so I'll give an example of what the structure is:

# generate dataframe representing X and Y positions df <- data.frame(x=seq(1:200),y=seq(1:200))
# 100 replications of sampling 100 "positions"
resamp <- replicate(100,df[sample(nrow(df),100),])
# convert to data frame (kernel.area needs an xy dataframe) dfnew <- do.call("rbind", resamp[1:2,]) # xy positions need to be in columns for kernel.area
df2 <- t(dfnew)

Ok, here is the for loop I've been using for my actual positions:

#calculate the 50,95% kernel area for each simulation, as well as the 95% volume contour

for (j in seq(1,ncol(df2)-1,2)) {
kud <-kernelUD(SpatialPoints(df2[,j:(j+1)]),kern="bivnorm")
kernAr<-kernel.area(kud,percent=c(50,95),unin=c("m"),unout=c("km2"))
print(kernAr)
name[[j]]<-getverticeshr(kud, lev = 95)
}
Again, the next step I wish to take is to average the hundred 95% contours I have created (ie name[[1]],name[[3]]...). Any suggestions?
Melanie
--
Melanie Shapiera
MSc candidate
Memorial University of Newfoundland
       [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo@r-project.org<mailto:R-sig-Geo@r-project.org>
https://stat.ethz.ch/mailman/listinfo/r-sig-geo



--
Melanie Shapiera
MSc candidate
Memorial University of Newfoundland


	[[alternative HTML version deleted]]

