Trouble building unmarkedFrameOccu in Package unmarked

2,302 views
Skip to first unread message

Brian Ortman

unread,
Dec 11, 2014, 4:15:09 PM12/11/14
to unma...@googlegroups.com

I am trying to run an occupancy analysis using the R package Unmarked.  I normally work with  very large sets of data that include point (repeated) count survey data for multiple species of birds with associated data for site covariates such as habitat type(s) and detection covariates such as time, temperature, cloud cover, wind, observer, etc.  I have in the past used PRESENCE Software but would like to use R instead as I am trying to stream line this process.  I have imported the data and attempted to build an unmarkedFrameOccu data frame using the following data as practice (normally my data sets include 8-10 site variables and 10-12 detection covariates):

Site Species D1 D2 D3 Date1 Date2 Date3 Temp1 Temp2 Temp3 Time1 Time2 Time3 Grass Woods  Water
1 GOEA 1 0 1 0.07 0.16 0.25 0.56 0.67 0.67 0.56 0.67 0.67 0.413333 0.578333 0.39
2 BAEA 0 0 0 0.07 0.16 0.27 0.89 0.78 0.78 0.89 0.78 0.78 0.356667 0.563333 0.43
3 BAEA 0 0 0 0.07 0.16 0.27 0.56 0.45 0.45 0.56 0.45 0.45 0.3 0.42 0.471667
4 GOEA 1 0 1 0.14 0.16 0.27 0.34 0.87 0.87 0.34 0.87 0.87 0.218333 0.51 0.511667
5 GOEA 0 1 1 0.14 0.16 0.27 0.98 0.38 0.38 0.98 0.38 0.38 0.158333 0.08 0.55
6 BAEA 0 1 0 0.14 0.14 0.27 0.56 0.54 0.54 0.56 0.54 0.54 0.098333 0.47 0.578333
7 BAEA 1 0 0 0.14 0.14 0.27 0.76 0.978 0.978 0.76 0.978 0.978 0.471667 0.341667 0.453333
8 GOEA 1 1 1 0.12 0.14 0.27 0.9 0.67 0.67 0.9 0.67 0.67 0.528333 0.385 0.261667
9 GOEA 0 0 1 0.16 0.17 0.27 0.12 0.39 0.39 0.12 0.39 0.39 0.578333 0.158333 0.075
10 BAEA 0 0 0 0.16 0.17 0.35 0.23 0.85 0.85 0.23 0.85 0.85 0.563333 0.105 0.11


The following is the script I used:

library(unmarked)

data=read.csv("SDataR.csv")

head(data)

y=data[3:5]

head(y)

siC=data[15:17]  ###Site data Grass, Woods, Water)###

head(siC)

suC=data[6:11]  ###Survey data Date, Time, Temp###

head(suC)

 

So far so good, the next line of script:

data1=unmarkedFrameOccu(y=y,siteCovs=siC,obsCov=suC)

And returns the error message:

Error in validObject(.Object) :

invalid class “unmarkedFrame” object: obsCovData does not have M*obsNum rows

 

If I enter this line of script it will build the data frame without the ObsCov:

 

data1=unmarkedFrameOccu(y=y,siteCovs=siC)

R will build that frame without error.  I just get the error message when I try and build the frame with the ObsCov (observation covariates) and I simply don’t know the reason.  Any help would be greatly appreciated.

 

 

Lastly, I have multiple species in each data set (some data sets 128 species). Can I build all species into one “unmarkedFrameOccu” data frame then call out a species as I need it or do i need a separate unmarkedFrameOccu for each species?

 

Thanks

Brian

Jeffrey Royle

unread,
Dec 11, 2014, 4:32:33 PM12/11/14
to unma...@googlegroups.com
hi Brian,

 you will see in the help file ?unmarkedFrameOccu an example that has 2 observations covariates and they are packaged as an R list. I think if you do that then your example will work.

regards
andy


--
You received this message because you are subscribed to the Google Groups "unmarked" group.
To unsubscribe from this group and stop receiving emails from it, send an email to unmarked+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Bryn Evans

unread,
Jul 27, 2016, 4:11:08 PM7/27/16
to unmarked
Hello,

I apologize for tagging onto an old post, I think I have a similar problem, but even the ?unmarked and ?occu answers are difficult for me to follow (very new to the R environment!)

Is there any example of data that is packaged as an R list that you could share?

I currently have everything in a matrix, but no matter what I name observation level covariates or how I structure them I get "Error in validObject(.Object) : 
  invalid class “unmarkedFrame” object: obsCovData does not have M*obsNum rows."

I attached what I am currently working with, if that is helpful, thanks very much!

