@@ -20,6 +20,7 @@ mutable struct KrigingState{D<:AbstractGeoTable,F,A}
20
20
LHS:: F
21
21
RHS:: A
22
22
ncon:: Int
23
+ miss:: Vector{Int}
23
24
end
24
25
25
26
"""
@@ -51,21 +52,22 @@ status(fitted::FittedKriging) = issuccess(fitted.state.LHS)
51
52
52
53
function fit (model:: KrigingModel , data)
53
54
# initialize Kriging system
54
- LHS, RHS, ncon = initkrig (model, domain ( data) )
55
+ LHS, RHS, ncon, miss = initkrig (model, data)
55
56
56
57
# factorize LHS
57
58
FLHS = lhsfactorize (model, LHS)
58
59
59
60
# record Kriging state
60
- state = KrigingState (data, FLHS, RHS, ncon)
61
+ state = KrigingState (data, FLHS, RHS, ncon, miss )
61
62
62
63
FittedKriging (model, state)
63
64
end
64
65
65
66
# initialize Kriging system
66
- function initkrig (model:: KrigingModel , domain )
67
+ function initkrig (model:: KrigingModel , data )
67
68
fun = model. fun
68
- dom = domain
69
+ dom = domain (data)
70
+ tab = values (data)
69
71
70
72
# retrieve matrix parameters
71
73
V, (_, nobs, nvar) = GeoStatsFunctions. matrixparams (fun, dom)
@@ -84,10 +86,16 @@ function initkrig(model::KrigingModel, domain)
84
86
# set blocks of constraints
85
87
lhsconstraints! (model, LHS, nvar, dom)
86
88
89
+ # find locations with missing values
90
+ miss = missingindices (tab)
91
+
92
+ # knock out entries with missing values
93
+ lhsmissings! (LHS, ncon, miss)
94
+
87
95
# pre-allocate memory for RHS
88
96
RHS = similar (LHS, nrow, nvar)
89
97
90
- LHS, RHS, ncon
98
+ LHS, RHS, ncon, miss
91
99
end
92
100
93
101
# choose appropriate factorization of LHS
122
130
# no adjustments in case of general geostatistical functions
123
131
lhsadjustments! (LHS, fun, dom) = nothing
124
132
133
+ # find locations with missing values
134
+ function missingindices (tab)
135
+ cols = Tables. columns (tab)
136
+ vars = Tables. columnnames (cols)
137
+ nvar = length (vars)
138
+
139
+ # find locations with missing values and
140
+ # map to entries of blocks in final matrix
141
+ entries = map (1 : nvar) do j
142
+ vals = Tables. getcolumn (cols, vars[j])
143
+ inds = findall (ismissing, vals)
144
+ [nvar * (i - 1 ) + j for i in inds]
145
+ end
146
+
147
+ # sort indices to improve locality
148
+ sort (reduce (vcat, entries))
149
+ end
150
+
151
+ # knock out entries with missing values
152
+ function lhsmissings! (LHS, ncon, miss)
153
+ nrow = size (LHS, 1 )
154
+ nfun = nrow - ncon
155
+ @inbounds for j in miss, i in 1 : nfun
156
+ LHS[i, j] = 0
157
+ end
158
+ @inbounds for j in 1 : nfun, i in miss
159
+ LHS[i, j] = 0
160
+ end
161
+
162
+ nothing
163
+ end
164
+
125
165
# -----------------
126
166
# PREDICTION STEP
127
167
# -----------------
@@ -159,18 +199,22 @@ function krigmean(fitted::FittedKriging, weights::KrigingWeights, vars)
159
199
sum (1 : n) do p
160
200
λₚ = @view λ[p: k: end , j]
161
201
zₚ = Tables. getcolumn (cols, vars[p])
162
- sum (i -> λₚ[i] * zₚ[i], eachindex (λₚ, zₚ))
202
+ sum (i -> λₚ[i] ⦿ zₚ[i], eachindex (λₚ, zₚ))
163
203
end
164
204
end
165
- elseif k == 1
205
+ else # k == 1
166
206
@inbounds map (1 : n) do p
167
207
λₚ = @view λ[:, 1 ]
168
208
zₚ = Tables. getcolumn (cols, vars[p])
169
- sum (i -> λₚ[i] * zₚ[i], eachindex (λₚ, zₚ))
209
+ sum (i -> λₚ[i] ⦿ zₚ[i], eachindex (λₚ, zₚ))
170
210
end
171
211
end
172
212
end
173
213
214
+ # handle missing values in linear combination
215
+ ⦿ (λ, z) = λ * z
216
+ ⦿ (λ, z:: Missing ) = 0
217
+
174
218
function predictvar (fitted:: FittedKriging , weights:: KrigingWeights , gₒ)
175
219
RHS = fitted. state. RHS
176
220
fun = fitted. model. fun
@@ -244,6 +288,7 @@ function weights(fitted::FittedKriging, gₒ)
244
288
LHS = fitted. state. LHS
245
289
RHS = fitted. state. RHS
246
290
ncon = fitted. state. ncon
291
+ miss = fitted. state. miss
247
292
dom = domain (fitted. state. data)
248
293
fun = fitted. model. fun
249
294
@@ -259,6 +304,9 @@ function weights(fitted::FittedKriging, gₒ)
259
304
# set blocks of constraints
260
305
rhsconstraints! (fitted, gₒ′)
261
306
307
+ # knock out entries with missing values
308
+ rhsmissings! (RHS, miss)
309
+
262
310
# solve Kriging system
263
311
W = LHS \ RHS
264
312
295
343
# no adjustments in case of general geostatistical functions
296
344
rhsadjustments! (RHS, fun, dom) = nothing
297
345
346
+ # knock out entries with missing values
347
+ function rhsmissings! (RHS, miss)
348
+ nvar = size (RHS, 2 )
349
+ @inbounds for j in 1 : nvar, i in miss
350
+ RHS[i, j] = 0
351
+ end
352
+
353
+ nothing
354
+ end
355
+
298
356
# the following functions are implemented by
299
357
# all variants of Kriging (e.g., SimpleKriging)
300
358
function nconstraints end
0 commit comments