24 July 2014

Updated #R Code: GAM exercise using mgcv package

Dear friends,

The previous GAM post was based on only one year dataset. I have added another four year dataset in to the system and unfortunately it needed several adjustment, especially for the knot (k) value.

So the following is the updated R code. I am sure someone can come up with more efficient code.

Cheers,
Erwin

Note: 
We can use (x,y) coordinate as one of the predictor, as tensor function using "te()".
We can also include character-type column as the predictor.

  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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#---
# title : MGCV package tryout
# author: Dasapta Erwin Irawan^1 and Farzina Akter^2
# affiliation^1: Institut Teknologi Bandung (Indonesia)
# affiliation^2: University of Sydney (Australia)
# date  : 22 July 2014
#---

# This code is following http://www3.nd.edu/~mclark19/learn/GAMS.pdf

# Load library and data
require("mgcv")
data <- read.csv("alldata23.csv")


##########################
##### GAM ANALYSIS #######
##########################

# load library and data
require("mgcv")
data <- read.csv("alldata23.csv")
group1 <- data[,c("x","y","ec","elv","aq","ph","hard","tds","temp","eh","Q")]
group2 <- data[,c("x","y","ec","Ca","Mg","Fe","Mn","K","Na")]
group3 = data[,c("x","y","ec","CO3","HCO3","CO2","Cl","SO4","NO2",
                 "NO3","SiO2")]

# GAM models (check, all predictors must be numeric)

################## FAMILY = GAUSSIAN #####################

## ols (k=10 default changed to k=5, to avoid smoothing error)
k1<-3 
# change the knot (k) value  to avoid this error message
# ... basis dimension, k, increased to minimum possible ...
# k=10 (default)
gam11<-gam(ec ~ s(x,k=k1) + s(y,k=k1) + s(elv,k=k1) + 
             s(ph,k=k1) + s(hard,k=k1) + 
             s(tds,k=k1) + s(temp,k=k1) + s(eh,k=k1) + 
             s(Q,k=k1), data=group1)

# [dropping "Mg"] 
# I've tested each variables to avoid these error messages
# ... max not meaningful for factors ...
k2<-3 # (if you don't change it, then R will use previous k value)
gam12<-gam(ec ~ s(x,k=k2) + 
             s(y,k=k2) + 
             s(Ca,k=k2) + 
             s(Fe,k=k2) + 
             s(K,k=k2) + 
             s(Na,k=k2) + 
             s(Mn,k=k2), 
           data=group2)

k3<-3 # (if you don't change it, then R will use previous k value)
gam13<-gam(ec ~ s(x,k=k3) + s(y,k=k3) + s(CO3,k=k3) + 
             s(HCO3,k=k3) + s(CO2,k=k3) + s(Cl,k=k3) + 
             s(SO4,k=k3) + s(NO2,k=k3) + s(NO3,k=k3) +
             + s(SiO2,k=k3), data=group3)

## smoothing=thin plate smoothing
#k1<-3
gam21<-gam(ec ~ s(x,k=k1,bs="tp") + s(y,k=k1,bs="tp") + s(elv,k=k1,bs="tp") + 
             s(ph,k=k1,bs="tp") + s(hard,k=k1,bs="tp") + 
             s(tds,k=k1,bs="tp") + s(temp,k=k1,bs="tp") + s(eh,k=k1,bs="tp") + 
             s(Q,k=k1,bs="tp"), data=group1)

#k2<-3 xxxxxxxxxxxx
gam22<-gam(ec ~ s(x,k=k2,bs="tp") + s(y,k=k2,bs="tp") + s(Ca,k=k2,bs="tp") + 
             s(Fe,k=k2,bs="tp") + s(Mn,k=k2,bs="tp") + 
             s(K,k=k2,bs="tp") + s(Na,k=k2,bs="tp"), data=group2)

#k3<-5
gam23<-gam(ec ~ s(x,k=k3,bs="tp") + s(y,k=k3,bs="tp") + s(CO3,k=k3,bs="tp") + 
             s(HCO3,k=k3,bs="tp") + s(CO2,k=k3,bs="tp") + s(Cl,k=k3,bs="tp") + 
             s(SO4,k=k3,bs="tp") + s(NO2,k=k3,bs="tp") + s(NO3,k=k3,bs="tp") +
             + s(SiO2,k=k3,bs="tp"), data=group3)