-Bryn
Trial Runs with Observation Data 072516.R
Otter Standardized Season 01.csv

Florent Bled

unread,
Jul 27, 2016, 10:27:08 PM7/27/16
to unmarked
Hello Bryn,

The problem you're having comes from the fact that the input you're giving for the observation covariates is not in the proper format.
For occu(), you can do it in 2 ways:

1/ You can either use one dataframe per covariate, with as many rows as sites, and as many columns as observations. If you decide to use this option, you input this as a list of all these dataframes, with the name you plan on using in model formulation as the name for the corresponding dataframe in your list.

2/ You can stack one covariate dataframe on top of the other in a "giant" dataframe with as many columns as observations, and as many rows as the product of your number of covariates, and your number of sites. As specificed in the help file, these will have to be in site-major order.

I personally prefer option 1 as I find it easier to read each covariate matrix individually if needed. In your case you would then use a code along those lines:

umf<-unmarkedFrameOccu(y=detect, siteCovs= ottrz01SiteCovars,
                                              obsCovs= list(cov1=ottrz01ObsCovars[,1:11],
                                                                     cov2=ottrz01ObsCovars[,12:22],
                                                                     cov3=ottrz01ObsCovars[,23:33],
                                                                     cov4=ottrz01ObsCovars[,34:44],
                                                                     cov5=ottrz01ObsCovars[,45:55],
                                                                     cov6=ottrz01ObsCovars[,56:66],
                                                                     cov7=ottrz01ObsCovars[,67:77]
                                                                     )
                                            )

(assuming that you ordered your covariates in ottrz01ObsCovars such that the columns from 1 to 11 correspond to replicates of the first covariate and so on.

Hope it helps!

Florent

Bryn Evans

unread,
Jul 28, 2016, 5:42:44 PM7/28/16
to unmarked
Thank you so much for the speedy reply! I actually couldn't find my post and figured it was lost forever...

Well the good news is I am no longer getting the M*obsNum error! (Been banging my head against a wall trying to set things up as a matrix instead and STILL getting that...)

The bad news is now I am getting this:

Error in unmarkedFrame(y, siteCovs, obsCovs, obsToY = diag(J), mapInfo = mapInfo) : 
  At least one matrix in obsCovs has incorrect number of dimensions.

Do I need to specify somehow that the first row is the label?
They do correspond to the 11 replicates in the order you have shown, thanks for checking

Greatly appreciate your time, I'll be carrying on with it!

-Bryn

Florent Bled

unread,
Jul 28, 2016, 9:58:16 PM7/28/16
to unmarked
Notes: in your CVS file, replace the "." with NA, otherwise R is going to read the columns with these data as factors instead of numbers. Alternatively, you can also load your file specifying what the character string indicating NAs by setting the argument: na.strings="."
However, I would recommend having your raw data properly formatted instead.
Incidently, this is the reason you had to write the line after "# Make unmarked behave" ... because your data were misbehaving. ;)

Anyway, please find below a code that works for me, and is not producing any error. If you're still having troubles, please include a copy of the code up to the command returning the error you're talking about, so I can try to reproduce it.


otter_trz_01<-read.csv("Otter Standardized Season 01.csv", header=T,na.strings=".")

## OTTER FULL SUBSET TRI SEASON 01 PREPARATION ##

# Create data frame of detections
ottrz01Detects<- otter_trz_01[,2:12]

# Create data frame of covariates
ottrz01SiteCovars<-as.data.frame(otter_trz_01[,13:21])

# Create data frame of covariates
ottrz01ObsCovars<-as.data.frame(otter_trz_01[,22:98])


umf<-unmarkedFrameOccu(y=ottrz01Detects, siteCovs= ottrz01SiteCovars,

                                              obsCovs= list(cov1=ottrz01ObsCovars[,1:11],
                                                            cov2=ottrz01ObsCovars[,12:22],
                                                            cov3=ottrz01ObsCovars[,23:33],
                                                            cov4=ottrz01ObsCovars[,34:44],
                                                            cov5=ottrz01ObsCovars[,45:55],
                                                            cov6=ottrz01ObsCovars[,56:66],
                                                            cov7=ottrz01ObsCovars[,67:77]
                                                            )
                       )

fm=occu(~cov1+cov2+cov3+cov4+cov5+cov6+cov7 ~BLR+NMJ+SCR+ZDIST.WATER+ZTREE.COVER+ZMARSH.COVER+ZSLOPE+ZCAM.HEIGHT+ZTREE.DBH, umf)

Florent

Bryn Evans

