1.3 Interactions and non-linear effects
1.3.1 Interactions
\[ y_i = \beta_0 + \beta_1 x_{1i} + \beta_2 x_{2i} + \beta_3 x_{1i}x_{2i} + \epsilon_i \]
We can specify the interaction between two predictors by adding an interactions
list to the parameters list. Interactions can then be specified between two named variables using “:”. Interactions can be specified between two predictors at the same level, or at different hierarchical levels.
<- simulate_population(
squid_data n=2000,
response_name = "body_mass",
parameters=list(
observation=list(
names=c("temperature","rainfall"),
beta = c(0.5,0.3)
),residual=list(
vcov=0.3
),interactions=list(
names=c("temperature:rainfall"),
beta = c(-0.1)
)
)
)
<- get_population_data(squid_data)
data head(data)
## body_mass temperature rainfall residual temperature:rainfall squid_pop
## 1 0.3684694 1.933232214 0.19203892 -0.61863284 0.3712558292 1
## 2 -0.4730299 0.081011530 -1.32092129 -0.12796029 -0.1070098551 1
## 3 -1.1576776 0.088942791 -0.70727428 -0.99625744 -0.0629069479 1
## 4 0.1311283 0.004136626 -0.08379699 0.15416441 -0.0003466369 1
## 5 0.1344398 0.296427406 -0.27937107 0.06175613 -0.0828132420 1
## 6 -1.4594139 0.555758164 -1.15559295 -1.45483810 -0.6422302156 1
coef(lm(body_mass ~ temperature * rainfall, data))
## (Intercept) temperature rainfall
## 0.01175390 0.50865026 0.33617972
## temperature:rainfall
## -0.09533579
1.3.2 Non-linear effects
Polynomial (quadratic, cubic, etc) functions are essentially interactions with the same predictor. They can therefore be specified in the same way:
<- simulate_population(
squid_data n=2000,
response_name = "body_mass",
parameters=list(
observation=list(
names=c("temperature"),
beta = c(0.5)
),interactions=list(
names=c("temperature:temperature"),
beta = c(-0.3)
),residual=list(
vcov=0.3
)
)
)<- get_population_data(squid_data)
data
plot(body_mass ~ temperature, data, pch=19, cex=0.5, col=alpha(1,0.5))
coef(lm(body_mass ~ temperature + I(temperature^2), data))
## (Intercept) temperature I(temperature^2)
## -0.002908256 0.516327302 -0.291477043