File: cov.test2.R

package info (click to toggle)
r-cran-fields 16.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,972 kB
  • sloc: fortran: 1,021; ansic: 288; sh: 35; makefile: 2
file content (312 lines) | stat: -rw-r--r-- 9,516 bytes parent folder | download | duplicates (2)
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
#
# fields  is a package for analysis of spatial data written for
# the R software environment.
# Copyright (C) 2022 Colorado School of Mines
# 1500 Illinois St., Golden, CO 80401
# Contact: Douglas Nychka,  douglasnychka@gmail.edu,
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with the R software environment if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
# or see http://www.r-project.org/Licenses/GPL-2
##END HEADER
##END HEADER


suppressMessages(library(fields))
options( echo=FALSE)
test.for.zero.flag<- 1
data(ozone2)
y<- ozone2$y[16,]
x<- ozone2$lon.lat
#
# Omit the NAs
good<- !is.na( y)
x<- x[good,]
y<- y[good]
x1<- x[1:20,]
x2<- x[1:10,]

look<- exp(-1*rdist(x1,x2)/4)
look2<- stationary.cov( x1,x2, aRange=4)
test.for.zero( look, look2)

V<- matrix( c(2,1,0,4), 2,2)
Vi<- solve( V)

u1<- t(Vi%*% t(x1))
u2<- t(Vi%*% t(x2))


look<- exp(-1*rdist(u1,u2))
look2<- stationary.cov( x1,x2, V= V)
test.for.zero( look, look2)

look<- Wendland(rdist(u1,u2), k=3, dimension=2)
look2<- stationary.cov( x1,x2, V= V, Covariance = "Wendland",
                        k=3, dimension=2)


test.for.zero( look, look2)


x1<- x[1:5,]
x2<- x[2:6,]
V<- matrix( c(2,1,0,4), 2,2)
Vi<- solve( V)

u1<- x1
u2<- x2

look1a<- exp(-1*rdist(u1,u2))
look1b<-  Wendland(rdist(u1,u2),
                   k=3, dimension=2, aRange= 1)
look1<- look1a*look1b
look2<- stationary.taper.cov( x1,x2, aRange=1,
        Taper.args=list( aRange=1,k=3, dimension=2), 
        verbose=FALSE)
test.for.zero( look1, as.matrix(look2))


u1<- t(Vi%*% t(x1))
u2<- t(Vi%*% t(x2))


look1a<- exp(-1*rdist(u1,u2))
look1b<-  Wendland(rdist(u1,u2),
                   k=3, dimension=2, aRange= 1.5)
look1<- look1a*look1b
look2<- stationary.taper.cov( x1,x2,V=V,
                    Taper.args=list( aRange=1.5,k=3, dimension=2),
                    verbose=FALSE)
test.for.zero( look1, as.matrix(look2))


u1<- t(Vi%*% t(x1))
u2<- t(Vi%*% t(x2))


look1a<- Matern(rdist(u1,u2), smoothness=1.5)
look1b<-  Wendland(rdist(u1,u2),
                   k=3, dimension=2, aRange= 1.5)
look1<- look1a*look1b
look2<- stationary.taper.cov( x1,x2,V=V,Covariance=Matern, smoothness=1.5,
                        Taper.args=list( aRange=1.5,k=3, dimension=2), verbose=FALSE)
test.for.zero( look1, as.matrix(look2))


# some tests of great circle distance


stationary.taper.cov( x[1:3,],x[1:10,] , aRange=200, Taper.args= 
                        list(k=2,aRange=300, dimension=2),
                      Dist.args=list( method="greatcircle") )-> temp

# temp is now a tapered 3X10 cross covariance matrix in sparse format. 
# should be identical to
# the direct matrix product

temp2<- Exponential( rdist.earth(x[1:3,],x[1:10,]), range=200) * 
  Wendland(rdist.earth(x[1:3,],x[1:10,]),
            aRange= 300, k=2, dimension=2)

test.for.zero(  as.matrix(temp), temp2, tol=2e-6,
                 tag="taper with great circle")

