13 July 2014

#R: GAM exercise using mgcv package (the workflow)

mgcv1

Dear friends,

This post is related to:

  1. Hydromad exercise
  2. Variogram exercise
  3. Pairs exercise

I am still finishing my example of GAM (Generalised Additive Model) using MGCV package (by MGCV package)(by Simon Wood). I've been applying some variations of parameter setting. Next step will be choosing which one is the best model. In this practice I collaborate my code with Farzina Akter (a PhD student from Univ. of Sydney).

Our work flow would be like this:

  1. Dataset preparation
  2. GAM fitting using Gaussian family.
  3. GAM fitting using Gamma family
  4. Choosing the best model using AIC and ANOVA
  5. Predict GAM for new data

Model try outs

Our models try outs can be seen in this table.

GAM Family Smoothing Code
gam1 Gaussian no smoothing gam1<-gam(y ~ s(cov1) + s(cov2) + ... + s(cov-n),data=data.frame)
gam2 Gaussian thin plate (tp) gam2<-gam(y ~ s(cov1,bs="tp") + s(cov2,bs="tp") + ... + s(cov-n,bs="tp"),data=data.frame)
gam3 Gaussian thin shrinkage (ts) gam3<-gam(y ~ s(cov1,bs="ts") + s(cov2,bs="ts") + ... + s(cov-n,bs="ts"),data=data.frame)
gam4 Gaussian cubic reg spline (cr) gam4<-gam(y ~ s(cov1,bs="cr") + s(cov2,bs="cr") + ... + s(cov-n,bs="cr"),data=data.frame)
gam5 Gaussian cubic shrinkage spline (cs) gam5<-gam(y ~ s(cov1,bs="cs") + s(cov2,bs="cs") + ... + s(cov-n,bs="cs"),data=data.frame)
gam6 Gaussian cubic cyclic spline (cc) gam6<-gam(y ~ s(cov1,bs="cc") + s(cov2,bs="cc") + ... + s(cov-n,bs="cc"),data=data.frame)
gam7 Gamma no smoothing gam7<-gam(y ~ s(cov1) + s(cov2) + ... + s(cov-n),Gamma (link="log"),data=data.frame)
gam8 Gamma thin plate (tp) gam8<-gam(y ~ s(cov1,bs="tp") + s(cov2,bs="tp") + ... + s(cov-n,bs="tp"),Gamma (link="log"),data=data.frame)
gam9 Gamma thin shrinkage (ts) gam9<-gam(y ~ s(cov1,bs="ts") + s(cov2,bs="ts") + ... + s(cov-n,bs="ts"),Gamma (link="log"),data=data.frame)
gam10 Gamma cubic reg spline (cr) gam10<-gam(y ~ s(cov1,bs="cr") + s(cov2,bs="cr") + ... + s(cov-n,bs="cr"),Gamma (link="log"),data=data.frame)
gam11 Gamma cubic shrinkage spline (cs) gam11<-gam(y ~ s(cov1,bs="cs") + s(cov2,bs="cs") + ... + s(cov-n,bs="cs"),Gamma (link="log"),data=data.frame)
gam12 Gamma cubic cyclic spline (cc) gam1<-gam(EC ~ s(cov1,bs="cc") + s(cov2,bs="cc") + ... + s(cov-n,bs="cc"),data=data.frame)

You can see the change on parameter bs and Gamma(link="log"). You can rename all y and the cov based on your model.

After each of the GAM model, you can add the following lines (eg for gam1)

summary(gam1) gam.check(gam1) plot(gam1,pages=1) AIC(gam1) plot(gam1,residuals=T,pages=1)

Model summary

Then after you run all of the models you can summarise all the results with the following lines.

  • Anova test for family=Gaussian, link=identity, default

anovaGaussian<-anova(gam1,gam2,gam3,gam4,gam5,gam6,test="Chisq")

  • Anova test for family=Gamma, link=log

