Mixed Model Anova Error


I am trying to run a 3 way mixed model anova using my data (Sex x PCB x Day_2) and the lme4 package.

This was my initial code:

Pref_df <- read_xlsx("~/Documents/D_DSacBotStat.xlsx", sheet = "Pref")
MMA_D2 <- lmer(Pref_df$Day_2 ~ PCB Treat* Sex + 1|id, data = D_DSacBotStat )

Error in model.frame.default(data = D_DSacBotStat, drop.unused.levels = TRUE, :
variable lengths differ (found for 'PCB Treat')

So I adjusted the file and changed the name to PCB_treatment and had this code:

MMA_D2 <- lmer(Pref_df$Day_2 ~ Pref_df$PCB_Treatment * Pref_df$Sex + 1|id, data = D_DSacBotStat )

But I was met with this error:

Error in model.frame.default(data = D_DSacBotStat, drop.unused.levels = TRUE, :
invalid type (closure) for variable 'id'

This is the portion of the data I am trying to use (image attached)

Also I am fairly new to R, although I cannot seem to figure out what 1 | id means in the code and if that could be the source of the problem because maybe I am not using it correctly as it invalid type (closure) for variable id?

Any help would be greatly appreciated!

Thank you!

I have not used lmer but assuming the use of formulas in it is similar to other cases, I suggest you try

MMA_D2 <- lmer(Day_2 ~ PCB_Treatment * Sex + 1|Animal_ID, data = Pref_df )

Things to note:
There is no need to include Pref_df$ in the names of the variables in the formula if the Pref_df data frame is named as the data.
Judging from other functions I use, the 1|id expression in the formula signifies an independent intercept for each value of id. But your data do not have an id variable. I guessed that this should be Animal ID, which is in your data. I also changed the column name to Animal_ID in the formula. Spaces in column names cause more trouble than they are worth. That data would have to be changed to match.
I do not see an object named D_DSacBotStat in your data before it appears as the data parameter in the call to lmer. I changed this to Pref_df from which it seems you data are coming.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

I think you have a data leak, if Animal_id indicates an individual (and not an attribute of an individual). Then you could fit a perfect model , just on the animal_id, as you could directly record a Day_2 value for each unique ID.

This gives me:

MMA_D2 <- lmer(Day_2 ~ PCB_Treatment*Sex + 1|Animal, data = Pref_df)
Error: number of levels of each grouping factor must be < number of observations

I changed the column names to make it easier, however is there something I need to change within the actual file as it says (I am guessing) that the number of observations are too large.

Thank you!

try omitting animal_id from your model formula, so that it isnt considered as an independent variable for fitting your model.

Animal_ID does indicate an individual, is there any way to fix this if it is a data leak (adjust my file)? I'm sorry I am new to R so I am still trying to work my way around the error messages.

Thank you!