# example of calling the taper version directly 
# Note that default covariance is exponential and default taper is 
# Wendland (k=2).

stationary.taper.cov( x[1:3,],x[1:10,] , aRange=1.5, Taper.args= 
                        list(k=2,aRange=2.0, dimension=2) )-> temp
# temp is now a tapered 5X10 cross covariance matrix in sparse format. 
# should be identical to
# the direct matrix product

temp2<- Exp.cov( x[1:3,],x[1:10,], aRange=1.5) * 
  Wendland(rdist(x[1:3,],x[1:10,]),
           aRange= 2.0, k=2, dimension=2)

test.for.zero(  as.matrix(temp), temp2, tag= "high level test of taper cov")

stationary.taper.cov( x[1:3,],x[1:10,] , range=1.5,
                      Taper.args= list(k=2,aRange=2.0,
                                       dimension=2) )-> temp

test.for.zero(  as.matrix(temp), temp2,
                tol=1e-7,
                tag= "high level test of taper cov")



##### Test precomputing distance matrix
#

y<- ozone2$y[16,]
x<- ozone2$lon.lat

#
# Omit the NAs

good<- !is.na( y)
x<- x[good,]
y<- y[good]

#####test that stationary.cov returns the same result when passed distance matrix:

#with x1 == x2:

x1<- x[1:20,]
compactDistMat = rdist(x1, compact=TRUE)
distMat = rdist(x1)
look<- stationary.cov(x1, aRange=4)
look2 <- stationary.cov(x1, aRange=4, distMat = compactDistMat)
look3 <- stationary.cov(x1, aRange=4, distMat = distMat)
test.for.zero( look, look2, tag="stationary.cov versus stationary.cov compact distMat")
test.for.zero( look, look3, tag="stationary.cov versus stationary.cov matrix distMat")

#with x1 != x2:

x2=x[1:10,]
distMat = rdist(x1, x2)
look<- stationary.cov(x1, x2, aRange=4)
look2 <- stationary.cov(x1, x2, aRange=4, distMat = distMat)
test.for.zero( look, look2, tag="stationary.cov versus stationary.cov asymmetric distMat")

#####test that stationary.cov returns the same result when passed distance matrix:

#with x1 == x2:
distMat = rdist(x1, x1)
compactDistMat = rdist(x1, compact=TRUE)

look<- Exp.cov(x1, aRange=4)
look2 <- Exp.cov(x1, aRange=4, distMat = compactDistMat)
look3 <- Exp.cov(x1, aRange=4, distMat = distMat)
test.for.zero( look, look2, tag="Exp.cov versus Exp.cov compact distMat")
test.for.zero( look, look3, tag="Exp.cov versus Exp.cov matrix distMat")

#with x1 != x2:

x1<- x[1:20,]
x2=x[1:10,]
distMat = rdist(x1, x2)
look<- Exp.cov(x1, x2, aRange=4)
look2 <- Exp.cov(x1, x2, aRange=4, distMat = distMat)
test.for.zero( look, look2, tag="Exp.cov versus Exp.cov asymmetric distMat")

##### test for correct value when using C argument:

Ctest<- rnorm(10)

#with x1 == x2:

x1 = x[1:10,]
compactDistMat = rdist(x1, compact=TRUE)
distMat = rdist(x1, x1)

temp1<- stationary.cov( x1, C= Ctest, aRange=4 )
temp2 = stationary.cov( x1, C= Ctest, aRange=4, distMat=compactDistMat )
temp3 = stationary.cov( x1, C= Ctest, aRange=4, distMat=distMat )

exp1<- Exp.cov( x1, C= Ctest, aRange=4 )
exp2 = Exp.cov( x1, C= Ctest, aRange=4, distMat=compactDistMat )
exp3 = Exp.cov( x1, C= Ctest, aRange=4, distMat=distMat )

test.for.zero(temp1, temp2, tag="stationary.cov vs stationary.cov with C set, compact distMat")
test.for.zero(temp1, temp3, tag="stationary.cov vs stationary.cov with C set, matrix distMat")
test.for.zero(temp1, exp1, tag="stationary.cov vs Exp.cov with C set, no distMat")
test.for.zero(temp2, exp2, tag="stationary.cov vs Exp.cov with C set, compact distMat")
test.for.zero(temp3, temp3, tag="stationary.cov vs Exp.cov with C set, matrix distMat")

