rbchan / unmarked

R package for hierarchical models in ecological research
https://rbchan.github.io/unmarked/
37 stars 25 forks source link

R session aborted when run occu with covariates #246

Closed TSchriever closed 1 year ago

TSchriever commented 1 year ago

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.s~site.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.

kenkellner commented 1 year ago

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

TSchriever commented 1 year ago

Hi Ken, Thank you for the quick response. Attached is the file. Tiffany PS. the kenkellner.com didn't work to send email to.


From: Ken Kellner @.> Sent: Tuesday, January 24, 2023 3:54 PM To: rbchan/unmarked @.> Cc: Tiffany Schriever @.>; Author @.> Subject: Re: [rbchan/unmarked] R session aborted when run occu with covariates (Issue #246)

Attention: This email is from outside Western Michigan University. Use caution when opening links and attachments.

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

— Reply to this email directly, view it on GitHubhttps://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Frbchan%2Funmarked%2Fissues%2F246%23issuecomment-1402640804&data=05%7C01%7Ctiffany.schriever%40wmich.edu%7Ccf9d0ac544144a8c643f08dafe4d2a36%7C257622517aa94c72905f39bf026a8a84%7C0%7C0%7C638101904620716114%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=NnkTDcbU0dQ3AGIQuIlxW%2BDliyl3zdC%2BQ7HTFBN%2B2NA%3D&reserved=0, or unsubscribehttps://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FA5OJUTZSMDY3U5PCIQZKFELWUA6PXANCNFSM6AAAAAAUFRR3YE&data=05%7C01%7Ctiffany.schriever%40wmich.edu%7Ccf9d0ac544144a8c643f08dafe4d2a36%7C257622517aa94c72905f39bf026a8a84%7C0%7C0%7C638101904620716114%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=Ueiic4%2BxkC7hfkJ%2FKp9%2BwVFAQb144Hxksczs%2F9Hjfdw%3D&reserved=0. You are receiving this because you authored the thread.Message ID: @.***>

kenkellner commented 1 year ago

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
TSchriever commented 1 year ago

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