## smoothing=thin shrinkage 
#k1<-5
bsm<-"ts"
gam31<-gam(ec ~ s(x,k=k1,bs=bsm) + s(y,k=k1,bs=bsm) + s(elv,k=k1,bs=bsm) + 
             s(ph,k=k1,bs=bsm) + s(hard,k=k1,bs=bsm) + 
             s(tds,k=k1,bs=bsm) + s(temp,k=k1,bs=bsm) + s(eh,k=k1,bs=bsm) + 
             s(Q,k=k1,bs=bsm), data=group1)

#k2<-3 xxxxxxxxxxxx
bsm<-"ts"
gam32<-gam(ec ~ s(x,k=k2,bs=bsm) + s(y,k=k2,bs=bsm) + s(Ca,k=k2,bs=bsm) + 
             s(Fe,k=k2,bs=bsm) + s(Mn,k=k2,bs=bsm) + 
             s(K,k=k2,bs=bsm) + s(Na,k=k2,bs=bsm), data=group2)

#k3<-5
bsm<-"ts"
gam33<-gam(ec ~ s(x,k=k3,bs=bsm) + s(y,k=k3,bs=bsm) + s(CO3,k=k3,bs=bsm) + 
             s(HCO3,k=k3,bs=bsm) + s(CO2,k=k3,bs=bsm) + s(Cl,k=k3,bs=bsm) + 
             s(SO4,k=k3,bs=bsm) + s(NO2,k=k3,bs=bsm) + s(NO3,k=k3,bs=bsm) +
             + s(SiO2,k=k3,bs=bsm), data=group3)

# smoothing=cubic regression spline
#k1<-5
bsm<-"cr"
gam41<-gam(ec ~ s(x,k=k1,bs=bsm) + s(y,k=k1,bs=bsm) + s(elv,k=k1,bs=bsm) + 
             s(ph,k=k1,bs=bsm) + s(hard,k=k1,bs=bsm) + 
             s(tds,k=k1,bs=bsm) + s(temp,k=k1,bs=bsm) + s(eh,k=k1,bs=bsm) + 
             s(Q,k=k1,bs=bsm), data=group1)

#k2<-3 xxxxxxxxxxxx
gam42<-gam(ec ~ s(x,k=k2,bs=bsm) + s(y,k=k2,bs=bsm) + s(Ca,k=k2,bs=bsm) + 
             s(Fe,k=k2,bs=bsm) + s(Mn,k=k2,bs=bsm) + 
             s(K,k=k2,bs=bsm) + s(Na,k=k2,bs=bsm), data=group2)

#k3<-5
gam43<-gam(ec ~ s(x,k=k3,bs=bsm) + s(y,k=k3,bs=bsm) + s(CO3,k=k3,bs=bsm) + 
             s(HCO3,k=k3,bs=bsm) + s(CO2,k=k3,bs=bsm) + s(Cl,k=k3,bs=bsm) + 
             s(SO4,k=k3,bs=bsm) + s(NO2,k=k3,bs=bsm) + s(NO3,k=k3,bs=bsm) +
             + s(SiO2,k=k3,bs=bsm), data=group3)

# smoothing=cubic shrinkage version
bsm<-"cs"
#k1<-5
gam51<-gam(ec ~ s(x,k=k1,bs=bsm) + s(y,k=k1,bs=bsm) + s(elv,k=k1,bs=bsm) + 
             s(ph,k=k1,bs=bsm) + s(hard,k=k1,bs=bsm) + 
             s(tds,k=k1,bs=bsm) + s(temp,k=k1,bs=bsm) + s(eh,k=k1,bs=bsm) + 
             s(Q,k=k1,bs=bsm), data=group1)

#k2<-3 xxxxxxxxxxxx
gam52<-gam(ec ~ s(x,k=k2,bs=bsm) + s(y,k=k2,bs=bsm) + s(Ca,k=k2,bs=bsm) + 
             s(Fe,k=k2,bs=bsm) + s(Mn,k=k2,bs=bsm) + 
             s(K,k=k2,bs=bsm) + s(Na,k=k2,bs=bsm), data=group2)

#k3<-5
gam53<-gam(ec ~ s(x,k=k3,bs=bsm) + s(y,k=k3,bs=bsm) + s(CO3,k=k3,bs=bsm) + 
             s(HCO3,k=k3,bs=bsm) + s(CO2,k=k3,bs=bsm) + s(Cl,k=k3,bs=bsm) + 
             s(SO4,k=k3,bs=bsm) + s(NO2,k=k3,bs=bsm) + s(NO3,k=k3,bs=bsm) +
             + s(SiO2,k=k3,bs=bsm), data=group3)

