rbchan/unmarked

R session aborted when run occu with covariates

TSchriever opened this issue · 4 comments

Screenshot 2023-01-24 152624

Occupancy modeling for anurans at Ludington State Park

#Morgan Morin thesis

library(terra)
library(unmarked)
library(wiqid)
frogoc=read.csv("toad breeding OM matrix.csv")
#toad breeding presence/absence matrix based on 4 site visits [,1:4]x 41 sites [rows]
#columns of observational data (presence/absence by sampling visit)
toad.presence <- as.matrix(frogoc[1:4])
head(toad.presence)

#Next, we want to gather our covariate data. We will need to standardize the date and time data.
#annotate and standardize variables
#columns of covariate/predictor data (DO %)
DOper <- as.matrix(frogoc[5:8])
DOper.s<-standardize(DOper)
DOper <- data.frame(frogoc[5:8])

#min and max air temps in Ludington MI on sampling days
min.air <- as.matrix(frogoc[9:12])
min.air.s <- standardize(min.air)
max.air <- as.matrix(frogoc[13:16])
max.air.s <- standardize(max.air)
#group the observational covariates
obs.cov = list(min.air.s,max.air.s)

#use DOper.s as a site covariate
toad.umf <- unmarkedFrameOccu(y=toad.presence, siteCovs = DOper, obsCovs = obs.cov)
summary(toad.umf)

#covariate needs unlisted or error message appears
site.Cov <- unlist(DOper)

#model formula will include both our site covariates and our observation covariates.
toad.DO.1 <- occu(min.air.s+max.air.ssite.Cov, data=toad.umf)

Error message appears at this point. I have updated R studio to 2022.12.0 Build 353 and the problem still occurs. The same occurs when a student runs it on their computer.

I am guessing the bug has to do with the structure of the unmarkedFrame. Can you email me the CSV file so I can try to replicate it? contact at kenkellner.com

There were a couple problems with the unmarkedFrame. First in order to use a covariate in the model it has to be inside the unmarkedFrame (so you can't use site.Cov directly). Second, a proper site covariate can only have one value per site. Your DOper covariate has four values per site. unmarked isn't expecting this and crashes. It should give you an error message instead. You'll need to summarize DOper to have a single value per site, perhaps by averaging the four values for each site. See example below.

library(unmarked)
library(wiqid)

frogoc=read.csv("toad breeding OM matrix.csv")
toad.presence <- as.matrix(frogoc[1:4])

DOper <- as.matrix(frogoc[5:8])
# Site covs can only have one value per site
# Average the four values by site
DOper_site <- apply(DOper, 1, mean)

# Standardize and make site covs dataframe
site.cov.s <- data.frame(DOper=as.numeric(scale(DOper_site)))

min.air <- as.matrix(frogoc[9:12])
max.air <- as.matrix(frogoc[13:16])

#group the observational covariates
# Need list to be named
# Standardize
obs.cov.s <- list(min.air=standardize(min.air),
                  max.air=standardize(max.air))

toad.umf.s <- unmarkedFrameOccu(y=toad.presence, siteCovs = site.cov.s, 
                                obsCovs = obs.cov.s)
summary(toad.umf.s)

toad.DO.1 <- occu(~min.air+max.air ~ DOper, data=toad.umf.s)
toad.DO.1

# You can also skip standardizing manually and do it directly in the formulas
site.cov <- data.frame(DOper = DOper_site)
obs.cov <- list(min.air=min.air, max.air=max.air)

toad.umf <- unmarkedFrameOccu(y=toad.presence, siteCovs=site.cov, obsCovs=obs.cov)

occu(~scale(min.air)+scale(max.air) ~ scale(DOper), data=toad.umf) # same answer

Thank you Ken.
I was thinking it could be something like that given the examples only had one value per covariate. I will correct as suggested and let you know if it works.
Tiffany