anovaGamma<-anova(gam7,gam8,gam9,gam10,gam11,gam12, test="Chisq")

  • AIC test for family=Gaussian, link=identity, default

AICGaussian<-AIC(gam1,gam2,gam3,gam4,gam5,gam6,test="Chisq")

  • AIC test for family=Gamma, link=log

AICGamma<-anova(gam7,gam8,gam9,gam10,gam11,gam12, test="Chisq")

  • Print the anova and AIC

print(anovaGaussian) print(anovaGamma) print(AICGaussian) print(AICGamma)

Or if you use xtable package, you can write the following lines to produce html table (or LaTeX table)

print(xtable(AIC1)) print(xtable(AIC2))

Choosing the best model

Then choose the best model by looking at the smallest AIC and anova result as additional model evaluation.

Thanks for visiting my blog.

(the md and pdf files are available at onlinewaterbook.wordpress.com)

This post is related to:

  1. Hydromad exercise
  2. Variogram exercise
  3. Pairs exercise
  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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#---
# 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  : 8 July 2014
#---

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

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

##########################
# PAIRS ANALYSIS #########
##########################

# Data group 1: Physical parameters
group1 <- data[,c("x","y","ec","elv","aq","ph","hard","tds","temp","eh","Q")]
pairs(group1,labels=colnames(group1),
      main="Physical parameter", 
      pch=21,bg=c('red','green3','blue','yellow')
      [unclass(data$aq)],
      upper.panel=NULL)
legend(x=0.6,y=0.8,levels(data$aq), 
       pt.bg=c('red','green3','blue','yellow'), 
       pch=21,bty='n',ncol=2)

# Data group 2: Cations (unit = ppm)
group2 <- data[,c("x","y","ec","elv","Ca","Mg","Fe","Mn","K","Na","Ca")]
pairs(group2,labels=colnames(group2),main="Cations", 
      pch=21,bg=c('red','green3','blue','yellow')
      [unclass(data$aq)],
      upper.panel=NULL)
legend(x=0.6,y=0.8,legend=levels(data$aq), 
       pt.bg=c('red','green3','blue','yellow'), 
       pch=21, ncol=2, bty ='n')

## Data group 3: Anions (unit = ppm)
group3 = data[,c("x","y","ec","CO3","HCO3","CO2","Cl","SO4","NO2",
      "NO3","SiO2")]
pairs(group3,labels=colnames(group3),main="Anions", 
      pch=21,bg=c('red','green3','blue','yellow')
      [unclass(data$aq)],upper.panel=NULL)
#par(xpd='TRUE')
legend(x=0.6,y=0.8,legend=levels(data$aq), 
       pt.bg=c('red','green3','blue','yellow'), 
       pch=21, ncol=2, bty ='n')

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

