(A guest blog post from Dakota Benjamin today, a Case Western Reserve University senior, and 3 year Summer intern we’ve been luck enough to recruit)
This post has two big take-aways: using spatial data in R, and applying that knowledge to estimating home ranges. If you are not familiar with the R environment, there are many great resources to familiarizing yourself with this powerful language, such as [here](http://cran.r-project.org/doc/manuals/R-intro.pdf) and [here](http://www.statmethods.net/index.html). You will need a basic understanding of R before proceeding.
The package [rgdal](http://cran.r-project.org/web/packages/rgdal/) is essential for utilizing spatial data in R. There are two functions that we will use. First is readOGR(). This function will import a shape file into a special type of data frame, in this case a SpatialPointsDataFrame, which contains the data, coordinates, bounding box, etc. which makes it all accessible in R.
coyote <- readOGR("nc_coyote_centroids_dn","nc_coyote_centroids_dn")
The second function we will use is writeOGR(), which as you can probably guess will write a SpatialDataFrame to a shape file (or other format).
writeOGR(ver, "hr_coyote_centroids_dn", "hr_centroids_dn", "ESRI Shapefile")
Now we can work with the coyote data in R and use some tools to estimate the home range. The previous blog post shows how to clean up the data using PostGIS (https://smathermather.wordpress.com/2014/07/31/cleaning-animal-tracking-data-throwing-away-extra-points/) will be useful for cleaning up and framing the data in a useful way. What if we want to look at how the home range changes between night and day? I wrote a quick function to add a column to the data. calcDayNight() uses a function from the [RAtmosphere package](http://cran.r-project.org/web/packages/RAtmosphere/) called suncalc(), which calculates the sunrise and sunset for a given date and location. calcDayNight() compares each data point to the sunrise and sunset times and determines whether the point was taken during the day or at night. Here’s the code:
calcDayNight <- function(x) { #if time is greater than the sunrise time or less than the sunset time, apply DAY suntime <- suncalc(as.numeric(as.Date(x["LMT_DATE"], format="%m-%d-%y") - as.Date("2013-01-01")), Lat=41.6, Long=-81.4) coytime <- as.numeric(strptime(x["LMT_TIME"], "%T") - strptime("00:00:00", "%T")) if(coytime > suntime$sunrise & coytime < suntime$sunset){ x["dayornight"] <- "DAY" } else x["dayornight"] <- "NIGHT" }
After we get the day and night centroids (from the previous post), we can do our home range estimation. The package we need is [adehabitatHR](http://cran.r-project.org/web/packages/adehabitatHR/). There’s two steps here: create the utilization distribution, and produce a home range contour from that distribution.
The function for creating the distribution is kernelUD(). More information about this model can be found [here](http://cran.r-project.org/web/packages/adehabitatHR/adehabitatHR.pdf) on page 33.
ud <- kernelUD(coyote[,2], h="href")
coyote[,2] refers to the “dayornight” column that contains the “DAY” and “NIGHT” values. Two different distributions will be calculated. Then we will produce a SpatialPolygonsDataFrame using the getverticeshr(), which will be the 95% estimation of the home range:
ver <- getverticeshr(ud, 95)
Export that as I showed above and we get a shape file with two polygons. Above is the home range estimation with the day/night centroids overlaid (blue for day and purple for night).