unread,
Jul 30, 2016, 7:10:20 AM7/30/16
to unmarked
Success!!
Very slight minor tweaking after switching back to the NAs, and behold the matrix reads in without an error!
Really appreciate it Florent, I owe you a beer for sure :)
-Bryn

Sheon

unread,
Mar 22, 2018, 8:35:38 AM3/22/18
to unmarked

 Hi everyone,

I am very new to R and I am struggling to find a solution to this error.
Does anyone have suggestion how can I overcome this issue?
Could it be I am fitting in the wrong columns into my dataframe or something is else is wrong with the way I am fitting my data?

I am trying to look at if the presence/absence of rats (y) is different between roads and forests (treatment) in 8 sites.
I did trapping over 6 continuos trap nights, so it is 6 nights per site of sampling.
I have co-variates of distance from road, species, sex and age.

Thank you so much for helping.

The following are my codes and attached is my csv file:

> ##Add in the detection data rows - columns for detection and site name##
> y<-data[,1:3]
> 
> ##Check to see if y is in the right format##
> ##If yes it is - proceed##
> n<-nrow(data)
> 
> ##Now to add in the site-specific co-variates-treatment,distance from road##
> blgr.site<-data[,8:13]
> 
> ##Check dataframe created##
> str(blgr.site)
'data.frame':	3330 obs. of  6 variables:
 $ Set.distance     : Factor w/ 8 levels "0m","120m","150m",..: 1 6 7 8 2 3 4 5 1 6 ...
 $ Distance.fromroad: int  0 30 60 90 120 150 180 210 0 30 ...
 $ Trap.Status      : Factor w/ 3 levels " NEW","NEW","NILL": 3 3 3 3 3 3 3 3 3 3 ...
 $ Species          : Factor w/ 8 levels "BS","DTT","LTG",..: NA NA NA NA NA NA NA NA NA NA ...
 $ Sex              : Factor w/ 2 levels "F","M": NA NA NA NA NA NA NA NA NA NA ...
 $ Age              : Factor w/ 2 levels "A","SA": NA NA NA NA NA NA NA NA NA NA ...
> head(blgr.site)
  Set.distance Distance.fromroad Trap.Status Species  Sex  Age
1           0m                 0        NILL    <NA> <NA> <NA>
2          30m                30        NILL    <NA> <NA> <NA>
3          60m                60        NILL    <NA> <NA> <NA>
4          90m                90        NILL    <NA> <NA> <NA>
5         120m               120        NILL    <NA> <NA> <NA>
6         150m               150        NILL    <NA> <NA> <NA>
> ##Ok. Proceed##
> 
> #create time factor and use as covariate
> #observation level (time specific) covariates
> blgr.obs<-data[,5:6]
> 
> ##Check the format of the time factor data##
> str(blgr.obs)
'data.frame':	3330 obs. of  2 variables:
 $ Date      : Factor w/ 48 levels "1-May-16","10-Jul-16",..: 21 21 21 21 21 21 21 21 21 21 ...
 $ Trap.Night: int  1 1 1 1 1 1 1 1 1 1 ...
> 
> ### to put everything together in unmarked data frame called blgr##
> blgr<-unmarkedFrameOccu(y=y, siteCovs = blgr.site, obsCovs = blgr.obs)
Error in validObject(.Object) : 
  invalid class “unmarkedFrame” object: obsCovData does not have M*obsNum rows.



Sheon

unread,
Mar 22, 2018, 8:48:31 AM3/22/18
to unmarked

This is my data attached for your reference.
Occu_data1.csv

Nathalie Jreidini

unread,
May 5, 2019, 2:38:23 PM5/5/19
to unmarked
Hi Brian,

I realize this is a pretty old thread, but I was wondering if you could help me out with a similar situation. I am using the package unmarked for the first time, the lines of code are easy to follow, but my problem is figuring out how to format my data to run the model. I have occurrence data for a toad population over 7 years. Would date1 be the first encounter of the individual or the first time we went out to survey? Would consecutive years continue as consecutive date numbers (i.e. 2012 would have dates 1 through 4 for 4 survey nights, then 2013 dates 5 through 8 etc.)? Also, there is no variation between sites as all individuals are spotted along the beach. Can I get ride of the site covariates?

Thank you for your time,
Nat

Tanvi Gurjar

unread,
Nov 16, 2023, 2:36:29 PM11/16/23
to unmarked
Hi everyone, 

I have a problem similar to some of the problems contained in this thread. 
I am trying to fit a single-season single species occupancy model for a frog species. I have a separate .csv file for my encounter histories and a separate file for my covariates. 
I keep running into an error when I try to specify an object for "sitesCovs" or "obsCovs". Following is my code and the error:

"
cov1 <- as.data.frame(Covariates_GBK$WATER_TEMP) #create a dataframe for 'siteCovs'
names(cov1) <- "WTEMP" #name the variable(s) in the dataframe 
GBK2 <- unmarkedFrameOccu(y = GBK_dets, sitesCovs= cov1) #Water temperature

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

To resolve this error, I tried the following:
cov1list <- list(Covariates_GBK=data.frame(Covariates_GBK$WATER_TEMP))
GBK2 <- unmarkedFrameOccu(y = GBK_dets, obsCovs= cov1list) #WTEMP - Covariates_GBK Error in FUN(X[[i]], ...) : At least one element of obsCovs has incorrect number of dimensions.
"
Here is the  data
> print(cov1) WTEMP 1 0.000 2 21.550 3 18.775 4 18.775 5 21.800 6 21.800 7 25.775 8 25.775 9 26.575 10 24.950 11 17.400 12 16.700

When I imported a single covariate (a single column), I did not run into any problems:

cov <- as.data.frame(GBK_Sub$`SUBSTRATE: G(0)/L(1)`) 
names(cov) <- "Substrate" 
GBK1 <- unmarkedFrameOccu(y = GBK_dets, siteCovs = cov) 

The data for this:
> print(cov) Substrate 1 Lim 2 Lim 3 Gyp 4 Gyp 5 Lim 6 Lim 7 Lim 8 Lim 9 Lim 10 Lim 11 Lim 12 Lim

Looking forward to any responses!

Thanking you and best wishes,
Tanvi

Airy González

unread,
Nov 16, 2023, 3:06:37 PM11/16/23
to unma...@googlegroups.com
First, could you provide answers to these questions:
  • How many sites do you have?
  • Maximum visit number per site?
  • What's the row number for your 'siC' table
  • What's the row number for your 'suC'
~Airy

--
*** Three hierarchical modeling email lists ***
(1) unmarked (this list): for questions specific to the R package unmarked
(2) SCR: for design and Bayesian or non-bayesian analysis of spatial capture-recapture
(3) HMecology: for everything else, especially material covered in the books by Royle & Dorazio (2008), Kéry & Schaub (2012), Kéry & Royle (2016, 2021) and Schaub & Kéry (2022)
---
You received this message because you are subscribed to the Google Groups "unmarked" group.
To unsubscribe from this group and stop receiving emails from it, send an email to unmarked+u...@googlegroups.com.


--
Airy Adriana Peralta
​Ph. D. Student​ & Professional Aide
​Ecology and Evolutionary Biology​
Institute of Arctic and Alpine Research - CU Boulder

Tanvi Gurjar

unread,
Nov 16, 2023, 3:45:18 PM11/16/23
to unmarked
Dear Airy, 

Thank you for your response! Yes, surely. 
I have a total of 12 sites with 5 sampling events per site. 
I am not sure what you mean by the row number for my siC and suC tables. The column numbers for my siC table are 7,8,9, 10and  11 while those for my suC table are 6 and 8. 
Below is a snapshot of the data frame containing detection histories and covariates since I am unable to attach any other file formats. 

Thanking you and best wishes, 
Tanvi
data.JPG

Airy González

unread,
Nov 16, 2023, 4:34:32 PM11/16/23
to unma...@googlegroups.com
Tanvi,
If you have 12 sites with five visits per site, 

your site covariates dataframe should have 12 records (12 rows in a data frame; one per site) and five columns (columns 7,8,9,10,11).

Your obsCov covariates data frame should have 12*5 rows (60 rows) regardless of whether you missed a visit to a given site if that is the case. Also, it should contain two columns (6 and 8, as per your answer). Is this the case? If you are missing records, that would explain the error you are getting. If you missed visits to a given site, then you just populate the entries with NA.

Also, speaking from my ignorance of bird surveying. Why would you expect substrate and O2 to affect detection? Shouldn't you consider other covariates like 'observer,' "time of the day."

~Airy

Tanvi Gurjar

unread,
Nov 17, 2023, 9:59:39 AM11/17/23
to unmarked
Dear Airy, 

Thank you! I had included averaged values for all the covariates, but reformatting according to your advice solved the problem!

About the nature of the data: I sampled amphibians not birds, therefore, the covariates! :)

Thanking you and best wishes, 
Tanvi

Airy González

unread,
Nov 17, 2023, 2:41:12 PM11/17/23
to unma...@googlegroups.com
You're welcome!

Ah! I thought you were working with birds. It makes sense then.
Cheers,
~Airy

Reply all
Reply to author
Forward
0 new messages