Showing posts with label cikapundung. Show all posts
Showing posts with label cikapundung. Show all posts

17 March 2015

hydrogeological analysis using open source tools: case Cikapundung River

Dear friends,

The following slides (in Rmd) or pdf format are from my recent talk on Sarasehan Geologi Populer, which was held by Geological Survey of Indonesia. It covers various open source tools, with more focus on R, for geological and hydrogeological analysis. This talk tells some bits of my research on finding out interaction between groundwater and surface water interaction by analysing water quality pattern. I used R in this research. The slides contains some R codes example. The objective of this talk is to raise awareness of open source apps and how its contribution to reproducibility in science.

You can view and download:
@dasaptaerwin


05 August 2014

Preliminary "mgcv" result

Dear friends,

I managed to add some other predictors: anions, cumulative monthly rain, and lagged-1 monthly rain.

The following table shows the updated result.
Based on the table, I made a conceptual model about the water flow and the interaction between predictors in the system. As I've mentioned before, the surficial processes have stronger control to water chemistry than lithology. Hopefully the following sketch can give more spatial sense of the area. The strongest process detected by mgcv is NO3 enrichment in the river water as it gains water from groundwater flow. The other process is dilution  effect by river water as shown by decreasing pattern for elements like Cl, SO4, and Mn towards river.



New results

GAM Predictant Predictors Significance Family Residuals AIC Deviance GCV Rsq Pattern TrendRiver
10 logEC te(x,y), elv, lithology, cumRain, lagRain Y, Y, N, N, Y gamma good 3794.627 16.200 0.448 0.140 clear decreasing
11 logCO3 te(x,y), elv, lithology, cumRain, lagRain Y, Y, N, N, N gaussian poor 886.045 25.800 1.171 0.229 clear decreasing
12 logHCO3 te(x,y), elv, lithology, cumRain, lagRain N, Y, N, N, Y gaussian good 778.806 40.300 0.819 0.362 not clear
13 logCO2 te(x,y), elv, lithology, cumRain, lagRain Y, Y, N, Y, N gaussian poor 1035.771 14.200 1.950 0.111 clear decreasing
14 logCl te(x,y), elv, lithology, cumRain, lagRain Y, Y, N, N, Y gaussian good 814.054 31.000 0.922 0.267 not clear
15 logSO4 te(x,y), elv, lithology, cumRain, lagRain Y, Y, N, N, N gaussian good 1129.933 32.700 2.686 0.296 clear decreasing
16 logNO2 te(x,y), elv, lithology, cumRain, lagRain Y, Y, N, N, Y gaussian good 778.314 62.200 0.819 0.589 clear decreasing
17 logNO3 te(x,y), elv, lithology, cumRain, lagRain Y, N, N, Y, N gaussian good 1168.619 42.500 3.062 0.399 clear increasing
18 logFe te(x,y), elv, lithology, cumRain, lagRain Y, N, N, N, N gaussian good 649.973 12.300 0.528 0.083 clear decreasing
19 logCa te(x,y), elv, lithology, cumRain, lagRain N, Y, N, N, Y gaussian good 785.075 30.300 0.833 0.281 not clear
20 logMg te(x,y), elv, lithology, cumRain, lagRain Y, Y, N, N, Y gaussian good 730.692 41.600 0.696 0.371 not clear
21 logMn te(x,y), elv, lithology, cumRain, lagRain Y, Y, N, N, N gaussian poor -72.300 79.100 0.046 0.774 clear decreasing
22 logNa te(x,y), elv, lithology, cumRain, lagRain Y, Y, N, N, Y gaussian good 641.144 39.500 0.514 0.353 clear decreasing
23 logK te(x,y), elv, lithology, cumRain, lagRain N, N, N, N, N gaussian good 592.524 18.400 0.434 0.166 not clear






----
Previous post

This post would be the continuation of the previous serial posts on GAM using mgcv package. As I have posted before, I am looking for predictors that explain the interaction between groundwater and river water in Cikapundung watershed (Bandung, West Java, Indonesia).

The following table is the preliminary result. I'll make further explanation.




