Issue with "array" in yearlySiteCovs when using unmarkedMultFrame

27 views
Skip to first unread message

Saâd HANANE

unread,
Mar 1, 2026, 7:52:52 AM (3 days ago) Mar 1
to unma...@googlegroups.com
Dear unmarked community,

I am currently using the unmarked package to create a unmarkedMultFrame for analyzing wildlife data. I am facing an issue where the yearlySiteCovs object, which I am passing as one of the arguments, contains elements that appear as "arrays" rather than matrices, despite my efforts to convert them.

Here's the context:
yearlySiteCovs contains three periods (period1, period2, period3), each having dimensions 368x18.
However, when I check the class of each element in yearlySiteCovs, I see that they are reported as "arrays" instead of matrices.
Even after using functions like as.matrix() and drop(), the elements are still identified as arrays, which is causing issues when I try to run the unmarkedMultFrame model.

Example script:
# My yearlySiteCovs is a list containing three periods
yearlySiteCovs <- list(
  period1 = matrix(c(1, 2, 3, 4), nrow = 368, ncol = 18),  
  period2 = matrix(c(2, 3, 4, 5), nrow = 368, ncol = 18),  
  period3 = matrix(c(3, 4, 5, 6), nrow = 368, ncol = 18)  
)
# Attempt to convert to matrix
yearlySiteCovs <- lapply(yearlySiteCovs, function(x) {
  if (is.array(x)) {
    x <- as.vector(x)  # Flatten the array
    x <- matrix(x, nrow = 368, ncol = 18, byrow = TRUE)  
  }
  return(x)
})

sapply(yearlySiteCovs, function(x) c(class(x), dim(x)))
    period1  period2  period3
[1,] "matrix" "matrix" "matrix"
[2,] "array"  "array"  "array"
[3,] "368"    "368"    "368"  
[4,] "18"     "18"     "18"    

Despite these efforts, the issue persists, and I receive the error:

"At least one element of yearlySiteCovs has an incorrect number of dimensions."

What I've tried:
I have used as.matrix(), drop(), and other standard methods to flatten and convert the arrays into matrices.
I verified the dimensions of all the objects in yearlySiteCovs, and they all appear to have the correct dimensions (368 rows and 18 columns).
I confirmed the format of y_matrix_processed, siteCovs, and obs_covs, all of which are correctly formatted.
Could anyone help me understand why the "array" issue persists, and how I can resolve it to ensure that yearlySiteCovs works correctly with unmarkedMultFrame?

Any insights or suggestions would be greatly appreciated!

Thank you in advance!
---
Saâd Hanane, PhD
Service d'Ecologie, de Biodiversité et de Conservation des Sols
Centre de Recherche Forestière
Chariae Omar Ibn Al Khattab, BP 763, Rabat-Agdal/Maroc.



Ken Kellner

unread,
Mar 1, 2026, 8:01:12 AM (3 days ago) Mar 1
to unmarked
A matrix is a special type of array in R, so if you run class() on a matrix you get back both matrix and array (as seen in your call to sapply). That's OK and shouldn't be causing the issue here.

The error you are getting comes from one of two checks: 
1. does the number of columns of the yearlySiteCov match the number of primary periods (numPrimary)?
2. does the number of rows of the yearlySiteCov match the number of sites (i.e., number of rows of y?)

Double check those two things, especially #1 - what is the numPrimary value you are setting when creating the unmarkedMultFrame? Is it actually 18 or some other value? We can't tell based on the code you shared.

Ken

Saâd HANANE

unread,
Mar 2, 2026, 3:33:21 AM (2 days ago) Mar 2
to unma...@googlegroups.com
Hi Ken Kellner,

Thank you for your quick answer.
Yes, we have found the problem as you've advised. However, we still stuck to converting our matrix from 368/18(culums) to 368/3(culums).
We made this code:

# Load effort files
eff_files <- c("ppdur.txt", "pplist.txt", "ppdis.txt", "pplg.txt", "ppobs.txt") # those effort variable files 

# Read the effort files
eff_data <- lapply(eff_files, function(file) {
  read.table(file, header = TRUE)
})

# Checking the dimensions of each file
dim(eff_data[[1]])  # Cela devrait donner 368 18
dim(eff_data[[2]])  # Cela devrait donner 368 18
dim(eff_data[[3]])  # Cela devrait donner 368 18
dim(eff_data[[4]])  # Cela devrait donner 368 18
dim(eff_data[[5]])  # Cela devrait donner 368 18

