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
|
R version 4.2.1 (2022-06-23) -- "Funny-Looking Kid"
Copyright (C) 2022 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin17.0 (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> #
> # 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]
> #source("~/Home/Src/fields/R/mKrig.family.R")
>
> # now look at mKrig w/o sparse matrix
> look<- mKrig( x,y, cov.function="stationary.cov", aRange=10, lambda=.3,
+ chol.args=list( pivot=FALSE))
>
>
> lookKrig<- Krig( x,y, cov.function="stationary.cov",
+ aRange=10)
>
> test.df<-Krig.ftrace(look$lambda,lookKrig$matrices$D)
>
> test<- Krig.coef( lookKrig, lambda=look$lambda)
>
> test.for.zero( look$d, test$d, tag="Krig mKrig d coef")
Testing: Krig mKrig d coef
PASSED test at tolerance 1e-08
> test.for.zero( look$c, test$c, tag="Krig mKrig c coef")
Testing: Krig mKrig c coef
PASSED test at tolerance 1e-08
>
> # test of trace calculation
>
> look<- mKrig( x,y, cov.function="stationary.cov", aRange=10, lambda=.3,
+
+ find.trA=TRUE, NtrA= 1000, iseed=243)
>
> test.for.zero( look$eff.df, test.df,tol=.01, tag="Monte Carlo eff.df")
Testing: Monte Carlo eff.df
PASSED test at tolerance 0.01
>
>
> #
> lookKrig<-Krig( x,y, cov.function="stationary.cov",
+ aRange=350, Distance="rdist.earth",Covariance="Wendland",
+ cov.args=list( k=2, dimension=2) )
>
> look<- mKrig( x,y, cov.function="stationary.cov",
+ aRange=350,
+ Distance="rdist.earth",Covariance="Wendland",
+ cov.args=list( k=2, dimension=2),
+ lambda=lookKrig$lambda,
+ find.trA=TRUE, NtrA= 1000, iseed=243)
>
> test.for.zero( look$c, lookKrig$c, tag="Test of wendland and great circle")
Testing: Test of wendland and great circle
PASSED test at tolerance 1e-08
>
> test.for.zero(look$eff.df, Krig.ftrace( lookKrig$lambda, lookKrig$matrices$D)
+ ,tol=.01, tag="eff.df")
Testing: eff.df
PASSED test at tolerance 0.01
>
> # same calculation using sparse matrices.
>
> look4<- mKrig( x,y, cov.function="wendland.cov",
+ aRange=350,
+ Dist.args=list( method="greatcircle"),
+ cov.args=list( k=2),
+ lambda=lookKrig$lambda,
+ find.trA=TRUE, NtrA=500, iseed=243)
>
> test.for.zero( look$c.coef, look4$c.coef,tol=8e-7,
+ tag="Test of sparse wendland and great circle")
Testing: Test of sparse wendland and great circle
PASSED test at tolerance 8e-07
> test.for.zero(look4$eff.df, Krig.ftrace( lookKrig$lambda, lookKrig$matrices$D),
+ tol=.01, tag="sparse eff.df")
Testing: sparse eff.df
PASSED test at tolerance 0.01
>
> # great circle distance switch has been a big bug -- test some options
>
> look<- mKrig( x,y, cov.function="wendland.cov",
+ aRange=350, Dist.args=list( method="greatcircle"),
+ cov.args=list( k=2),lambda=lookKrig$lambda,
+ find.trA=TRUE, NtrA=1000, iseed=243)
>
> test.for.zero(look$eff.df, Krig.ftrace( lookKrig$lambda, lookKrig$matrices$D),
+ tol=1e-2, tag="exact sparse eff.df")
Testing: exact sparse eff.df
PASSED test at tolerance 0.01
>
> # compare to fast Tps
> look3<- fastTps( x,y,aRange=350,lambda=lookKrig$lambda, NtrA=200, iseed=243,
+ lon.lat=TRUE)
> #look3$c<- lookKrig$c
> #look3$d<- lookKrig$d
> object<- look3
> np<- object$np
> Ey <- diag(1, np)
> NtrA <- np
> hold <- predict.mKrig(object, ynew = Ey, collapseFixedEffect=FALSE)
> hold2<- matrix( NA, np,np)
> for( k in 1:np){
+ hold2[,k] <- predict.Krig(lookKrig, y = Ey[,k])
+ }
> #plot( diag(hold), diag(hold2))
>
>
> test.for.zero( look3$c, lookKrig$c, tol=5e-7)
PASSED test at tolerance 5e-07
> test.for.zero( look3$d, lookKrig$d, tol=2e-8)
PASSED test at tolerance 2e-08
> test.for.zero( look3$fitted.values, lookKrig$fitted.values, tol=1e-7)
PASSED test at tolerance 1e-07
>
> test.for.zero( predict( look3, xnew= look3$x), predict( lookKrig, xnew= lookKrig$x),
+ tol=5e-7)
PASSED test at tolerance 5e-07
>
> test.for.zero( hold[,1], hold2[,1], tol=1e-7, relative=FALSE)
PASSED test at tolerance 1e-07
>
> test.for.zero(diag(hold),diag(hold2), tol=2E-7,
+ relative=FALSE, tag="exact sparse eff.df by predict -- fastTps")
Testing: exact sparse eff.df by predict -- fastTps
PASSED test at tolerance 2e-07
> #plot( diag(hold), ( 1- diag(hold2)/ diag(hold)) )
>
> test.for.zero(look3$eff.df,sum( diag(hold)) , tag="fastTps ef.df exact" )
Testing: fastTps ef.df exact
PASSED test at tolerance 1e-08
>
> test.for.zero(look3$eff.df, Krig.ftrace( lookKrig$lambda, lookKrig$matrices$D),
+ tol=2e-7, tag="exact sparse eff.df through mKrig-- fastTps")
Testing: exact sparse eff.df through mKrig-- fastTps
PASSED test at tolerance 2e-07
>
> # calculations of likelihood, sigma and tau
>
> lam<-.2
>
> out<- mKrig( x,y, cov.function =Exp.cov, aRange=4, lambda=lam)
> out2<- Krig( x,y, cov.function =Exp.cov, aRange=4, lambda=lam)
>
>
> Sigma<- Exp.cov( x,x,aRange=4)
> X<- cbind( rep(1, nrow(x)), x)
>
> Sinv<- solve( Sigma + lam* diag( 1, nrow( x)))
>
> #checks on likelihoods
>
> # quadratic form:
> betaHat<- c(solve( t(X)%*%Sinv%*%(X) ) %*% t(X) %*%Sinv%*%y)
> test.for.zero( betaHat, out$beta, tag="initial check on d for likelihood")
Testing: initial check on d for likelihood
PASSED test at tolerance 1e-08
> r<- y -X%*%betaHat
> N<- nrow(x)
> look<- t( r)%*%(Sinv)%*%r/N
>
>
>
> test.for.zero( look, out$summary["sigma2"], tag="sigma2 hat from likelihood")
Testing: sigma2 hat from likelihood
PASSED test at tolerance 1e-08
>
> test.for.zero( look, out2$sigma.MLE, tag="sigma2 hat from likelihood compared to Krig")
Testing: sigma2 hat from likelihood compared to Krig
PASSED test at tolerance 1e-08
>
>
>
> # check determinant
> lam<- .2
> Sigma<- Exp.cov( x,x,aRange=4)
> M<- Sigma + lam * diag( 1, nrow(x))
> chol( M)-> Mc
> look2<- sum( log(diag( Mc)))*2
>
> out<-mKrig( x,y,cov.function =Exp.cov, aRange=4, lambda=lam)
>
> test.for.zero( out$lnDetCov, look2)
PASSED test at tolerance 1e-08
> test.for.zero( out$lnDetCov, determinant(M, log=TRUE)$modulus)
PASSED test at tolerance 1e-08
>
> # weighted version
> lam<- .2
> Sigma<- Exp.cov( x,x,aRange=4)
> set.seed( 123)
> weights<- runif(nrow( x))
> M<- Sigma + diag(lam/ weights)
> chol( M)-> Mc
> look2<- sum( log(diag( Mc)))*2
>
> out<-mKrig( x,y,weights=weights, cov.function =Exp.cov, aRange=4, lambda=lam)
>
> test.for.zero( out$lnDetCov, look2)
PASSED test at tolerance 1e-08
> test.for.zero( look2, determinant(M, log=TRUE)$modulus)
PASSED test at tolerance 1e-08
> test.for.zero( out$lnDetCov, determinant(M, log=TRUE)$modulus)
PASSED test at tolerance 1e-08
>
>
>
> # check profile likelihood by estimating MLE
> lam.true<- .2
> N<- nrow( x)
> Sigma<- Exp.cov( x,x,aRange=4)
> M<- Sigma + lam.true * diag( 1, nrow(x))
> chol( M)-> Mc
> t(Mc)%*%Mc -> test
>
>
>
>
> ##D set.seed( 234)
> ##D NSIM<- 100
> ##D hold2<-rep( NA, NSIM)
> ##D temp.fun<- function(lglam){
> ##D out<-mKrig( x,ytemp,
> ##D cov.function =Exp.cov, aRange=4, lambda=exp(lglam))
> ##D return(-1* out$lnProfileLike)}
>
> ##D hold1<-rep( NA, NSIM)
> ##D yt<- rep( 1, N)
> ##D obj<- Krig( x,yt, aRange=4)
>
>
> ##D E<- matrix( rnorm( NSIM*N), ncol=NSIM)
>
> ##D for ( j in 1:NSIM){
> ##D cat( j, " ")
> ##D ytemp <- x%*%c(1,2) + t(Mc)%*%E[,j]
> ##D out<- optim( log(.2), temp.fun, method="BFGS")
> ##D hold2[j]<- exp(out$par)
> ##D hold1[j]<- gcv.Krig(obj, y=ytemp)$lambda.est[6,1]
>
> ##D }
> ##D test.for.zero( median( hold1), .2, tol=.08)
> ##D test.for.zero( median( hold2), .2, tol=.12)
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> proc.time()
user system elapsed
1.770 0.161 1.888
|