Force specific interactions in Package 'earth' in R

622 views Asked by At

I am using the 'earth' package to construct a Multivariate Adaptive Regression Spline model. Using the earth function, is there a way to allow interactions ONLY between certain predictor variables?

For example, if I want to model y as a function of the predictor variables a, b, c, and d with 2 degrees of interaction, can I allow b to only interact with c and d, NOT a? I recognize that there is the "allowable" input in the earth function, but from my understanding the "allowable" input only specifies if the predictor is allowed to interact, not specifically which other predictors it can interact with.

Note that I am new to using MARS models and this R package, so any help is greatly appreciated.

1

There are 1 answers

0
Stephen Milborrow On

Here is an example that allows only the specified interactions. Predictors in PREDICTORS are allowed to interact with predictors in PARENTS, and no other interactions are allowed:

library(earth)
data(etitanic)

PREDICTORS <- c("age")
PARENTS    <- c("survived", "parch")

example5 <- function(degree, pred, parents, namesx)
{
    if (degree < 2)
        return(TRUE)
    predictor <- namesx[pred]
    parents   <- namesx[parents != 0]
    if((any(predictor %in% PREDICTORS) && any(parents %in% PARENTS)) ||
       (any(predictor %in% PARENTS)    && any(parents %in% PREDICTORS))) {
        return(TRUE)
    }
    FALSE
}
a5 <- earth(sex~., data=etitanic, degree=2, allowed=example5)