0

I'm trying to code a linear mixed effects model from data with 48 observations of 6 variables, with survival as a response to parent environment and offspring environment as fixed effects, and population as a random effect. There are three populations which I have dummy coded into two variables.

EDIT: Here's a subset of my original data.

> dput(head(survivaldata,4))
structure(list(pop3 = c(0L, 0L, 0L, 0L), pop5 = c(1L, 1L, 1L, 
1L), parent.env = c(0L, 0L, 0L, 0L), env = c(1L, 1L, 1L, 1L), 
    survived = c(33L, 29L, 29L, 50L), died = c(17L, 21L, 21L, 
    0L)), .Names = c("pop3", "pop5", "parent.env", "env", "survived", 
"died"), spec = structure(list(cols = structure(list(pop3 = structure(list(), class = c("collector_integer", 
"collector")), pop5 = structure(list(), class = c("collector_integer", 
"collector")), parent.env = structure(list(), class = c("collector_integer", 
"collector")), env = structure(list(), class = c("collector_integer", 
"collector")), survived = structure(list(), class = c("collector_integer", 
"collector")), died = structure(list(), class = c("collector_integer", 
"collector"))), .Names = c("pop3", "pop5", "parent.env", "env", 
"survived", "died")), default = structure(list(), class = c("collector_guess", 
"collector"))), .Names = c("cols", "default"), class = "col_spec"), row.names = c(NA, 
4L), class = c("tbl_df", "tbl", "data.frame"))

When I assign the variables to objects first:

> survival <- cbind(survived,died)
> population <- cbind(pop3,pop5)

And then run the model with the following syntax:

> m1 <- glmer(survival ~ parent.env + env + parent.env:env + (1|population), family = binomial)

I get this error:

Error in `[[<-.data.frame`(`*tmp*`, i, value = c(1L, 1L, 1L, 1L, 1L, 1L,  : 
replacement has 96 rows, data has 48

However, when I don't assign the objects first and run the model like this:

> m1 <- glmer(cbind(survived,died) ~ parent.env + env + parent.env:env + (1|cbind(pop3,pop5)), family = binomial)

I get this error instead:

Error: (p <- ncol(X)) == ncol(Y) is not TRUE

All the solutions I've looked up for these errors involve properly accounting for NAs in the data, but my data set doesn't have any NAs. I'm new to R and GLMMs so any help would be greatly appreciated. Thanks.

EDIT: I ran the following code based on suggestion:

> survivaldata$population <- with(survivaldata,paste0(pop3,pop5))

> m1 <- glmer(survival ~ parent.env + env + parent.env:env + (1|survivaldata$population), family = binomial)

And now I get this error instead:

Error in model.frame.default(drop.unused.levels = TRUE, formula = survival ~  : 
  variable lengths differ (found for 'parent.env')

EDIT: Never mind, I just had survival defined improperly. Problem is fixed now, thanks.

  • Please show us your data. See http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example. –  Mar 25 '17 at 21:51
  • what is `(1|cbind(pop3,pop5))` doing on the right-hand side of the formula? that seems weird. Can you explain what it's supposed to represent in the model? What are `pop3` and `pop5`, and what are you trying to do in combining them in that way? – Ben Bolker Mar 26 '17 at 00:47
  • @BenBolker Hi, yes sorry I can explain this better. In my dataset there are three populations; 1, 3 and 5. I've dummy coded it so population 1 is pop3 = 0 and pop5 = 0, population 3 is pop3 = 1 and pop5 = 0, and population 5 is pop3 = 0 and pop5 = 1. I combined them with cbind so that the population identity would be a single object. That part of the formula is supposed to be incorporating the population as a random effect in the model. Pehaps I've not done it properly? – Marcus Hibbins Mar 26 '17 at 00:57
  • `cbind` won't work. Try `paste0(pop3,pop5)` instead (probably best to use `survivaldata$population <- with(survivaldata,paste0(pop3,pop5))` – Ben Bolker Mar 26 '17 at 01:19

0 Answers0