R Unmarked error: obsCovData does not have M*obsNum rows

1k Views Asked by At

I am fairly new to R and trying to model occupancy probability for my species (Lepilemur). I have 21 sites, 2 site covariates and 3 observation covariates. My code is as follows:

lepi<-read.csv("Lepilemur2.csv", header=T)
nSites<- 21

lepiDetects<- lepi[,2:4]
lepiSiteCovars<-as.data.frame(lepi[,5:6])
lepiObsCovars<-as.data.frame(lepi[,7:9])
detect<-apply(lepiDetects, 2, function(x){as.numeric(as.character(x))})
umf<-unmarkedFrameOccu(y=detect, siteCovs= lepiSiteCovars, obsCovs=lepiObsCovars)

However when I run the final line of code i receive the following error: Error in validObject(.Object) : invalid class “unmarkedFrame” object: obsCovData does not have M*obsNum rows

In my global environment it says I have 21 observations of 3 variables.

Any help you can offer me would be hugely appreciated.

Thanks,

Buffy

1

There are 1 best solutions below

0
On

For your case, I would do

umf<-unmarkedFrameOccu(y=detect, siteCovs= lepiSiteCovars, 
obsCovs=list(lepiObsCovars=lepiObsCovars))

Details about why that will work are as follows. The manual for unmarkedFrameOccu states you can use a data frame but there's something weird about it:

obsCovs: Either a named list of ‘data.frame’s of covariates that vary within sites, or a ‘data.frame’ with RxJ rows in site-major order.

I tested it using the example from the library unmarked, we simulate some data:

library(unmarked)

R <- 4 # number of sites
J <- 3 # number of visits
y <- matrix(c(
   1,1,0,
   0,0,0,
   1,1,1,
   1,0,1), nrow=R, ncol=J, byrow=TRUE)

site.covs <- data.frame(x1=1:4, x2=factor(c('A','B','A','B')))

obs.covs <- list(
   x3 = matrix(c(
      -1,0,1,
      -2,0,0,
      -3,1,0,
      0,0,0), nrow=R, ncol=J, byrow=TRUE),
   x4 = matrix(c(
      'a','b','c',
      'd','b','a',
      'a','a','c',
      'a','b','a'), nrow=R, ncol=J, byrow=TRUE))

The example from the vignette works, using a list:

umf <- unmarkedFrameOccu(y=y, siteCovs=site.covs, 
    obsCovs=obs.covs)  

But if we do something similar to your example, we get the error:

x4_df <- data.frame(obs.covs$x4)
umf <- unmarkedFrameOccu(y=y, siteCovs=site.covs, 
        obsCovs=x4_df)

Error in validObject(.Object) : 
  invalid class “unmarkedFrame” object: obsCovData does not have M*obsNum rows.

So we have to do:

x4_list <- list(x4=data.frame(obs.covs$x4))
umf <- unmarkedFrameOccu(y=y, siteCovs=site.covs, 
            obsCovs=x4_list)