[R-sig-Geo] placing spdf back into adehabiatHR estUDm

Anthony Fischbach afischbach at usgs.gov
Thu Sep 8 21:53:42 CEST 2011


I have worked from the script that you provided in your previous posting to
address the question, 
"Is there a means to convert the SpatialPixelDataFrame back into an estUDm?
"
My modifications to your script are noted by my initials, ***ASF**.

I have extended your script to work with an estUDm created by the BRB
function.

In delving into the estUDm object I noticed two odd things.
	1) estUD objects within the estUDm do not have names assigned when they are
created by the BRB function.
	2) each estUD object within the estUDm has a full listing of the D values
from all animals listed in the estUDm.

I was able to clip the estUD's using the habitat grid.  By use of the slot()
function I was able to placed the 'clipped' utilization distribution in the
form of a spatial pixel data frame back into the estUDm class object. 
However, the resultant estUDm raster appears to have the y coordinates
reversed.

The modified script is listed below.

## Load the data
library(adehabitatHR)
library(adehabitatLT)
data(puechabonsp)

## Store the relocations
loc <- puechabonsp$relocs

## and the map
elev <- puechabonsp$map

## have a look at the data
head(as.data.frame(loc))
## the first column of this data frame is the ID

## Look at the map
image(elev)

## Now we build a "fake" habitat map (1 = habitat and 0 = non habitat) from
the elevation map
fullgrid(elev) <- TRUE
hab <- elev
hab[[1]] <- as.numeric(!is.na(hab[[1]]))

## show this habitat map (yellow is habitat)
image(hab)

## Estimation of UD for the four animals, using
## the map hab as the grid
ud <- kernelUD(loc[,1], grid=hab)
###ASF### 
###ASF### try with BRB
###ASF### first make ltraj object
###ASF### Cast the Dates as POSIXct dates
loc$DT <-as.POSIXct(strptime(loc$Date, format='%y%m%d' ))
###ASF### Create an ltraj trajectory object.
ltraj <- as.ltraj(coordinates(loc), loc$DT, id = loc$Name, burst = loc$Name,
typeII = TRUE)
###ASF### Assigne parameter values for BRB
###ASF### Parameters for the Biased Random Bridge Kernel approach
tmax <- 5*(24*60*60) + 1	## set the maximum time between locations to be
just more than 5 day
lmin <- 0.1	## locations less than 0.1 meters apart are considered inactive.  
			## Set very very low to ensure that the acvitivy variable is used.
hMin <- 100	## arbitrarily set to be same as hab grid cell resolution

vv2<- BRB.likD(ltraj, Dr=c(1,50000), Tmax = tmax, Lmin = lmin,  habitat =
NULL)  ##
ud <-BRB(ltraj, D =vv2, Tmax =tmax, Lmin = lmin, hmin = hMin, habitat =
NULL,  
		grid = hab, b = TRUE, extent = 0, tau = 24*60*60 )
## ud is an object of the class estUDm
## get volumetric contours
ver <- getverticeshr(ud, percent=95,  standardize = TRUE, whi = id(ltraj))
###ASF###  !! PROBLEM !!
# > str(ud)
# List of 4
 # $ :Formal class 'estUD' [package "adehabitatHR"] with 9 slots
  # .. ..@ h          :List of 3
  # .. .. ..$ values:List of 2
  # .. .. .. ..$ hmin: num 100
  # .. .. .. ..$ D   :List of 4
  # .. .. .. .. ..$ Brock: num 4.26
  # .. .. .. .. ..$ Calou: num 1
  # .. .. .. .. ..$ Chou : num 1.86
  # .. .. .. .. ..$ Jean : num 6.21
  # ... further str(ud) results omitted
###ASF### 2 Problems apparent here: 
###ASF### 1) no names have been assigned to the items in the ud list;
###ASF### 2) the list of D values has been inflated such that a full listing
of D values is given under each animal, rather than just one for each animal
###ASF### address names in ud by assigning them to be the same as the ids in
ltraj
names(ud) <- id(ltraj)
## Convert it to SpatialPixelsDataFrame:
udspdf <- estUDm2spixdf(ud)

## udspdf is an object of class SpatialPixelsDataFrame
## have a look
mimage(udspdf)

## Convert the original map to fullgrid (i.e. SpatialGridDataFrame)
fullgrid(udspdf) <- TRUE

## and the same for the original habitat map (here, it is not needed,
## as the map hab is already fullgrid, but it might be required on you data)
fullgrid(hab)<-TRUE

## The two maps have the same dimensions:
## > length(udspdf[[1]])
## [1] 6636
## > length(hab[[1]])
## [1] 6636

## Then, you just have to multiply each column of udspdf by the habitat
variable:
resu <- lapply(1:ncol(udspdf), function(i) {
     udspdf[[i]] * hab[[1]] / sum(udspdf[[i]] * hab[[1]])
})
resu <- as.data.frame(resu)
names(resu) <- names(udspdf at data)

## and define it as data slot for udspdf
udspdf at data <- resu

## Have a look at the data (after conversion to SpatialPixelsDataFrame):
fullgrid(udspdf) <- FALSE
mimage(udspdf)

## Note that Brock and Calou, the UD have sharp limits
###ASF### Yes I see this.

###ASF### [2011 August 05]: Questions for ClementCalenge:
###ASF### ???? How can we extract home-range contours now that the home
range estimates are of class SpatialPixelsDataFrame?
###ASF### In an effort to return access the function getverticeshr, cast the
'clipped' udspdf back into the estUDm object
###ASF### Try stepping through each animal in the ud and replacing the @data
element.

ud2 <- ud	##make copy of the ud
	for (i in 1:length(ud2)){
		idStr <- id(ltraj[i])
		print(paste(i, "th animal is ", idStr, sep=""))
		temp <- udspdf[idStr]
		udf <- temp at data
		names(udf) = "dens"
		print(summary(slot(ud2[[i]], "data")))	## print summary of the ud object
		print(summary(udf))	## print summary of the udspdf object
		slot(ud2[[i]], "data", check = TRUE) <- udf	## Replaced the data slot with
the clipped data
	}
ver2 <- getverticeshr(ud2, percent=95,  standardize = TRUE, whi = id(ltraj))
###ASF### !!PROBLEM!! the coordinates are twisted.
###ASF### Have a look at the resulting ud
image(ud2)




-----
Tony Fischbach, Wildlife Biologist
Walrus Research Program
Alaska Science Center
U.S. Geological Survey
4210 University Drive
Anchorage, AK 99508-4650

AFischbach at usgs.gov
http://alaska.usgs.gov/science/biology/walrus
--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/Walruses-and-adehabitatHR-class-estUDm-exclusion-of-non-habitat-pixels-and-summary-over-all-animals-tp6497315p6773497.html
Sent from the R-sig-geo mailing list archive at Nabble.com.



More information about the R-sig-Geo mailing list