Asterisk (*) VS. Colon (:) in R Formulas

Asterisk (*) vs. colon (:) in R formulas

From help(formula):

 In addition to ‘+’ and ‘:’, a number of other operators are useful
in model formulae. The ‘*’ operator denotes factor crossing:
‘a*b’ interpreted as ‘a+b+a:b’.

Use colon between two characters as a regressor in lm()

As noted in the comments above, : denotes an interaction term between regressors. If you want to consider each regressor on its own and the interaction, then you can use x1*x2 which is the same as x1 + x2 + x1:x2.

Getting coefficients for explanatory variables not in the formula

According to ?formula

The * operator denotes factor crossing: a*b interpreted as a+b+a:b

Based on the comment, if we want only interaction, then use a different symbol

The terms themselves consist of variable and factor names separated by : operators. Such a term is interpreted as the interaction of all the variables and factors appearing in the term.

Asterisk (*) vs. colon (:) in R formulas

From help(formula):

 In addition to ‘+’ and ‘:’, a number of other operators are useful
in model formulae. The ‘*’ operator denotes factor crossing:
‘a*b’ interpreted as ‘a+b+a:b’.

Linear model in R - Multiplication Expression

If you just want a single term that is the literal product of the two variables, not an interaction, you can use I():

Model1 <- lm(MyData$A ~ I(MyData$B * MyData$C))

I think in practice, with 2 numeric variables, this ends up the same as Dan's suggestion to use x1:x2 to get just the interaction without the terms for each individual predictor, but it might differ in other cases.



Related Topics



Leave a reply



Submit