# uhcdenscalc

Function to calculate density estimates for the environmental characteristics associated with the observed locations in the test data and also associated with the randomly chosen locations generated by the uhcsim or uhcsimstrat functions.

• sims = characteristics associated with the simulated locations in the test data set
• dat = observed characteristics associated with observed locations in the test data set
• avail = characteristics available to the animal
• gridsize = size of grid for estimating the kde

importFrom = KernSmooth

return = list of density estimates

``````uhcdenscalc <- function(sims, dat, avail, gridsize=500){

# Density plot of x for each simulated data set & also for the test data set
sims <- as.matrix(sims)
dat <- as.matrix(dat)
avail <- as.matrix(avail)
alldat <- c(sims,dat, avail) # combine data to get reasonable axis limits
range.x <- c(min(alldat), max(alldat)) # to get reasonable axis limits
nsims <- nrow(sims)

# to hold density estimates fU^(z) for all nsims datasets
densrand <- matrix(NA, nsims,gridsize)
# observed data density, fu(z)
densdat <- bkde(dat, range.x=range.x, gridsize=gridsize)
for(i in 1:nsims){ # densities for simulated data sets
temp <- bkde(sims[i,], range.x=range.x, gridsize=gridsize)
densrand[i,] <- temp\$y #estimated density
}
# density of availalbe points
densavail <- bkde(avail,range.x=range.x, gridsize=gridsize)
out <- list(densdat=densdat, densrand=densrand, densavail=densavail)
return(out)
}
``````

spun with ezspin(“uhcplots/functions/uhcdenscalc.R”, out_dir = “uhcplots/output”, fig_dir=“uhcplots/figures”, keep_md=F)