# smoothing=cyclic cubic regression spline
k1<-5 [changed from 3 to 5]
bsm<-"cc"
gam61<-gam(ec ~ s(x,k=k1,bs=bsm) + s(y,k=k1,bs=bsm) + s(elv,k=k1,bs=bsm) + 
             s(ph,k=k1,bs=bsm) + s(hard,k=k1,bs=bsm) + 
             s(tds,k=k1,bs=bsm) + s(temp,k=k1,bs=bsm) + s(eh,k=k1,bs=bsm) + 
             s(Q,k=k1,bs=bsm), data=group1)

k2<-8 xxxxxxxxxxxxx
gam62<-gam(ec ~ s(x,k=k2,bs=bsm) + s(y,k=k2,bs=bsm) + s(Ca,k=k2,bs=bsm) + 
             s(Fe,k=k2,bs=bsm) + s(Mn,k=k2,bs=bsm) + 
             s(K,k=k2,bs=bsm) + s(Na,k=k2,bs=bsm), data=group2)

k3<-5
gam63<-gam(ec ~ s(x,k=k3,bs=bsm) + s(y,k=k3,bs=bsm) + s(CO3,k=k3,bs=bsm) + 
             s(HCO3,k=k3,bs=bsm) + s(CO2,k=k3,bs=bsm) + s(Cl,k=k3,bs=bsm) + 
             s(SO4,k=k3,bs=bsm) + s(NO2,k=k3,bs=bsm) + s(NO3,k=k3,bs=bsm) +
             + s(SiO2,k=k3,bs=bsm), data=group3)

# Dropping "cc" model, causing error, don't have cyclic pattern


################## FAMILY = GAMMA #####################
## link=log, default smoothing
#k1<-5 # k=10 (default)
gam71<-gam(ec ~ s(x,k=k1) + s(y,k=k1) + s(elv,k=k1) + 
             s(ph,k=k1) + s(hard,k=k1) + 
             s(tds,k=k1) + s(temp,k=k1) + s(eh,k=k1) + 
             s(Q,k=k1), Gamma (link="log"), data=group1)

#k2<-3 xxxxxxxxxxxxx
gam72<-gam(ec ~ s(x,k=k2) + s(y,k=k2) + s(Ca,k=k2) + 
             s(Fe,k=k2) + s(Mn,k=k2) + 
             s(K,k=k2) + s(Na,k=k2), Gamma (link="log"), data=group2)

#k3<-5
gam73<-gam(ec ~ s(x,k=k3) + s(y,k=k3) + s(CO3,k=k3) + 
             s(HCO3,k=k3) + s(CO2,k=k3) + s(Cl,k=k3) + 
             s(SO4,k=k3) + s(NO2,k=k3) + s(NO3,k=k3) +
             + s(SiO2,k=k3), Gamma (link="log"), data=group3)

## smoothing=thin plate smoothing
#k1<-5
gam81<-gam(ec ~ s(x,k=k1,bs="tp") + s(y,k=k1,bs="tp") + s(elv,k=k1,bs="tp") + 
             s(ph,k=k1,bs="tp") + s(hard,k=k1,bs="tp") + 
             s(tds,k=k1,bs="tp") + s(temp,k=k1,bs="tp") + s(eh,k=k1,bs="tp") + 
             s(Q,k=k1,bs="tp"), 
             Gamma (link="log"), data=group1)

#k2<-3 xxxxxxxxxxxxx
gam82<-gam(ec ~ s(x,k=k2,bs="tp") + s(y,k=k2,bs="tp") + s(Ca,k=k2,bs="tp") + 
             s(Fe,k=k2,bs="tp") + s(Mn,k=k2,bs="tp") + 
             s(K,k=k2,bs="tp") + s(Na,k=k2,bs="tp"), 
             Gamma (link="log"), data=group2)

#k3<-5
gam83<-gam(ec ~ s(x,k=k3,bs="tp") + s(y,k=k3,bs="tp") + s(CO3,k=k3,bs="tp") + 
             s(HCO3,k=k3,bs="tp") + s(CO2,k=k3,bs="tp") + s(Cl,k=k3,bs="tp") + 
             s(SO4,k=k3,bs="tp") + s(NO2,k=k3,bs="tp") + s(NO3,k=k3,bs="tp") +
             + s(SiO2,k=k3,bs="tp"), 
             Gamma (link="log"), data=group3)