# Divide the files into periods
eff1_period1 <- eff_data[[1]][, 1:6]  # Period 1 (month 1 à 6)
eff1_period2 <- eff_data[[1]][, 7:12]  # Period 2 (month 7 à 12)
eff1_period3 <- eff_data[[1]][, 13:18] # Period 3 (month 13 à 18)

eff2_period1 <- eff_data[[2]][, 1:6]
eff2_period2 <- eff_data[[2]][, 7:12]
eff2_period3 <- eff_data[[2]][, 13:18]

eff3_period1 <- eff_data[[3]][, 1:6]
eff3_period2 <- eff_data[[3]][, 7:12]
eff3_period3 <- eff_data[[3]][, 13:18]

eff4_period1 <- eff_data[[4]][, 1:6]
eff4_period2 <- eff_data[[4]][, 7:12]
eff4_period3 <- eff_data[[4]][, 13:18]

eff5_period1 <- eff_data[[5]][, 1:6]
eff5_period2 <- eff_data[[5]][, 7:12]
eff5_period3 <- eff_data[[5]][, 13:18]

# Create yearlySiteCovs with 3 periods for each effort variable
eff1_yearly <- cbind(eff1_period1, eff1_period2, eff1_period3)
eff2_yearly <- cbind(eff2_period1, eff2_period2, eff2_period3)
eff3_yearly <- cbind(eff3_period1, eff3_period2, eff3_period3)
eff4_yearly <- cbind(eff4_period1, eff4_period2, eff4_period3)
eff5_yearly <- cbind(eff5_period1, eff5_period2, eff5_period3)

# Create the yearlySiteCovs list
yearlySiteCovs <- list(
  eff1 = eff1_yearly,
  eff2 = eff2_yearly,
  eff3 = eff3_yearly,
  eff4 = eff4_yearly,
  eff5 = eff5_yearly
)

# Verification of dimensions after the creation of yearlySiteCovs
sapply(yearlySiteCovs, dim)  # Cela doit donner des matrices de 368 lignes et 3 colonnes

   eff1 eff2 eff3 eff4 eff5
[1,]  368  368  368  368  368
[2,]   18   18   18   18   18 this should be (3) 

If you can help.....

Thank you.

--
*** 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.
To view this discussion visit https://groups.google.com/d/msgid/unmarked/54835b55-4256-40c0-ade4-2f263eb0f823n%40googlegroups.com.


--

Ken Kellner

unread,
Mar 2, 2026, 7:47:51 AM (2 days ago) Mar 2
to unma...@googlegroups.com
There's not necessarily a single right answer here.

It seems like your covariate represents effort at the observation level. If that's the case it may make more sense to include it, with the existing dimensions, as an observation-level covariate (obsCovs) instead of a primary period (yearlySiteCovs) level covariate.

If you really want to include it as a yearlySiteCov, then one solution would be, for each site x primary period, use the average value of the effort covariates for all observations in that site x primary period (I guess there would be 6 of them per primary period). That would give you a matrix with the correct dimensions of 368x3. This may or may not make sense for your system.

Ken
> [2,] *18 18 18 18 18 this should be (3) *
> >> *[2,] "array" "array" "array" *
> >> [3,] "368" "368" "368"
> >> [4,] "18" "18" "18"
> >>
> >> Despite these efforts, the issue persists, and I receive the error:
> >>
> >> "*At least one element of yearlySiteCovs has an incorrect number of
> >> dimensions.*"
> > <https://groups.google.com/d/msgid/unmarked/54835b55-4256-40c0-ade4-2f263eb0f823n%40googlegroups.com?utm_medium=email&utm_source=footer>
> > .
> >
>
>
> --
> Saâd Hanane, PhD
> Service d'Ecologie, de Biodiversité et de Conservation des Sols
> Centre de Recherche Forestière
> Chariae Omar Ibn Al Khattab, BP 763, Rabat-Agdal/Maroc.
>
> --
> *** 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.
> To view this discussion visit https://groups.google.com/d/msgid/unmarked/CA%2BQAJT6v-KjJV4S0a_OLGZUWXOfS-oXZKNxEW3TPrzTdMNJYAw%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages