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.