I'm writing a function that accepts a full and a reduced glm object to summarize interaction results for a variable of interest varofint and the interaction variable interaction_var (by performing a lrtest and using svycontrast on the full object to extract results for varofint for each level of interaction_var). Sample data:
x <- data.frame(outcome=rbinom(100,1,.3),varofint=rnorm(100), interaction_var=sample(letters[1:3],100,replace=TRUE))
reduced <- glm(outcome~varofint+interaction_var,data=x)
full <- glm(outcome~varofint*interaction_var,data=x)
I'd like to know the best way to extract a reference category for said (full) glm model. I could obviously do something like
levels(full$data$interaction_var)[1]
but would this be a "safe" method to extract a reference category given inputs to the contrasts argument? It seems like, given the option to select SAS contrast, this method could produce a level of interactionv_var that isn't the one used as a reference category in the model. Would the following be safer?
mf <- model.frame(full)
setdiff(rownames(contrasts(mf[, "interaction_var"])), colnames(contrasts(mf[, "interaction_var"])))
or similarly
names(which(apply(contrasts(mf[, "interaction_var"]),1,function(.v){all(.v==0)})))
Am I missing a simpler way to extract the reference category?