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.75306930 -1.7993007 0.1797179 0.06032903 -0.32336662 1
## 2 1.10189574 1.4074719 -0.3185415 0.44888842 -0.44833826 1
## 3 0.38025544 -0.2554504 -0.8691029 0.79091279 0.22201270 1
## 4 -0.50416601 -1.3475335 0.9115643 -0.22670489 -1.22836340 1
## 5 -0.02845418 2.3573010 -0.3703654 -1.18330134 -0.87306263 1
## 6 0.35353883 -0.1211901 0.7102531 0.19245041 -0.08607567 1
coef(lm(body_mass ~ temperature * rainfall, data))
## (Intercept) temperature rainfall
## 0.01766038 0.48214243 0.32101602
## temperature:rainfall
## -0.08422462
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.02261543 0.50498361 -0.30214780