-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03-model-checking-and-selection.R
137 lines (101 loc) · 3.31 KB
/
03-model-checking-and-selection.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
library(mgcv)
trawl_data <- read.csv("data/trawl_nl.csv")
trawl_data_2010 <- trawl_data[trawl_data$year == 2010, ]
shrimp <- gam(
shrimp ~ s(depth) + s(temp_bottom),
data = trawl_data_2010,
family = gaussian,
method = "REML")
summary(shrimp)
plot(shrimp, pages = 1)
plot(shrimp, residuals = TRUE, cex=0.5, pch=21, pages = 1)
par(mfrow = c(2,2))
gam.check(shrimp)
par(mfrow = c(1,1))
concurvity(shrimp, full = TRUE)
concurvity(shrimp, full = FALSE)
lapply(concurvity(shrimp, full = FALSE), round, 2)
hist(trawl_data_2010$shrimp)
hist(log(trawl_data_2010$shrimp))
shrimp_log <- gam(
shrimp ~ s(depth) + s(temp_bottom),
data = trawl_data_2010,
family = gaussian(link = "log"),
method = "REML")
summary(shrimp_log)
plot(shrimp_log, residuals = TRUE, cex=0.5, pch=21, pages = 1)
plot(shrimp_log, residuals = FALSE, cex=0.5, pch=21, pages = 1)
par(mfrow = c(2,2))
gam.check(shrimp_log)
par(mfrow = c(1,1))
shrimp_tw <- gam(
shrimp ~ s(depth) + s(temp_bottom),
data = trawl_data_2010,
family = tw,
method = "REML")
summary(shrimp_tw)
plot(shrimp_tw, residuals = TRUE, cex=0.5, pch=21, pages = 1)
par(mfrow = c(2,2))
gam.check(shrimp_tw)
par(mfrow = c(1,1))
shrimp_tw2 <- gam(
shrimp ~ s(depth, k = 20) + s(temp_bottom, k = 20),
data = trawl_data_2010,
family = tw,
method = "REML")
summary(shrimp_tw2)
plot(shrimp_tw2, residuals = TRUE, cex=0.5, pch=21, pages = 1)
par(mfrow = c(2,2))
gam.check(shrimp_tw2)
par(mfrow = c(1,1))
shrimp_tw_te <- gam(
shrimp ~ te(depth, temp_bottom),
data = trawl_data_2010,
family = tw,
method = "REML")
summary(shrimp_tw_te)
plot(shrimp_tw_te, pages = 1, residuals = TRUE, cex=0.5, pch=21, scheme = 2)
par(mfrow = c(2,2))
gam.check(shrimp_tw_te)
par(mfrow = c(1,1))
# ---- Exercise ----
# Fit a model of bottom temperature, using smooths `stratum`, `depth`,
# and `x`, `y` coordinates
# Plot the model, examine gam.check, diagnostic plots and concurvity
# Modify the model to reduce any poor diagnostics outcomes. You may modify smooths,
# add or remove variables, change `k` values, or change the distribution.
temp <- gam(
temp_bottom ~ __YOUR_FORMULA_HERE__
data = trawl_data_2010,
family = __YOUR_DISTRIBUTION_HERE__,
method = "REML")
# --- Back to it! Model selection ---
trawl_data_extra <- trawl_data_2010
trawl_data_extra$var1 <- rnorm(nrow(trawl_data_extra))
shrimp_tw_all <- gam(
shrimp ~ s(depth) + s(x) + s(y) + s(temp_bottom) + s(stratum) + s(var1),
data = trawl_data_extra,
family = tw,
method = "REML")
summary(shrimp_tw_all)
plot(shrimp_tw_all, pages = 1, residuals = TRUE, cex=0.5, pch=21, scheme = 2, scale = 0)
shrimp_tw_sel <- gam(
shrimp ~ s(depth) + s(x) + s(y) + s(temp_bottom) + s(stratum) + s(var1),
data = trawl_data_extra,
family = tw,
method = "REML",
select = TRUE)
summary(shrimp_tw_sel)
plot(shrimp_tw_sel, pages = 1, residuals = TRUE, cex=0.5, pch=21, scheme = 2, scale = 0)
par(mfrow = c(2,2))
gam.check(shrimp_tw_sel)
par(mfrow = c(1,1))
# ---- Exercise ----
# Use double penalization to select variables that should remain in the
# following model predicting bottom temperatures
temp_all <- gam(
temp_bottom ~ s(depth) + s(x) + s(y) + s(stratum) + s(shrimp) + s(cod) + s(total) + s(richness),
data = trawl_data_2010,
family = gaussian,
method = "REML")
# Using gam.check and concurvity, how would you improve this model?