Reputation: 5898
Given a glm model reduced
containing variable of interest varofint
and an arbitrary number of additional variables v1
... v[n]
of class numeric
, integer
, or factor
(some of which may be interactions with each other), and a glm model full
that contains all terms in reduced
as well as an additional term which is the interaction of varofint
and exactly one of the additional variables v[i]
(this interaction specified using a :
or *
in the formula for full
), how can I extract the name of v[i]
? I'd prefer solutions that don't rely on regular expressions but rather use the model structure itself.
In the example code, v[i]
is named "interaction_var"
and that character vector ("interaction_var
") is the return value that I want.
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)
Here's the start of a solution using sub
which I can't figure out how to finish and makes some assumptions that I don't like:
addl_terms <- setdiff(names(coef(full)),names(coef(reduced)))
varname_pluslevel <- sub(".+?:",replacement="", addl_terms[1])
Upvotes: 1
Views: 73
Reputation: 44320
It seems like the term.labels attribute of the terms field in full provides more straightforward access to the interacted variable name:
term.labels.reduced = attr(reduced$terms, "term.labels")
term.labels.full = attr(full$terms, "term.labels")
interaction = setdiff(term.labels.full, term.labels.reduced)
strsplit(interaction, ":")[[1]][2]
# [1] "interaction_var"
Upvotes: 1