# Interact categorical variables in GLM in R

I am trying to predict child nutrition (binary) using a set of variables. The two that I want to interact are maternal education (none, primary, middle, HS) and wealth quintile (1,2,3,4,5). Thus far I tried:

form <- nutrition ~ maternal_ed + wealth_q + maternal_eq * wealth_q + other_covars
model.results <- glm(form, data=d, family=quasibinomial)


I am only getting one value in model.results for the interaction, which reads 'mat_ed_level:wealth_q'. I am unsure how to use or interpret this value. I am somewhat unfamiliar with the use of interaction terms, so any information about the interpretation of the (correct) output would be greatly appreciated. Thanks,

• Does R know wealth quintile is a factor? – mnel Nov 30 '12 at 1:30
• Not sure what you mean -- how would I specify that? Right now, they are both numeric with values 1,2,3,4 and 1,2,3,4,5 – mike Nov 30 '12 at 1:40

## 1 Answer

From your comments, it appears that you have not specified to R that these two variables are categorical. (factor variables in R). Given they have the appearance of numeric (continuous) variables, R will assume they are, and fit the model as if they were continuous.

To convert to factor variables (with your data.frame d)

d$wealth_q <- factor(d$wealth_q)
d$maternal_q <- factor(d$maternal_q)


Also, your formula is somewhat redundant

~maternal_eq * wealth_q expands to main effects + interactions

So the following should work

form <- nutrition ~ maternal_eq * wealth_q + other_covars
model.results <- glm(form, data=d, family=quasibinomial)

• Thanks for the response -- is there any drawback to defining the categories are factors if they are ordered and (likely) equidistant from one another? – mike Nov 30 '12 at 1:58
• I think not defining them as factors would be far more dangerous Do you expect the effect comparing no education to primary to be the same as comparing middle to high school and exactly half the effect of comparing primary and high school. They are categorical data, not continuous, so you need to code them as such and model appropriately. You could go down the path of ordered factors if you wished, but I think that would add more assumptions to your model – mnel Nov 30 '12 at 2:07
• R also has "ordered factors" for handling this sort of case. I don't believe that glm treats ordered factors differently from regular factors, but other regression packages (such as gbm) do. Ordered factors are described on this page. – David J. Harris Nov 30 '12 at 7:56
• @PeterFlom There is a package gbm. – Roland Nov 30 '12 at 21:18
• @PeterFlom ?? searches only the help files of installed packages. It would find package names (try ??ggplot2, which you probably have installed). You could have used RSiteSearch('gbm'). – Roland Dec 3 '12 at 8:43