GAM Predictant Predictors Significance Family Residuals AIC Deviance (%) Notes Trend
10 EC (x,y), elv, lithology yes, yes, no gamma good 3805.445 10.6 clear pattern, decreasing trend towards river E-W
11 logCO3 (x,y), elv, lithology yes, yes, no gaussian poor 889.724 24.5 clear pattern, decreasing trend towards river E-W
13 logCO2 (x,y), elv, lithology yes, yes, no gaussian poor 1046.908 11.8 clear pattern, decreasing trend towards river NE-SW
15 logSO4 (x,y), elv, lithology yes, yes, no gaussian poor 1132.996 32.9 clear pattern, decreasing trend towards river NW-SE
17 NO3 (x,y), elv, lithology yes, no, no gaussian poor 1388.982 8.38 clear pattern, decreasing trend towards river NW-SE
20 logMg (x,y), elv, lithology no, yes, no gaussian poor 780.82 26.4 clear pattern, decreasing trend towards river E-W
21 Mn (x,y), elv, lithology yes, yes, no gaussian poor -971.85 74.4 clear pattern, decreasing trend towards river N-S, NE-SW
16 logNO2 (x,y), elv, lithology yes, no, no gaussian poor 1183.261 43.3 clear pattern, increasing trend towards river NE, SE
18 Fe (x,y), elv, lithology no, no, no gaussian poor -70.72 2.44 clear pattern, increasing trend towards river NW-SE
22 logNa (x,y), elv, lithology yes, yes, no gamma good 649.918 33.3 clear pattern, increasing trend towards river E-W
12 logHCO3 (x,y), elv, lithology no, yes, no gaussian good 847.1502 22.8 no clear pattern
14 logCl (x,y), elv, lithology yes, yes, no gaussian good 828.088 27.4 no clear pattern
19 logCa (x,y), elv, lithology no, yes, no gaussian poor 838 17.6 no clear pattern
23 logK (x,y), elv, lithology no, yes, no gaussian good 594.923 18.3 no clear pattern

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)

24 June 2014