# load library and data
require("mgcv")
data <- read.csv("data97utm.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<-5 # 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)
k2<-3
gam12<-gam(ec ~ s(x,k=k2) + s(y,k=k2) + s(Ca,k=k2) + 
             s(Mg,k=k2) + s(Fe,k=k2) + s(Mn,k=k2) + 
             s(K,k=k2) + s(Na,k=k2), data=group2)
k3<-5
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<-5
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
gam22<-gam(ec ~ s(x,k=k2,bs="tp") + s(y,k=k2,bs="tp") + s(Ca,k=k2,bs="tp") + 
             s(Mg,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
bsm<-"ts"
gam32<-gam(ec ~ s(x,k=k2,bs=bsm) + s(y,k=k2,bs=bsm) + s(Ca,k=k2,bs=bsm) + 
             s(Mg,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
gam42<-gam(ec ~ s(x,k=k2,bs=bsm) + s(y,k=k2,bs=bsm) + s(Ca,k=k2,bs=bsm) + 
             s(Mg,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
gam52<-gam(ec ~ s(x,k=k2,bs=bsm) + s(y,k=k2,bs=bsm) + s(Ca,k=k2,bs=bsm) + 
             s(Mg,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
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<-3
gam62<-gam(ec ~ s(x,k=k2,bs=bsm) + s(y,k=k2,bs=bsm) + s(Ca,k=k2,bs=bsm) + 
             s(Mg,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
gam72<-gam(ec ~ s(x,k=k2) + s(y,k=k2) + s(Ca,k=k2) + 
             s(Mg,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
gam82<-gam(ec ~ s(x,k=k2,bs="tp") + s(y,k=k2,bs="tp") + s(Ca,k=k2,bs="tp") + 
             s(Mg,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
bsm<-"ts"
gam92<-gam(ec ~ s(x,k=k2,bs=bsm) + s(y,k=k2,bs=bsm) + s(Ca,k=k2,bs=bsm) + 
             s(Mg,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
gam102<-gam(ec ~ s(x,k=k2,bs=bsm) + s(y,k=k2,bs=bsm) + s(Ca,k=k2,bs=bsm) + 
             s(Mg,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
gam112<-gam(ec ~ s(x,k=k2,bs=bsm) + s(y,k=k2,bs=bsm) + s(Ca,k=k2,bs=bsm) + 
             s(Mg,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
gam122<-gam(ec ~ s(x,k=k2,bs=bsm) + s(y,k=k2,bs=bsm) + s(Ca,k=k2,bs=bsm) + 
             s(Mg,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)

# Same situation as GAUSSIAN family: Dropping "cc" model, causing error, don't have cyclic pattern

######### 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) # dropped
print(AIC.gsdef) ; print(AIC.gstp) # lowestAIC=gam13(572.5448) and gam23(572.5448)
print(AIC.gsts) ; print(AIC.gscr) # lowestAIC=gam33(571.6253) and gam43(587.7679)
print(AIC.gscs) # lowest AIC=gam53(593.4830)
#print(AIC.gscc) # dropped

summary(gam13) 
# R-sq=0.897, GCV=6765, scale=3120, sigpar=all,ytrend
gam.check(gam13)

summary(gam23)
# R-sq=0.897, GCV=6765, scale=3120, sigpar=all,ytrend,-HCO3
gam.check(gam23)

summary(gam33)
# R-sq=0.899, GCV=6676.2, scale=3065.4, sigpar=all,xytrend
gam.check(gam33)

summary(gam43)
# R-sq=0.861, GCV=7623.9, scale=4213.8, sigpar=all,xytrend,-Cl,NO2
gam.check(gam43)

summary(gam53)
# R-sq=0.845, GCV=8920, scale=4695.3, sigpar=all,ytrend,-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) # dropped
print(AIC.gmdef) ; print(AIC.gmtp) # lowestAIC=gam73(603.1337) and gam83(603.1337)
print(AIC.gmts) ; print(AIC.gmcr) # lowestAIC=gam93(601.2405) and gam103(598.4275)
print(AIC.gmcs) # lowestAIC=gam111(597.2866)
#print(AIC.gmcc) # dropped

# using summary and gam.check
summary(gam73)
# R-sq=0.66, GCV=0.20824, scale=0.13229, sigpar=all,ytrend,(-HCO3,Cl,SO4)
gam.check(gam73)

summary(gam83)
# R-sq=0.66, GCV=0.20824, scale=0.13229, sigpar=all,ytrend,(-HCO3,Cl,SO4)
gam.check(gam83)

summary(gam93)
# R-sq=0.664, GCV=0.19493, scale=0.12899, sigpar=all,ytrend,(-HCO3,Cl,SO4)
gam.check(gam93)

summary(gam103)
# R-sq=0.617, GCV=0.20011, scale=0.11909, sigpar=all,ytrend,(-HCO3,Cl,SO4,NO2)
gam.check(gam103)

summary(gam111)
# R-sq=0.602, GCV=0.17701, scale=0.12065, sigpar=all,ytrend,(-elv,ph,hard)
gam.check(gam111)
Post a Comment