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?
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.
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.
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.
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.