pitakakariki / simr

Power Analysis of Generalised Linear Mixed Models by Simulation
69 stars 19 forks source link

Error with makeGlmer: Invalid grouping factor specification, Subject #168

Open JCruk opened 4 years ago

JCruk commented 4 years ago

I am trying to do some sample size estimation for a glmer model. I have pulled the fixed and random effects from the actual fitted model, but when I try to specify a new model with makeGlmer, I get the above error and it does not complete.

Example:

# Specify required packages
Pkgs <- c( "tidyverse", "lme4", "lmerTest","simr")

# Load packages
lapply(Pkgs, require, c = T)

# Make a frame of predictors
Sub_Groups <- data.frame(Subject = factor(c(1:28)),
                                    Group = factor(c(rep(1, 14), rep(2,14))))

Cat_Cond <- data.frame(expand.grid(Category = factor(c("A", "B", "C")),
                       Condition = factor(c("D", "E", "F", "G", "H"))))

Sim_Data <- merge(Sub_Groups, Cat_Cond, by = NULL)

# Specify needed argument values
b <- c(1.07018921860115,
       0.0493144807004141,
       -0.0417342373911914,
       0.0151701510233487,
       0.10810610398453,
       0.544295737183165,
       0.728323729029262,
       0.0171335499356261) 
V1 <- 0.01261291 # random intercept variance
s <- 1.6190 # residual standard deviation

# Usage
#makeGlmer(formula, family, fixef, VarCorr, data)

summary(Mod_Sim <- makeGlmer(y ~
                               Condition + 
                               Category + 
                               Group + 
                               (1|Subject), 
                             family = gaussian(link = "log"),
                             fixef = b, 
                             VarCorr = V1,
                             data = Sim_Data))

This results in the error and warnings:

Error: Invalid grouping factor specification, Subject
In addition: Warning messages:
1: In setParams(object, newparams) :
  some parameters not specified in setParams()
2: In rnorm(nsim * length(ftd), ftd, sd = sigma(object)) : NAs produced

Is there a way to find out what parameters are missing from the makeGlmer call and pass them?

The predictors appear to be fine:

str(Sim_Data)
'data.frame':   420 obs. of  4 variables:
 $ Subject  : Factor w/ 28 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
 $ Group    : Factor w/ 2 levels "1","2": 1 1 1 1 1 1 1 1 1 1 ...
 $ Category : Factor w/ 3 levels "A","B","C": 1 1 1 1 1 1 1 1 1 1 ...
 $ Condition: Factor w/ 5 levels "D","E","F","G",..: 1 1 1 1 1 1 1 1 1 1 ...

Everything works as expected with makeLmer

## Try as lmer
summary(Mod_Sim <- makeLmer(y ~
                               Condition + 
                               Category + 
                               Group + 
                               (1|Subject),
                             fixef = b, 
                             VarCorr = V1,
                            sigma = s,
                             data = Sim_Data))