#with x1 != x2:

x1 = x
x2 = x[1:10,]

distMat = rdist(x1, x1)

temp1<- stationary.cov( x1, x2, C= Ctest, aRange=4 )
temp2 = stationary.cov( x1, x2, C= Ctest, aRange=4, distMat=distMat )
exp1 <- Exp.cov( x1, x2, C= Ctest, aRange=4 )
exp2 = Exp.cov( x1, x2, C= Ctest, aRange=4, distMat=distMat )

test.for.zero(temp1, temp2, tag="stationary.cov vs stationary.cov with C set and asymmetric distMat given")
test.for.zero(exp1, exp2, tag="Exp.cov vs Exp.cov with C set and asymmetric distMat given")


##### test covariance functions for onlyUpper=TRUE
#

distMat = rdist(x1, x1)
compactDistMat = rdist(x1, compact=TRUE)
out1 = stationary.cov(x1, onlyUpper=TRUE)
exp1 = Exp.cov(x1, onlyUpper=TRUE)
out2 = stationary.cov(x1, onlyUpper=TRUE, distMat=compactDistMat)
exp2 = Exp.cov(x1, onlyUpper=TRUE, distMat=compactDistMat)
out3 = stationary.cov(x1, onlyUpper=TRUE, distMat=distMat)
exp3 = Exp.cov(x1, onlyUpper=TRUE, distMat=distMat)

test.for.zero( out2[upper.tri(out1)], out3[upper.tri(exp1)], tag="onlyUpper=TRUE: stationary.cov versus Exp.cov")
test.for.zero( out2[upper.tri(out1)], out3[upper.tri(out2)], tag="onlyUpper=TRUE: stationary.cov versus stationary.cov with compactDistMat")
test.for.zero( out2[upper.tri(out1)], out3[upper.tri(exp2)], tag="onlyUpper=TRUE: stationary.cov versus Exp.cov with compactDistMat")
test.for.zero( out2[upper.tri(out1)], out3[upper.tri(out3)], tag="onlyUpper=TRUE: stationary.cov versus stationary.cov with matrix distMat")
test.for.zero( out2[upper.tri(out1)], out3[upper.tri(exp3)], tag="onlyUpper=TRUE: stationary.cov versus Exp.cov with matrix distMat")

##### test Exp.cov functions for correct use of p
#

p1 = 1
p2 = 2
p3 = 3
distMat = rdist(x1, x1)

exp1 = Exp.cov(x1, p=p1)
exp2 = Exp.cov(x1, p=p2)
exp2Dist = Exp.cov(x1, p=p2, distMat = distMat)
exp3 = Exp.cov(x1, p=p3)
test.for.zero(exp1^(rdist(x1, x1)^(p2 - p1)), exp2, tag="Testing p=1 v 2")
test.for.zero(exp2^(rdist(x1, x1)^(p3 - p2)), exp3, tag="Testing p=2 v 3")
test.for.zero(exp2, exp2Dist, tag="Testing p=2 v 2 with distMat")

##### test Exp.cov functions for correct use of aRange
#

aRange1 = 1
aRange2 = 2
aRange3 = 3
distMat = rdist(x1, x1)

exp1 = Exp.cov(x1, aRange=aRange1)
exp2 = Exp.cov(x1, aRange=aRange2)
exp2Dist = Exp.cov(x1, aRange=aRange2, distMat = distMat)
exp3 = Exp.cov(x1, aRange=aRange3)
test.for.zero(exp1^(aRange1/aRange2), exp2, tag="Testing aRange=1 v 2")
test.for.zero(exp2^(aRange2/aRange3), exp3, tag="Testing aRange=2 v 3")
test.for.zero(exp2, exp2Dist, tag="Testing aRange=2 v 2 with distMat")




cat("end tests of V argument in covariances", fill=TRUE)