CodexBloom - Programming Q&A Platform

best practices for 'scenarios in eval(predvars, data, env)' when using glm with interaction terms in R?

👀 Views: 289 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-05
glm interaction dataframe r R

I've been working on this all day and This might be a silly question, but I've searched everywhere and can't find a clear answer. I'm working with an scenario when trying to fit a generalized linear model (GLM) with interaction terms in R using the `glm()` function. The model seems to work fine without interaction, but when I include them, I get the following behavior message: `behavior in eval(predvars, data, env) : object 'X' not found`. Here's the model I'm trying to fit: ```r model <- glm(Y ~ A * B, data = my_data, family = 'binomial') ``` Where `Y` is a binary response variable, and `A` and `B` are both factors. My data frame `my_data` looks like this: ```r my_data <- data.frame( Y = sample(c(0, 1), size = 100, replace = TRUE), A = as.factor(sample(1:3, size = 100, replace = TRUE)), B = as.factor(sample(1:4, size = 100, replace = TRUE)) ) ``` I've double-checked that both `A` and `B` are properly formatted as factors, and I've confirmed that `my_data` contains no missing values. However, the moment I try to add the interaction term, the behavior occurs. I've also tried explicitly converting the interaction using `interaction(A, B)` in the formula: ```r model <- glm(Y ~ interaction(A, B), data = my_data, family = 'binomial') ``` but the same behavior continues. Could this be an scenario with how R evaluates the formula, or is there something specific about my data setup that could be causing this? Would appreciate any help in troubleshooting this scenario! My development environment is Ubuntu. My development environment is Windows. Any help would be greatly appreciated!