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
|
#
# 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)
#options( echo=TRUE)
#
##### generate test data
#
data( ozone2)
# x is a two column matrix where each row is a location in lon/lat
# coordinates
x<- ozone2$lon.lat
# y is a vector of ozone measurements at day 16 a the locations.
y<- ozone2$y[16,]
#ind<- !is.na( y)
#x<- x[ind,]
#y<- y[ind]
x<-x[1:31,]
y<-y[1:31]
y[31]<-NA
################ test that optim results match the model evaluated
################ at the optimized parameters.
optim.args = list(method = "BFGS",
control = list(fnscale = -1, parscale = c(0.5, 0.5),
ndeps = c(0.05,0.05)))
MLEfit0 <- mKrigMLEJoint(x, y,
cov.params.start= list(lambda=.5, aRange=1.2),
cov.fun="stationary.cov",
optim.args=optim.args,
cov.args = list(Covariance = "Matern", smoothness=1.0),
na.rm=TRUE,
mKrig.args = list( m=1),
verbose=FALSE)
# check agreement with a fast return note cov.params.start and lambda.fixed
# are the switches to indicate this case
MLEfit0C <- mKrigMLEJoint(x, y,
cov.params.start = NULL,
cov.function = "stationary.cov",
cov.args = list(Covariance = "Matern",
lambda = MLEfit0$pars.MLE["lambda"],
smoothness = 1.0,
aRange = MLEfit0$pars.MLE["aRange"]),
na.rm = TRUE,
mKrig.args = list( m=1),
verbose = FALSE
)
test.for.zero( MLEfit0$summary["lnProfileLike.FULL"], MLEfit0C$summary["lnProfileLike.FULL"],
tag="Likelihood Values optim and the fast return")
test.for.zero( MLEfit0$summary["lnProfileLike.FULL"], MLEfit0$optimResults$value,
tag="Likelihood Values summary and optim")
obj0<- mKrig( x,y, cov.args = list(Covariance = "Matern",
smoothness = 1.0),
na.rm=TRUE, m=1,
lambda= MLEfit0$pars.MLE["lambda"],
aRange=MLEfit0$pars.MLE["aRange"])
test.for.zero( MLEfit0$summary["lnProfileLike.FULL"],
obj0$summary["lnProfileLike.FULL"],
tag="Likelihood Values summary and direct mKrig call")
test.for.zero( MLEfit0$summary["sigma2"],obj0$summary["sigma2"],
tag="... and sigma^2.MLE")
# test that grid seraching is correct
aRange.MLE<- MLEfit0$summary["aRange"]
par.grid<- list( aRange= c(.5, 1.0, 1.5)*aRange.MLE )
MLEfit1<- mKrigMLEGrid(x, y,
cov.fun = "stationary.cov",
cov.args = list(Covariance = "Matern",
smoothness = 1.0
),
par.grid = par.grid,
mKrig.args = list( m=1),
na.rm = TRUE,
verbose = FALSE,
cov.params.start = list( lambda = .2)
)
hold<- (MLEfit1$summary[1,"lnProfileLike.FULL"] < MLEfit1$summary[2,"lnProfileLike.FULL"]) &
(MLEfit1$summary[3,"lnProfileLike.FULL"] < MLEfit1$summary[2,"lnProfileLike.FULL"])
test.for.zero(as.numeric(hold), 1, relative=FALSE,
tag="consistency of Likelihood values")
##########################
### now evaluate on the "grid" of lambdas found by profiling
lambda.MLEs<- MLEfit1$summary[,"lambda"]
par.grid<- list( lambda = lambda.MLEs,
aRange = c(.5, 1.0, 1.5)*aRange.MLE )
MLEfit1B<- mKrigMLEGrid(x, y,
cov.function = "stationary.cov",
cov.args = list(Covariance = "Matern", smoothness = 1.0),
par.grid = par.grid,
mKrig.args = list( m=1),
na.rm = TRUE,
verbose = FALSE)
tempCol<- c( "lnProfileLike.FULL",
"lambda", "tau","sigma2")
test.for.zero( as.matrix(MLEfit1$summary[,tempCol]),
as.matrix(MLEfit1B$summary[,tempCol]),
tag="grid search with and w/o profile")
par.grid<- list( lambda = c(.999, 1.0, 1.001)*MLEfit0$summary["lambda"],
aRange = rep(MLEfit0$summary["aRange"] ,3 ) )
MLEfit2 <- mKrigMLEGrid(x, y,
cov.function = "stationary.cov",
cov.args = list(Covariance = "Matern",
smoothness = 1.0),
mKrig.args = list( m=1),
par.grid = par.grid,
verbose = FALSE)
hold<- (MLEfit2$summary[1,"lnProfileLike.FULL"] < MLEfit2$summary[2,"lnProfileLike.FULL"]) &
(MLEfit2$summary[3,"lnProfileLike.FULL"] < MLEfit2$summary[2,"lnProfileLike.FULL"])
test.for.zero(as.numeric(hold), 1, relative=FALSE, tag="crude test of maxmimum")
#MLEfit3<- MLESpatialProcess( x,y,
# cov.args = list(Covariance = "Matern",
# smoothness = 1.0),
# mKrig.args = list( m=1),
# cov.params.start = list( lambda =.2, aRange = NA)
# )
MLEfit3<- spatialProcess( x,y,
cov.args = list(Covariance = "Matern",
smoothness = 1.0),
mKrig.args = list( m=1),
cov.params.start = list( lambda =.2)
)
test.for.zero(MLEfit0$summary[1:2]/
(MLEfit3$summary[1:2]), 1, tol=1e-5,
tag="Testing MLE from spatialProcess ")
######### making sure spatialProcess uses parameter information correctly
obj<- spatialProcess( x, y, mKrig.args= list(m = 1),
lambda= MLEfit0$summary["lambda"],
aRange = MLEfit0$summary["aRange"]
)
obj1<- spatialProcess( x, y, mKrig.args= list(m = 1),
)
test.for.zero(MLEfit0$summary[1],
obj$summary["lnProfileLike.FULL"],
tag="spatialProcess finding MLE " )
test.for.zero(MLEfit0$summary[1],
obj1$summary["lnProfileLike.FULL"], tol=1e-5,
tag="spatialProcess given MLE "
)
# testing Krig function
#out1<- Krig( x,y, cov.fun="stationary.cov",
# cov.args = list(Covariance = "Matern",
# smoothness=1.0, aRange=.9),
# na.rm=TRUE,
# m=2)
#generate observation locations
set.seed( 22)
n=100
x = matrix(runif(2*n), nrow=n)
#generate observations at the locations
trueARange = .1
trueLambda = .1
distanceMatrix<- rdist(x,x)
Sigma<- Matern( distanceMatrix/trueARange, smoothness=1.0 )
U = chol(Sigma)
M<- 2e3 # lots of replicated fields.
set.seed( 332)
y = t(U)%*%matrix( rnorm(n*M), n,M) +
sqrt(trueLambda)*matrix( rnorm(n*M), n,M)
out<- mKrig( x,y, lambda=trueLambda, aRange=trueARange,
cov.function ="stationary.cov",cov.args = list(Covariance = "Matern",
smoothness=1.0)
)
optim.args = list(method = "BFGS",
control = list(fnscale = -1,
ndeps = c(0.09,0.09)))
MLEfitA <- mKrigMLEJoint(x, y,
cov.params.start= list(aRange=.2, lambda=.01),
cov.function="stationary.cov",
optim.args=optim.args,
cov.args = list(Covariance = "Matern",
smoothness=1.0),
na.rm=TRUE,
reltol = 1e-6,
mKrig.args = list( m=0),
verbose=FALSE)
cat("Testing mKrigMLEJoint against true values",
fill=TRUE)
test.for.zero( MLEfitA$summary["lambda"],.1, tol=.006)
test.for.zero( MLEfitA$summary["aRange"],.1, tol=.02)
test.for.zero( MLEfitA$summary["sigma2"], 1.0, tol=.02)
### now test REML fitting
MLEfitB <- mKrigMLEJoint(x, y,
cov.params.start= list(aRange=.12, lambda=.5),
cov.function="stationary.cov",
optim.args=optim.args,
cov.args = list(Covariance = "Matern",
smoothness=1.0),
na.rm=TRUE,
mKrig.args = list( m=0),
REML=TRUE,
verbose=FALSE)
cat("Testing mKrigMLEJoint with REML against true values",
fill=TRUE)
test.for.zero( MLEfitB$summary["lambda"],.1, tol=.007)
test.for.zero( MLEfitB$summary["aRange"],.1, tol=.01)
test.for.zero( MLEfitB$summary["sigma2"], 1.0, tol=.01)
cat("Testing mKrigMLEJoint with REML FALSE against true values",
fill=TRUE)
MLEfitC <- mKrigMLEJoint(x, y,
cov.params.start= list(aRange=.12, lambda=.5),
cov.function ="stationary.cov",
optim.args=optim.args,
cov.args = list(Covariance = "Matern",
smoothness=1.0),
na.rm=TRUE,
mKrig.args = list( m=2),
REML=FALSE,
verbose=FALSE
)
test.for.zero( MLEfitC$summary["lambda"], .1, tol=.02)
test.for.zero( MLEfitC$summary[ "aRange"], .1, tol=.02)
test.for.zero( MLEfitC$summary["sigma2"], 1.0, tol=.01)
cat("all done with mKrigMLEGrid tests", fill=TRUE)
options( echo=TRUE)
|