'MuMIn::dredge(). Exclude models only including main effect, keep only models with interactions
Is it possible to make a general logical statement for subsetting away (exclude) all potential models that only include the main effect of e.g. A?
y ~ A + B + C + A:C + A:B
For models including A, only include those where A is a part of an interaction (as the relationship y~A would make no sense by itself).
Solution 1:[1]
An inelegant solution I arrived at (after noticing how dredge() worked with poly() terms; including or excluding the entire n x power matrix, not the individual columns) was to create a little function called "int" to create a similar matrix out of two predictors and their interaction. I called the elements produced "a", "b", and "a:b" following the convention of poly(), which calls its two elements "1" and "2".
int = function(a,b){
matOut = matrix(c(a,b,a*b),nrow=length(a))
dimnames(matOut) =list(NULL,c("a","b","a:b"))
return(matOut)}
Let's use the mtcars data set as an example, using "carb" instead of A, "gear" instead of B, and "am" instead of C to predict "mpg". Starting with the standard approach, we might try this.
require(MuMIn)
data(mtcars)
fm = lm(mpg ~ carb*gear + carb*am, mtcars,na.action=na.fail)
length(dredge(fm))
This produces 11 possible models with all combinations of the three linear terms and the two interaction terms.
Now, if we use int() instead of *, we see we only get four models; each interaction, their combination, and the null model. All models are excluded that contain linear terms without the interaction.
fm = lm(mpg ~ int(carb,gear) + int(carb,am), mtcars,na.action=na.fail)
length(dredge(fm))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
