You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: book-en/02-introduction.Rmd
+12-12Lines changed: 12 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -20,20 +20,21 @@ major assumptions:
20
20
A linear model can sometimes accommodate certain types of non-linear responses (e.g. $x^2$), but this approach strongly relies on decisions that can be either arbitrary or well-informed, and is much less flexible than using an additive model. For example, this linear model with multiple predictors can handle a non-linear response, but quickly becomes difficult to interpret and manage:
So, how can we fit a better model? To answer this question, we must first consider what the regression model is
36
-
trying to do. The linear model is trying to fit the best __straight line__ that passes through the middle of the data, _without __overfitting___ the data, which is what would happen if we simply drew a line between each point and its neighbours. Linear models do this by finding the best fit straight line that passes through the data.
37
+
trying to do. The linear model is trying to fit the best __straight line__ that passes through the middle of the data, _without __overfitting___ the data, which is what would happen if we simply drew a line between each point and its neighbours. Linear models do this by finding the best fit straight line that passes through the data.
37
38
38
39
In the same way, additive models fit a curve through the data, while controlling the ___wiggliness___ of this curve to avoid overfitting. This means additive models like GAMs can capture non-linear relationships by fitting a smooth function through the data, rather than a straight line. We will come back to the concept of ___wiggliness___ later!
The linear model is explaining quite a bit of variance in our dataset ($R_{adj}$ = 0.588), which means it's a pretty good model, right? Well, let's take a look at how our model fits the data:
65
+
The linear model is explaining quite a bit of variance in our dataset ($R_{adj}$ = 0.588), which means it's a pretty good model, right? Well, let's take a look at how our model fits the data:
@@ -94,7 +95,7 @@ where $y_i$ is the response variable, $x_i$ is the predictor variable, and $f$ i
94
95
Importantly, given that the smooth function $f(x_i)$ is non-linear and
95
96
local, the magnitude of the effect of the explanatory variable can vary
96
97
over its range, depending on the relationship between the variable and
97
-
the response.
98
+
the response.
98
99
99
100
That is, as opposed to one fixed coefficient $\beta$, the function $f$ can continually change over the range of $x_i$.
100
101
The degree of smoothness (or wiggliness) of $f$ is controlled using penalized regression determined automatically in `mgcv` using a generalized cross-validation (GCV) routine [@wood_2006].
@@ -122,7 +123,7 @@ Recall: As opposed to one fixed coefficient $\beta$ in a linear model, the funct
122
123
123
124
:::
124
125
125
-
The `mgcv` package also includes a default plot to look at the smooths:
126
+
The `mgcv` package also includes a default `plot()` function to look at the smooths:
Here, the AIC of the smooth GAM is lower, which indicates that adding a smoothing function improves model performance. Linearity is therefore not supported by our data.
145
146
146
147
:::explanation
147
-
As a brief explanation, the Akaike Information Criterion (AIC) is a comparative metric of model performance, where lower scores indicate that a model is performing "better" compared to other considered models.
148
+
As a brief explanation, the Akaike Information Criterion (AIC) is a comparative metric of model performance, where lower scores indicate that a model is performing "better" compared to other considered models.
148
149
:::
149
150
150
151
## Challenge 1
@@ -165,7 +166,7 @@ We have not discussed effective degrees of freedom (**EDF**) yet, but these are
165
166
166
167
```{r, echo = FALSE, include = FALSE}
167
168
# Challenge 1 ----
168
-
#
169
+
#
169
170
# 1. Fit a linear and smoothed GAM model to the relation between `SampleDepth` and `Sources`.
170
171
# 2. Determine if linearity is justified for this data.
171
172
# 3. How many effective degrees of freedom does the smoothed term have?
We can supplement this with a quantitative comparison of model performance using `AIC()`.
200
+
We can supplement this with a quantitative comparison of model performance using `AIC()`.
200
201
201
202
```{r}
202
203
AIC(linear_model_s1, smooth_model_s1)
203
204
```
204
205
205
-
The lower AIC score indicates that smooth model is performing better than the linear model, which confirms that linearity is not appropriate for our dataset.
206
+
The lower AIC score indicates that smooth model is performing better than the linear model, which confirms that linearity is not appropriate for our dataset.
206
207
207
208
__3.__ How many effective degrees of freedom does the smoothed term have?
208
209
@@ -213,4 +214,3 @@ smooth_model_s1
213
214
```
214
215
215
216
The effective degrees of freedom (EDF) are >> 1. Keep this in mind, because we will be coming back to EDF [later](#edf)!
Copy file name to clipboardExpand all lines: book-en/04-smooth-terms.Rmd
+21-20Lines changed: 21 additions & 20 deletions
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,9 @@
1
-
# GAM with multiple smooth terms
1
+
# GAM with multiple smooth terms
2
2
3
3
## GAM with linear and smooth terms
4
4
5
5
GAMs make it easy to include both smooth and linear terms, multiple
6
-
smoothed terms, and smoothed interactions.
6
+
smoothed terms, and smoothed interactions.
7
7
8
8
For this section, we will use the `ISIT` dataset again. We will try to model the response `Sources` using the predictors `Season` and `SampleDepth` simultaneously.
9
9
@@ -25,7 +25,7 @@ basic_model <- gam(Sources ~ Season + s(SampleDepth), data = isit, method = "REM
25
25
basic_summary <- summary(basic_model)
26
26
```
27
27
28
-
The `p.table` provides information on the linear effects:
28
+
The `p.table` provides information on the linear effects:
29
29
30
30
```{r}
31
31
basic_summary$p.table
@@ -70,9 +70,10 @@ In linear regression, the *model* degrees of freedom are equivalent to the numbe
70
70
71
71
Because the number of free parameters in GAMs is difficult to define, the **EDF** are instead related to the smoothing parameter $\lambda$, such that the greater the penalty, the smaller the **EDF**.
72
72
73
-
An upper bound on the **EDF** is determined by the basis dimension $k$ for each smooth function, meaning the **EDF** cannot exceed $k-1$.
73
+
An upper bound on the **EDF** is determined by the basis dimension $k$ for each smooth function, meaning the **EDF** cannot exceed $k-1$.
74
+
75
+
In practice, the exact choice of $k$ is arbitrary, but it should be **large enough** to accommodate a sufficiently complex smooth function. We will talk about choosing $k$ in upcoming sections.
74
76
75
-
In practice, the exact choice of $k$ is arbitrary, but it should be **large enough** to accommodate a sufficiently complex smooth function. We will talk about choosing $k$ in [Chapter 6](#model-checking).
76
77
77
78
:::explanation
78
79
Higher EDF imply more complex, wiggly splines.
@@ -85,7 +86,7 @@ When a term has an EDF value that is close to 1, it is close to being a linear t
85
86
We can add a second term (`RelativeDepth`) to our basic model, but specify a linear relationship with `Sources`.
86
87
87
88
```{r}
88
-
two_term_model <- gam(Sources ~ Season + s(SampleDepth) + RelativeDepth,
89
+
two_term_model <- gam(Sources ~ Season + s(SampleDepth) + RelativeDepth,
89
90
data = isit, method = "REML")
90
91
two_term_summary <- summary(two_term_model)
91
92
```
@@ -116,7 +117,7 @@ If we want to know whether the relationship between `Sources` and `RelativeDepth
116
117
non-linear, we can model `RelativeDepth` as a smooth term instead. In this model, we would have two smooth terms:
117
118
118
119
```{r}
119
-
two_smooth_model <- gam(Sources ~ Season + s(SampleDepth) + s(RelativeDepth),
120
+
two_smooth_model <- gam(Sources ~ Season + s(SampleDepth) + s(RelativeDepth),
120
121
data = isit, method = "REML")
121
122
two_smooth_summary <- summary(two_smooth_model)
122
123
```
@@ -133,7 +134,7 @@ In the `s.table`, we will now find two non-linear smoothers, `s(SampleDepth)` an
133
134
two_smooth_summary$s.table
134
135
```
135
136
136
-
Let us take a look at the relationships between the linear and non-linear predictors and our response variable.
137
+
Let us take a look at the relationships between the linear and non-linear predictors and our response variable.
137
138
138
139
```{r, fig.height = 8}
139
140
par(mfrow=c(2,2))
@@ -154,16 +155,16 @@ We can see that `two_smooth_model` has the lowest AIC value. The best fit model
154
155
155
156
## Challenge 2
156
157
157
-
For our second challenge, we will be building onto our model by adding variables which we think might be ecologically significant predictors to explain bioluminescence.
158
+
For our second challenge, we will be building onto our model by adding variables which we think might be ecologically significant predictors to explain bioluminescence.
158
159
159
160
1. Create two new models: Add `Latitude` to `two_smooth_model`, first as a linear term, then as a smoothed term.
160
161
2. Is `Latitude` an important term to include? Does `Latitude` have a linear or additive effect? Use plots, coefficient tables, and the `AIC()` function to help you answer this question.
161
162
162
163
```{r, echo = FALSE, include = FALSE}
163
164
# Challenge 2 ----
164
-
#
165
-
# For our second challenge, we will be building onto our model by adding variables which we think might be ecologically significant predictors to explain bioluminescence.
166
-
#
165
+
#
166
+
# For our second challenge, we will be building onto our model by adding variables which we think might be ecologically significant predictors to explain bioluminescence.
167
+
#
167
168
#
168
169
# 1. Create two new models: Add `Latitude` to `two_smooth_model`, first as a linear term, then as a smoothed term.
169
170
# 2. Is `Latitude` an important term to include? Does `Latitude` have a linear or additive effect? Use plots, coefficient tables, and the `AIC()` function to help you answer this question.
@@ -177,17 +178,17 @@ __1.__ Create two new models: Add `Latitude` to `two_smooth_model`, first as a l
@@ -221,14 +222,14 @@ Before deciding which model is "best", we should test whether the effect of `Lat
221
222
AIC(three_smooth_model, three_term_model)
222
223
```
223
224
224
-
Our model including Latitude as a _smooth_ term has a lower AIC score, meaning it performs better than our model including Latitude as a _linear_ term.
225
+
Our model including Latitude as a _smooth_ term has a lower AIC score, meaning it performs better than our model including Latitude as a _linear_ term.
225
226
226
227
But, does adding `Latitude` as a smooth predictor actually improve on our last "best" model (`two_smooth_model`)?
227
228
228
229
```{r}
229
230
AIC(two_smooth_model, three_smooth_model)
230
231
```
231
232
232
-
Our `three_smooth_model`, which includes `SampleDepth`, `RelativeDepth`, and `Latitude` as _smooth_ terms, and Season as a linear term, has a lower AIC score than our previous best model, which did not include `Latitude`.
233
+
Our `three_smooth_model`, which includes `SampleDepth`, `RelativeDepth`, and `Latitude` as _smooth_ terms, and Season as a linear term, has a lower AIC score than our previous best model, which did not include `Latitude`.
233
234
234
-
This implies that `Latitude` is indeed an informative non-linear predictor of bioluminescence.
235
+
This implies that `Latitude` is indeed an informative non-linear predictor of bioluminescence.
> This plot can be rotated by changing the value of the `theta` argument.
42
41
42
+
:::explanation
43
+
This plot can be rotated by changing the value of the `theta` argument.
44
+
:::
43
45
44
46
To test our idea that this interaction is important, we will perform a model comparison using AIC to determine whether the interaction term improves our model's performance.
45
47
@@ -55,12 +57,12 @@ The AIC of our model with a factor interaction between the `SampleDepth` smooth
55
57
Next, we'll look at the interactions between two smoothed terms, `SampleDepth` and `RelativeDepth`.
56
58
57
59
```{r}
58
-
smooth_interact <- gam(Sources ~ Season + s(SampleDepth, RelativeDepth),
60
+
smooth_interact <- gam(Sources ~ Season + s(SampleDepth, RelativeDepth),
59
61
data = isit, method = "REML")
60
62
summary(smooth_interact)$s.table
61
63
```
62
64
63
-
In the previous section, we were able to visualise an interaction effect between a smooth and a factor term by plotting a different smooth function of `SampleDepth` for each level of `Season`.
65
+
In the previous section, we were able to visualise an interaction effect between a smooth and a factor term by plotting a different smooth function of `SampleDepth` for each level of `Season`.
64
66
65
67
In this model, we have two smoothed terms, which means that the effect of `SampleDepth` varies smoothly with `RelativeDepth`, and vice-versa. When we visualise this interaction, we instead get a gradient between two continuous smooth functions:
We can also plot this interaction on a 3D surface:
72
74
73
75
```{r}
74
-
vis.gam(smooth_interact,
75
-
view = c("SampleDepth", "RelativeDepth"),
76
+
vis.gam(smooth_interact,
77
+
view = c("SampleDepth", "RelativeDepth"),
76
78
theta = 50, n.grid = 50, lwd = .4)
77
79
```
78
-
> Remember, this plot can be rotated by changing the value of the `theta` argument.
79
80
80
81
:::explanation
82
+
Remember, this plot can be rotated by changing the value of the `theta` argument.
83
+
81
84
You can change the colour of the 3D plot using the `color` argument. Try specifying `color = "cm"` in `vis.gam()` above, and check `?vis.gam` for more color options.
82
85
:::
83
86
@@ -89,4 +92,4 @@ So, there does seem to be an interaction effect between these smooth terms. Does
89
92
AIC(two_smooth_model, smooth_interact)
90
93
```
91
94
92
-
The model with the interaction between `s(SampleDepth)` and `s(RelativeDepth)` has a lower AIC, which means including this interaction improves our model's performance, and our ability to understand the drivers of bioluminescence.
95
+
The model with the interaction between `s(SampleDepth)` and `s(RelativeDepth)` has a lower AIC, which means including this interaction improves our model's performance, and our ability to understand the drivers of bioluminescence.
0 commit comments