chfs
chfs

Reputation: 57

How to describe regression interaction terms in compact form?

I have regressors a, b, c, e_1 and e_2 and I want to run the following regression:

y ~ a + b + c + e_1 + e_2 + a.e_1 + a.e_2 + b.e_1 + b.e_2 + c.e_1 + c.e_2

Each of the e_1 and e_2 has to interact with a, b and c variables.

I actually have 5 e's and 5 other variables. What would be the quickest way to write the code instead of writing individually a:e_1, a:e_2 etc.

Upvotes: 2

Views: 60

Answers (2)

Ben Bolker
Ben Bolker

Reputation: 226047

How about ~(a+b+c)*(e1+e2) ?

Example:

dd <- data.frame(a=1,b=1,c=1,e1=1,e2=1)
colnames(model.matrix(~(a+b+c)*(e1+e2),dd)
##  [1] "(Intercept)" "a"           "b"           "c"           "e1"         
##  [6] "e2"          "a:e1"        "a:e2"        "b:e1"        "b:e2"       
## [11] "c:e1"        "c:e2"     

For a larger set of variables, an alternative to @ThomasIsCoding's solution:

pfun <- function(x) paste("(", paste(x,collapse="+"), ")")
reformulate(paste(pfun(p), "*", pfun(q)), response="y")

Upvotes: 1

ThomasIsCoding
ThomasIsCoding

Reputation: 101024

If you have know the grouped names of variables for interactive parts in the formula, like p and q below

p <- c("a","b","c")
q <- c("e_1","e_2")

you can try the following code to generate the whole formula using as.formula and paste0, i.e.,

f <- as.formula(paste0("y ~",paste0(c(p,q,do.call(paste, c(expand.grid(p,q),sep = ":"))),collapse = "+")))

such that

> f
y ~ a + b + c + e_1 + e_2 + a:e_1 + b:e_1 + c:e_1 + a:e_2 + b:e_2 + 
    c:e_2

and

> class(f)
[1] "formula"

Upvotes: 1

Related Questions