[#R] Short tutorial variogram using geoR package

A tutorial how to make variogram: an exercise

A tutorial how to make variogram: an exercise

Author: Dasapta Erwin Irawan

Just starting to work on the variogram analysis of the Cikapundung dataset using geoR package. This post is connected to these previous posts:

The revised code can be downloaded here: http://goo.gl/7I4cuV

  1. #Pairs function short tutorial
  2. [#R] How to convert lat-long coordinates to UTM (easting-northing)
  3. Data preparation and plotting on #QGIS
  4. #R: Timeseries analysis try out
  5. #R: Hydromad on Cikapundung-update on monthly analysis

The following is the outline of this code is:

  1. loading geoR package and data
  2. assigning data frame as geo data and checking for duplicate coordinate
  3. making variogram with variog function
  4. fitting variogram with variofit function

I'll post the explanation later on.

# ANALYSIS FOR 1997 DATA

# LOADING LIBRARY
require("geoR")
## Loading required package: geoR
## Loading required package: sp
## Loading required package: MASS
## --------------------------------------------------------------
##  Analysis of geostatistical data
##  For an Introduction to geoR go to http://www.leg.ufpr.br/geoR
##  geoR version 1.7-4 (built on 2012-06-29) is now loaded
## --------------------------------------------------------------
# require('gstat') require('lattice') require('knitcitations') --------

# LOADING DATA
Data97 <- read.csv("data97utm.csv")

# ANALYSIS FOR 1ST GROUP PARAMETER: ELEVATION, EC, pH, HARD, TDS, TEMP Pairs
# analysis Insert Pairs Code

## Variogram analysis

#### ??? What is variogram? It's basically similar to autocorrelation but in
#### spatial arrangement.

## Binding necessary columns
Data97.1 <- Data97[, c("x", "y", "elv", "ec", "ph", "hard", "tds", "temp")]

### Assigning EC data as geodata (column 8)
EC97 <- as.geodata(Data97.1, coords.col = 1:2, data.col = 4)  #variogram for EC column 

### Checking any duplicate coordinates
dup.coords(EC97)
## NULL

### Building and plotting variogram
Var1.EC97 <- variog(EC97, trend = "1st")
## variog: computing omnidirectional variogram
Var2.EC97 <- variog(EC97, trend = "2nd")
## variog: computing omnidirectional variogram

#### Plot side by side
plot.new()
par(mfrow = c(2, 1))
plot(Var1.EC97, pch = 19, col = "blue", main = "1st order variogram")
plot(Var2.EC97, pch = 19, col = "red", main = "2nd order variogram")

plot of chunk unnamed-chunk-1


#### Plot overlaying
plot.new()
par(mfrow = c(1, 1))

plot of chunk unnamed-chunk-1

plot(Var1.EC97, pch = 19, col = "blue", main = "Variogram EC Data 1997")
par(new = TRUE)
plot(Var2.EC97, pch = 19, col = "red", xaxt = "n", yaxt = "n")

##### ??? Question: How to choose the 1st or 2nd order?

### Fitting 1st EC variogram
ini.vals <- expand.grid(seq(15000, 35000, by = 100), seq(1000, 2000, by = 100))
ols <- variofit(Var1.EC97, ini = ini.vals, fix.nug = TRUE, wei = "equal")
## variofit: covariance model used is matern 
## variofit: weights used: equal 
## variofit: minimisation function used: optim 
## variofit: searching for best initial value ... selected values:
##               sigmasq phi    tausq kappa
## initial.value "27500" "1400" "0"   "0.5"
## status        "est"   "est"  "fix" "fix"
## loss value: 470520606.080271
summary(ols)
## $pmethod
## [1] "OLS (ordinary least squares)"
## 
## $cov.model
## [1] "matern"
## 
## $spatial.component
## sigmasq     phi 
##   27509    1392 
## 
## $spatial.component.extra
## kappa 
##   0.5 
## 
## $nugget.component
## tausq 
##     0 
## 
## $fix.nugget
## [1] TRUE
## 
## $fix.kappa
## [1] TRUE
## 
## $practicalRange
## [1] 4170
## 
## $sum.of.squares
##     value 
## 470511572 
## 
## $estimated.pars
## sigmasq     phi 
##   27509    1392 
## 
## $weights
## [1] "equal"
## 
## $call
## variofit(vario = Var1.EC97, ini.cov.pars = ini.vals, fix.nugget = TRUE, 
##     weights = "equal")
## 
## attr(,"class")
## [1] "summary.variomodel"
wls <- variofit(Var1.EC97, ini = ini.vals, fix.nug = TRUE, wei = "npairs")
## variofit: covariance model used is matern 
## variofit: weights used: npairs 
## variofit: minimisation function used: optim 
## variofit: searching for best initial value ... selected values:
##               sigmasq phi    tausq kappa
## initial.value "24800" "1100" "0"   "0.5"
## status        "est"   "est"  "fix" "fix"
## loss value: 40933024994.4065
summary(wls)
## $pmethod
## [1] "WLS (weighted least squares)"
## 
## $cov.model
## [1] "matern"
## 
## $spatial.component
## sigmasq     phi 
##   24777    1114 
## 
## $spatial.component.extra
## kappa 
##   0.5 
## 
## $nugget.component
## tausq 
##     0 
## 
## $fix.nugget
## [1] TRUE
## 
## $fix.kappa
## [1] TRUE
## 
## $practicalRange
## [1] 3338
## 
## $sum.of.squares
##     value 
## 4.093e+10 
## 
## $estimated.pars
## sigmasq     phi 
##   24777    1114 
## 
## $weights
## [1] "npairs"
## 
## $call
## variofit(vario = Var1.EC97, ini.cov.pars = ini.vals, fix.nugget = TRUE, 
##     weights = "npairs")
## 
## attr(,"class")
## [1] "summary.variomodel"
lines(wls)
lines(ols, lty = 2, col = "blue")

plot of chunk unnamed-chunk-1


init.values <- expand.grid(seq(15000, 35000, by = 100), seq(1000, 2000, by = 100))
olsFit11.EC97 <- variofit(Var1.EC97, ini = init.values, fix.nug = T, wei = "equal")
## variofit: covariance model used is matern 
## variofit: weights used: equal 
## variofit: minimisation function used: optim 
## variofit: searching for best initial value ... selected values:
##               sigmasq phi    tausq kappa
## initial.value "27500" "1400" "0"   "0.5"
## status        "est"   "est"  "fix" "fix"
## loss value: 470520606.080271
olsFit12.EC97 <- variofit(Var1.EC97, ini = init.values, fix.nug = T, wei = "equal")
## variofit: covariance model used is matern 
## variofit: weights used: equal 
## variofit: minimisation function used: optim 
## variofit: searching for best initial value ... selected values:
##               sigmasq phi    tausq kappa
## initial.value "27500" "1400" "0"   "0.5"
## status        "est"   "est"  "fix" "fix"
## loss value: 470520606.080271
#### ??? Warning on both Fits: unreasonable initial value for sigmasq + nugget
#### (too low), why?
summary(olsFit11.EC97)
## $pmethod
## [1] "OLS (ordinary least squares)"
## 
## $cov.model
## [1] "matern"
## 
## $spatial.component
## sigmasq     phi 
##   27509    1392 
## 
## $spatial.component.extra
## kappa 
##   0.5 
## 
## $nugget.component
## tausq 
##     0 
## 
## $fix.nugget
## [1] TRUE
## 
## $fix.kappa
## [1] TRUE
## 
## $practicalRange
## [1] 4170
## 
## $sum.of.squares
##     value 
## 470511572 
## 
## $estimated.pars
## sigmasq     phi 
##   27509    1392 
## 
## $weights
## [1] "equal"
## 
## $call
## variofit(vario = Var1.EC97, ini.cov.pars = init.values, fix.nugget = T, 
##     weights = "equal")
## 
## attr(,"class")
## [1] "summary.variomodel"
summary(olsFit12.EC97)
## $pmethod
## [1] "OLS (ordinary least squares)"
## 
## $cov.model
## [1] "matern"
## 
## $spatial.component
## sigmasq     phi 
##   27509    1392 
## 
## $spatial.component.extra
## kappa 
##   0.5 
## 
## $nugget.component
## tausq 
##     0 
## 
## $fix.nugget
## [1] TRUE
## 
## $fix.kappa
## [1] TRUE
## 
## $practicalRange
## [1] 4170
## 
## $sum.of.squares
##     value 
## 470511572 
## 
## $estimated.pars
## sigmasq     phi 
##   27509    1392 
## 
## $weights
## [1] "equal"
## 
## $call
## variofit(vario = Var1.EC97, ini.cov.pars = init.values, fix.nugget = T, 
##     weights = "equal")
## 
## attr(,"class")
## [1] "summary.variomodel"

wlsFit11.EC97 <- variofit(Var1.EC97, ini = init.values, fix.nug = T, wei = "npairs")
## variofit: covariance model used is matern 
## variofit: weights used: npairs 
## variofit: minimisation function used: optim 
## variofit: searching for best initial value ... selected values:
##               sigmasq phi    tausq kappa
## initial.value "24800" "1100" "0"   "0.5"
## status        "est"   "est"  "fix" "fix"
## loss value: 40933024994.4065
wlsFit12.EC97 <- variofit(Var1.EC97, ini = init.values, fix.nug = T, wei = "npairs")
## variofit: covariance model used is matern 
## variofit: weights used: npairs 
## variofit: minimisation function used: optim 
## variofit: searching for best initial value ... selected values:
##               sigmasq phi    tausq kappa
## initial.value "24800" "1100" "0"   "0.5"
## status        "est"   "est"  "fix" "fix"
## loss value: 40933024994.4065
#### ??? Warning on both Fits: unreasonable initial value for sigmasq + nugget
#### (too low), why?  ??? What's the different between equal and npairs
summary(wlsFit11.EC97)
## $pmethod
## [1] "WLS (weighted least squares)"
## 
## $cov.model
## [1] "matern"
## 
## $spatial.component
## sigmasq     phi 
##   24777    1114 
## 
## $spatial.component.extra
## kappa 
##   0.5 
## 
## $nugget.component
## tausq 
##     0 
## 
## $fix.nugget
## [1] TRUE
## 
## $fix.kappa
## [1] TRUE
## 
## $practicalRange
## [1] 3338
## 
## $sum.of.squares
##     value 
## 4.093e+10 
## 
## $estimated.pars
## sigmasq     phi 
##   24777    1114 
## 
## $weights
## [1] "npairs"
## 
## $call
## variofit(vario = Var1.EC97, ini.cov.pars = init.values, fix.nugget = T, 
##     weights = "npairs")
## 
## attr(,"class")
## [1] "summary.variomodel"
summary(wlsFit12.EC97)
## $pmethod
## [1] "WLS (weighted least squares)"
## 
## $cov.model
## [1] "matern"
## 
## $spatial.component
## sigmasq     phi 
##   24777    1114 
## 
## $spatial.component.extra
## kappa 
##   0.5 
## 
## $nugget.component
## tausq 
##     0 
## 
## $fix.nugget
## [1] TRUE
## 
## $fix.kappa
## [1] TRUE
## 
## $practicalRange
## [1] 3338
## 
## $sum.of.squares
##     value 
## 4.093e+10 
## 
## $estimated.pars
## sigmasq     phi 
##   24777    1114 
## 
## $weights
## [1] "npairs"
## 
## $call
## variofit(vario = Var1.EC97, ini.cov.pars = init.values, fix.nugget = T, 
##     weights = "npairs")
## 
## attr(,"class")
## [1] "summary.variomodel"

### Fitting 2nd EC variogram
init.values <- expand.grid(seq(15000, 35000, by = 100), seq(1000, 2000, by = 100))
olsFit21.EC97 <- variofit(Var2.EC97, ini = init.values, fix.nug = T, wei = "equal")
## variofit: covariance model used is matern 
## variofit: weights used: equal 
## variofit: minimisation function used: optim 
## variofit: searching for best initial value ... selected values:
##               sigmasq phi    tausq kappa
## initial.value "28100" "2000" "0"   "0.5"
## status        "est"   "est"  "fix" "fix"
## loss value: 515433457.115543
olsFit22.EC97 <- variofit(Var2.EC97, ini = init.values, fix.nug = T, wei = "equal")
## variofit: covariance model used is matern 
## variofit: weights used: equal 
## variofit: minimisation function used: optim 
## variofit: searching for best initial value ... selected values:
##               sigmasq phi    tausq kappa
## initial.value "28100" "2000" "0"   "0.5"
## status        "est"   "est"  "fix" "fix"
## loss value: 515433457.115543
#### ??? Warning on both Fits: unreasonable initial value for sigmasq + nugget
#### (too low), why?
summary(olsFit21.EC97)
## $pmethod
## [1] "OLS (ordinary least squares)"
## 
## $cov.model
## [1] "matern"
## 
## $spatial.component
## sigmasq     phi 
##   28197    2117 
## 
## $spatial.component.extra
## kappa 
##   0.5 
## 
## $nugget.component
## tausq 
##     0 
## 
## $fix.nugget
## [1] TRUE
## 
## $fix.kappa
## [1] TRUE
## 
## $practicalRange
## [1] 6342
## 
## $sum.of.squares
##     value 
## 515162980 
## 
## $estimated.pars
## sigmasq     phi 
##   28197    2117 
## 
## $weights
## [1] "equal"
## 
## $call
## variofit(vario = Var2.EC97, ini.cov.pars = init.values, fix.nugget = T, 
##     weights = "equal")
## 
## attr(,"class")
## [1] "summary.variomodel"
summary(olsFit22.EC97)
## $pmethod
## [1] "OLS (ordinary least squares)"
## 
## $cov.model
## [1] "matern"
## 
## $spatial.component
## sigmasq     phi 
##   28197    2117 
## 
## $spatial.component.extra
## kappa 
##   0.5 
## 
## $nugget.component
## tausq 
##     0 
## 
## $fix.nugget
## [1] TRUE
## 
## $fix.kappa
## [1] TRUE
## 
## $practicalRange
## [1] 6342
## 
## $sum.of.squares
##     value 
## 515162980 
## 
## $estimated.pars
## sigmasq     phi 
##   28197    2117 
## 
## $weights
## [1] "equal"
## 
## $call
## variofit(vario = Var2.EC97, ini.cov.pars = init.values, fix.nugget = T, 
##     weights = "equal")
## 
## attr(,"class")
## [1] "summary.variomodel"

wlsFit21.EC97 <- variofit(Var2.EC97, ini = init.values, fix.nug = T, wei = "npairs")
## variofit: covariance model used is matern 
## variofit: weights used: npairs 
## variofit: minimisation function used: optim 
## variofit: searching for best initial value ... selected values:
##               sigmasq phi    tausq kappa
## initial.value "23000" "1100" "0"   "0.5"
## status        "est"   "est"  "fix" "fix"
## loss value: 34351583362.0137
wlsFit22.EC97 <- variofit(Var2.EC97, ini = init.values, fix.nug = T, wei = "npairs")
## variofit: covariance model used is matern 
## variofit: weights used: npairs 
## variofit: minimisation function used: optim 
## variofit: searching for best initial value ... selected values:
##               sigmasq phi    tausq kappa
## initial.value "23000" "1100" "0"   "0.5"
## status        "est"   "est"  "fix" "fix"
## loss value: 34351583362.0137
#### ??? Warning on both Fits: unreasonable initial value for sigmasq + nugget
#### (too low), why?  ??? What's the different between equal and npairs
summary(wlsFit21.EC97)
## $pmethod
## [1] "WLS (weighted least squares)"
## 
## $cov.model
## [1] "matern"
## 
## $spatial.component
## sigmasq     phi 
##   22975    1127 
## 
## $spatial.component.extra
## kappa 
##   0.5 
## 
## $nugget.component
## tausq 
##     0 
## 
## $fix.nugget
## [1] TRUE
## 
## $fix.kappa
## [1] TRUE
## 
## $practicalRange
## [1] 3375
## 
## $sum.of.squares
##     value 
## 3.434e+10 
## 
## $estimated.pars
## sigmasq     phi 
##   22975    1127 
## 
## $weights
## [1] "npairs"
## 
## $call
## variofit(vario = Var2.EC97, ini.cov.pars = init.values, fix.nugget = T, 
##     weights = "npairs")
## 
## attr(,"class")
## [1] "summary.variomodel"
summary(wlsFit22.EC97)
## $pmethod
## [1] "WLS (weighted least squares)"
## 
## $cov.model
## [1] "matern"
## 
## $spatial.component
## sigmasq     phi 
##   22975    1127 
## 
## $spatial.component.extra
## kappa 
##   0.5 
## 
## $nugget.component
## tausq 
##     0 
## 
## $fix.nugget
## [1] TRUE
## 
## $fix.kappa
## [1] TRUE
## 
## $practicalRange
## [1] 3375
## 
## $sum.of.squares
##     value 
## 3.434e+10 
## 
## $estimated.pars
## sigmasq     phi 
##   22975    1127 
## 
## $weights
## [1] "npairs"
## 
## $call
## variofit(vario = Var2.EC97, ini.cov.pars = init.values, fix.nugget = T, 
##     weights = "npairs")
## 
## attr(,"class")
## [1] "summary.variomodel"

plot.new()
par(mfrow = c(1, 2))
plot(Var1.EC97, main = "1st order EC Semivar", pch = 19, col = "blue")
lines(wlsFit11.EC97, lty = 2, col = "green")
lines(olsFit11.EC97, lty = 3, col = "blue")
lines(wlsFit12.EC97, lty = 2, col = "grey")
lines(olsFit12.EC97, lty = 3, col = "red")

plot(Var2.EC97, main = "2nd order EC Semivar", pch = 19, col = "red")
lines(wlsFit21.EC97, lty = 2, col = "green")
lines(olsFit21.EC97, lty = 3, col = "blue")
lines(wlsFit22.EC97, lty = 2, col = "grey")
lines(olsFit22.EC97, lty = 3, col = "red")

plot of chunk unnamed-chunk-1


# legend('topleft',c('ordinary least squares','weighted least
# squares'),lty=c(2,1), lwd=c(1,1), col=c('blue','black'))

####### REFERENCES

# R Core Team (2014). R: A language and environment for statistical
# computing. R Foundation for Statistical Computing, Vienna, Austria. URL
# http://www.R-project.org/.

# RIBEIRO JR., P.J. and DIGGLE, P.J. (2001) geoR: A package for
# geostatistical analysis. R-NEWS Vol 1, No 2. ISSN 1609-3631.