Skip to content
Prev 243977 / 398506 Next

lm() and interactions in model formula for x passed as matrix

On Dec 5, 2010, at 3:19 PM, William Simpson wrote:

            
The formula syntax in R allows you to specify interactions with the  
"^" operator but some testing makes me think you cannot use either y  
~ .^3 or y ~ X^3 with matrix data arguments, here assuming you only  
want interaction up to third order.

Assuming you know how to use do.call("cbind", varlist)
perhaps:

  form = as.formula( paste("y ~ (",
                            paste(colnames(X), collapse="+"),
                              ")^3", sep="")  )
lm(form)


--- output------
Call:
lm(formula = form)

Coefficients:
(Intercept)           x1           x2           x3        x1:x2         
x1:x3
   -0.383296    -0.333429     0.003976     0.332982    -0.001130      
0.100698
       x2:x3     x1:x2:x3
    0.366745     0.122111