Error in knitting markdown, but not interactive R


I'm trying to knit a glm in RMarkdown and I'm running into an issue. The console throws an error when trying to create the final model while knitting to html. However, when I run this piece of code in a regular script in R, I don't receive any errors.

the GLM run:

# Finished Model
summary(model <- glmer(code ~ blocktype*cond + level
                               + (1+ blocktype + cond | subject),
                               data=data, family="binomial"), cor = FALSE)
finalmodel = model

Error message:

Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'object' in selecting a method for function 'summary': Invalid grouping factor specification, subject

I checked and I don't have any missing values/NA values in my dataset. I have subject (labeled from 1-30), level (-1-1), code (0, 1) as numeric variables and the rest (blocktype and cond) are factor variables.

I'm wondering if this is an error with RMarkdown specifically since this error doesn't occur when run in the normal console or if it's a data-related problem since it says "invalid grouping factor specification"?

Any help is appreciated, let me know if you need any further information. Thank you!

It's a healthy attitude to assume first that the problem is with your code, and exhaust every possibility until you even begin to consider that the package/software has a bug.

Are you knitting the document? Knitting starts with a fresh R session, while running a chunk evaluates in your current session & environment. It could be that you have a data frame "data" in memory that the chunk is able to work on, but upon knitting, the sequence of chunks produce a "data" that is different from the one you have in memory.

I would recommend running the notebook with the option "Restart R and run all chunks" to make it behave just like knitting the document would and see if you get that same error. If so, you'll better be able to debug it from that point because you'll have your objects in memory.

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.