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
How to Make a Matrix from a List of Vectors in R
Diagnosing R Package Build Warning: "Latex Errors When Creating PDF Version"
Ggplot Geom_Point() with Colors Based on Specific, Discrete Values
Row/Column Counter in 'Apply' Functions
Cor Shows Only Na or 1 for Correlations - Why
How to Run an 'R' Script Without Suppressing Output
Combining Pivoted Rows in R by Common Value
Compare Two Character Vectors in R
How to Automatically Include All 2-Way Interactions in a Glm Model in R
Clipping Raster Using Shapefile in R, But Keeping the Geometry of the Shapefile
Where Should I Put Data for Automated Tests with Testthat