## smoothing=thin shrinkage 
#k1<-5
bsm<-"ts"
gam91<-gam(ec ~ s(x,k=k1,bs=bsm) + s(y,k=k1,bs=bsm) + s(elv,k=k1,bs=bsm) + 
             s(ph,k=k1,bs=bsm) + s(hard,k=k1,bs=bsm) + 
             s(tds,k=k1,bs=bsm) + s(temp,k=k1,bs=bsm) + s(eh,k=k1,bs=bsm) + 
             s(Q,k=k1,bs=bsm), 
             Gamma (link="log"), data=group1)

#k2<-3 xxxxxxxxxxxxx
bsm<-"ts"
gam92<-gam(ec ~ s(x,k=k2,bs=bsm) + s(y,k=k2,bs=bsm) + s(Ca,k=k2,bs=bsm) + 
             s(Fe,k=k2,bs=bsm) + s(Mn,k=k2,bs=bsm) + 
             s(K,k=k2,bs=bsm) + s(Na,k=k2,bs=bsm),
             Gamma (link="log"), data=group2)

#k3<-5
bsm<-"ts"
gam93<-gam(ec ~ s(x,k=k3,bs=bsm) + s(y,k=k3,bs=bsm) + s(CO3,k=k3,bs=bsm) + 
             s(HCO3,k=k3,bs=bsm) + s(CO2,k=k3,bs=bsm) + s(Cl,k=k3,bs=bsm) + 
             s(SO4,k=k3,bs=bsm) + s(NO2,k=k3,bs=bsm) + s(NO3,k=k3,bs=bsm) +
             + s(SiO2,k=k3,bs=bsm), 
             Gamma (link="log"), data=group3)

# Family=gaussian, smoothing=cubic regression spline
#k1<-5
bsm<-"cr"
gam101<-gam(ec ~ s(x,k=k1,bs=bsm) + s(y,k=k1,bs=bsm) + s(elv,k=k1,bs=bsm) + 
             s(ph,k=k1,bs=bsm) + s(hard,k=k1,bs=bsm) + 
             s(tds,k=k1,bs=bsm) + s(temp,k=k1,bs=bsm) + s(eh,k=k1,bs=bsm) + 
             s(Q,k=k1,bs=bsm), 
             Gamma (link="log"), data=group1)

#k2<-3 xxxxxxxxxxxxx
gam102<-gam(ec ~ s(x,k=k2,bs=bsm) + s(y,k=k2,bs=bsm) + s(Ca,k=k2,bs=bsm) + 
             s(Fe,k=k2,bs=bsm) + s(Mn,k=k2,bs=bsm) + 
             s(K,k=k2,bs=bsm) + s(Na,k=k2,bs=bsm), 
             Gamma (link="log"), data=group2)

#k3<-5
gam103<-gam(ec ~ s(x,k=k3,bs=bsm) + s(y,k=k3,bs=bsm) + s(CO3,k=k3,bs=bsm) + 
             s(HCO3,k=k3,bs=bsm) + s(CO2,k=k3,bs=bsm) + s(Cl,k=k3,bs=bsm) + 
             s(SO4,k=k3,bs=bsm) + s(NO2,k=k3,bs=bsm) + s(NO3,k=k3,bs=bsm) +
             + s(SiO2,k=k3,bs=bsm), 
             Gamma (link="log"), data=group3)

# smoothing=cubic shrinkage version
bsm<-"cs"
#k1<-5
gam111<-gam(ec ~ s(x,k=k1,bs=bsm) + s(y,k=k1,bs=bsm) + s(elv,k=k1,bs=bsm) + 
             s(ph,k=k1,bs=bsm) + s(hard,k=k1,bs=bsm) + 
             s(tds,k=k1,bs=bsm) + s(temp,k=k1,bs=bsm) + s(eh,k=k1,bs=bsm) + 
             s(Q,k=k1,bs=bsm), 
             Gamma (link="log"), data=group1)

#k2<-3 xxxxxxxxxxxxx
gam112<-gam(ec ~ s(x,k=k2,bs=bsm) + s(y,k=k2,bs=bsm) + s(Ca,k=k2,bs=bsm) + 
             s(Fe,k=k2,bs=bsm) + s(Mn,k=k2,bs=bsm) + 
             s(K,k=k2,bs=bsm) + s(Na,k=k2,bs=bsm), 
             Gamma (link="log"), data=group2)

#k3<-5
gam113<-gam(ec ~ s(x,k=k3,bs=bsm) + s(y,k=k3,bs=bsm) + s(CO3,k=k3,bs=bsm) + 
             s(HCO3,k=k3,bs=bsm) + s(CO2,k=k3,bs=bsm) + s(Cl,k=k3,bs=bsm) + 
             s(SO4,k=k3,bs=bsm) + s(NO2,k=k3,bs=bsm) + s(NO3,k=k3,bs=bsm) +
             + s(SiO2,k=k3,bs=bsm), 
             Gamma (link="log"), data=group3)

