I have many formulas (of the formula
or formula
class) of the form y ~ a*b
, where a
and b
are factors.
I need to write a function that takes such a formula, and returns a formula with all the members in the "spelled out" relationship. Here is an example:
fac1 <- factor(c('a', 'a', 'b', 'b')) fac2 <- factor(c('c', 'd', 'c', 'd')) BigFormula(formula(x ~ fac1*fac2))
where BigFormula
returns a formula(x ~ a + b + c + d + a:c + a:d + b:c + b:d)
.
Is there an easy way to do this?
(Context: I run many commands of the form anova(mod1, mod2)
, where mod2
nests in mod1
, and where the right-hand side of both models contains terms like fac1*fac2
. The command is to calculate the F statistics. The problem is that anova
considers fac1*fac2
as three variables, although it usually represents more than three variables. (In the above code, for example, fac1*fac2
represents eight variables.) As a result, anova
underestimates the number of constraints in the nested model and overestimates my degrees of freedom.)
source share