# smoothing=cyclic cubic regression spline
# k1<-5
bsm<-"cc"
gam121<-gam(ec ~ s(x,k=k1,bs=bsm) + s(y,k=k1,bs=bsm) + s(elv,k=k1,bs=bsm) + 
             s(ph,k=k1,bs=bsm) + s(hard,k=k1,bs=bsm) + 
             s(tds,k=k1,bs=bsm) + s(temp,k=k1,bs=bsm) + s(eh,k=k1,bs=bsm) + 
             s(Q,k=k1,bs=bsm), 
             Gamma (link="log"), data=group1)

#k2<-3 xxxxxxxxxxxxx
gam122<-gam(ec ~ s(x,k=k2,bs=bsm) + s(y,k=k2,bs=bsm) + s(Ca,k=k2,bs=bsm) + 
             s(Fe,k=k2,bs=bsm) + s(Mn,k=k2,bs=bsm) + 
             s(K,k=k2,bs=bsm) + s(Na,k=k2,bs=bsm), 
             Gamma (link="log"), data=group2)

#k3<-5
gam123<-gam(ec ~ s(x,k=k3,bs=bsm) + s(y,k=k3,bs=bsm) + s(CO3,k=k3,bs=bsm) + 
             s(HCO3,k=k3,bs=bsm) + s(CO2,k=k3,bs=bsm) + s(Cl,k=k3,bs=bsm) + 
             s(SO4,k=k3,bs=bsm) + s(NO2,k=k3,bs=bsm) + s(NO3,k=k3,bs=bsm) +
             + s(SiO2,k=k3,bs=bsm), 
             Gamma (link="log"), data=group3)

######### GAM EVALUATION ################
# Gaussian
AIC.gsdef<-AIC(gam11,gam12,gam13)
AIC.gstp<-AIC(gam21,gam22,gam23)
AIC.gsts<-AIC(gam31,gam32,gam33)
AIC.gscr<-AIC(gam41,gam42,gam43)
AIC.gscs<-AIC(gam51,gam52,gam53)
AIC.gscc<-AIC(gam61,gam62,gam63) 

print(AIC.gsdef) ; print(AIC.gstp) # lowestAIC=gam13(3300.728) and gam23(3300.728)
print(AIC.gsts) ; print(AIC.gscr) # lowestAIC=gam33(3296.121) and gam43(3295.407)
print(AIC.gscs) ; print(AIC.gscc) # lowest AIC=gam53(3290.296) and gam63(3307.973)

summary(gam13) 
# R-sq=0.359, GCV=24394, scale=22925, Dev=39.5% 
# signif pars=CO3, HCO3, CO2, Cl, NO2
gam.check(gam13)

summary(gam23)
# R-sq=0.359, GCV=24394, scale=22925, Dev=39.5% 
# sigpar=CO3, HCO3, CO2, Cl, NO2
gam.check(gam23)

summary(gam33)
# R-sq=0.358, GCV=23906, scale=22956, Dev=38.1%  
# sigpar=CO3, HCO3, CO2, Cl, NO2
gam.check(gam33)

summary(gam43)
# R-sq=0.372, GCV=23888, scale=22465, Dev=40.7% 
# sigpar=CO3, HCO3, CO2, Cl, NO2, SiO2
gam.check(gam43)

summary(gam53)
# R-sq=0.374, GCV=23369, scale=22403, Dev=39.7% 
# sigpar=CO3, HCO3, CO2, Cl, NO2
gam.check(gam53)

# Gamma
# using AIC
AIC.gmdef<-AIC(gam71,gam72,gam73)
AIC.gmtp<-AIC(gam81,gam82,gam83)
AIC.gmts<-AIC(gam91,gam92,gam93)
AIC.gmcr<-AIC(gam101,gam102,gam103)
AIC.gmcs<-AIC(gam111,gam112,gam113)
AIC.gmcc<-AIC(gam121,gam122,gam123)
print(AIC.gmdef) ; print(AIC.gmtp) # lowestAIC=gam71(3137.232) and gam81(3137.232)
print(AIC.gmts) ; print(AIC.gmcr) # lowestAIC=gam91(3133.663) and gam101(3138.866)
print(AIC.gmcs) ; print(AIC.gmcc) # lowestAIC=gam111(3135.529) and gam121(3163.347)
Post a Comment