Commits

Paul Hiemstra committed 248cc6c

Updating crossvalidation scripts:
- Changed the default models checked, "Mat" to "Ste"
for consistency with autofitVariogram
- Added significant numbers option to summary.autoKrige.cv
Updated docs and tests accordingly

  • Participants
  • Parent commits 5e60c6d

Comments (0)

Files changed (4)

File R/autoKrige.cv.r

 autoKrige.cv = function(formula, input_data, data_variogram = input_data,
-                          model = c("Sph", "Exp", "Gau", "Mat"), kappa = c(0.05, seq(0.2, 2, 0.1), 5, 10), 
+                          model = c("Sph", "Exp", "Gau", "Ste"), kappa = c(0.05, seq(0.2, 2, 0.1), 5, 10), 
 						  fix.values = c(NA,NA,NA), verbose = FALSE, GLS.model = NA,
                           start_vals = c(NA,NA,NA), miscFitOptions = list(),...)
 # Automatically fits a variogram model to the data using autofitVariogram and performs
 	return(kr.cv)
 }
 
-summary.autoKrige.cv = function(object, ...) {
+summary.autoKrige.cv = function(object, ..., digits = 4) {
 # The summary function for the autoKrige.cv object. Returns some
 # statistics about the cross-validation
 	obj = object$krige.cv_output
 	# Inter quartile range, ideally small
 	out$iqr = IQR(obj$residual)
 
-	return(t(data.frame(out)))
+    out = lapply(out, signif, digits = digits)
+    out = t(t(out))
+	return(out)
 }
 
 compare.cv = function(..., col.names, bubbleplots = FALSE, zcol = "residual", 
-						   layout, key.entries, reference = 1, plot.diff = FALSE) 
+						   layout, key.entries, reference = 1, plot.diff = FALSE,
+                           digits = 4) 
 # A function to compare cross-validations to each other in both statistics (using summary.autoKrige.cv) or
 # in bubble plots (using cv.compare.bubble). '...' can be both output from krige.cv or autoKrige.cv.
 {
 	dots = list(...)
+    
+    # If dots consists of only one entry, which is a list, that list is assigned to dots
+    if(length(dots) == 1 & inherits(dots, "list")) dots = dots[[1]]
 
     # If a user passes output directly from krige.cv, change it to an autoKrige.cv object
-    dots = lapply(dots, function(x) { 
-            if(inherits(x, "autoKrige.cv")) {                   # autoKrige.cv output
-                return(x)
-            } else if(inherits(x, "SpatialPointsDataFrame")){   # krige.cv output
-                x = list(krige.cv_output = x)
-                class(x) = "autoKrige.cv"
-                return(x)
-            } else {                                            # unknown output
-                stop(sprintf("One of the objects in \'...\' has class \'%s\'. Class of objects in \'...\' should be one of \n  \'autoKrige.cv\' or \'SpatialPointsDataFrame\'", class(x)[1]))
-            }
-        })
+    dots = checkIfautokrige.cv(dots)
 
-	out = do.call("cbind", lapply(dots, summary))
+	out = do.call("cbind", lapply(dots, summary, digits = digits))
 	if(missing(col.names)) {
         col.names = as.character(as.list(match.call(expand.dots=TRUE))[-1])[1:length(dots)]
 	}
 	return(data.frame(out))
 }
 
+# Checks wheter the list contains autoKrige.cv objects,
+# if they are spatial, convert to autoKrige.cv object.
+checkIfautokrige.cv = function(l) {
+  l = lapply(l, function(x) { 
+          if(inherits(x, "autoKrige.cv")) {                   # autoKrige.cv output
+              return(x)
+          } else if(inherits(x, "SpatialPointsDataFrame")){   # krige.cv output
+              x = list(krige.cv_output = x)
+              class(x) = "autoKrige.cv"
+              return(x)
+          } else {                                            # unknown output
+              stop(sprintf("One of the objects in \'...\' has class \'%s\'. Class of objects in \'...\' should be one of \n  \'autoKrige.cv\' or \'SpatialPointsDataFrame\'", class(x)[1]))
+          }
+      })
+  return(l)
+}
+
+
 cv.compare.bubble = function(objs, zcol, key.entries, layout, col.names, plot.diff, reference,...) 
 # A function to create bubble plots of multiple cross validation in order to compare them.
 {

File man/autoKrige_cv.rd

 autoKrige.cv(formula, 
 	     input_data, 
 	     data_variogram = input_data,
-	     model = c("Sph", "Exp", "Gau", "Mat"), 
+	     model = c("Sph", "Exp", "Gau", "Ste"), 
 	     kappa = c(0.05, seq(0.2, 2, 0.1), 5, 10), 
 	     fix.values = c(NA,NA,NA), 
 	     verbose = FALSE, 

File man/compare.cv.rd

 	   layout, 
 	   key.entries, 
 	   reference = 1, 
-	   plot.diff = FALSE) 
+	   plot.diff = FALSE,
+       digits = 4) 
 }
 \arguments{
 	\item{...}{\code{\link{autoKrige.cv}} objects that are compared to each other. Also accepts the output form krige.cv, these objects are transformed to \code{\link{autoKrige.cv}} objects.}
 	\item{key.entries}{A list of numbers telling what the key entries in the bubbleplots are. See \code{\link[sp]{bubble}} for more details.}
 	\item{reference}{An integer telling which of the objects should be taken as a reference if \code{plot.diff} equals TRUE. \code{reference}
 			equal to 1 means that the first object is the reference, \code{reference} equal to 2 means that the second object is the reference etc.}
-	\item{plot.diff}{logical, if \code{plot.diff} is TRUE the number specified in \code{reference} defines the CV object that is taken as a reference 
-			What is shown in the plot is reference data squared minus the other data squared. So the color red means that the CV is doing
-			worse than the reference, vice-versa for green. This is very useful to see where the differences between the results are spatially and if 
-			there is a pattern.}
+    \item{plot.diff}{logical, if \code{plot.diff} is TRUE the number specified in \code{reference} defines the CV object that is taken as a reference 
+            What is shown in the plot is reference data squared minus the other data squared. So the color red means that the CV is doing
+            worse than the reference, vice-versa for green. This is very useful to see where the differences between the results are spatially and if 
+            there is a pattern.}    
+     \item{digits}{The number of significant digits in the resulting data.frame.}
 }
 \value{A data.frame with for each cross-validation result a number of diagnostics:
 % \describe{

File tests/autoKrige.cv.Rout.save

 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |                                                                      |   1%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |=====================                                                 |  31%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |==============================                                        |  44%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |===================================                                   |  51%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=======================================                               |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |========================================                              |  58%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |=============================================                         |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |===============================================                       |  68%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=====================================================                 |  75%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |=======================================================               |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |========================================================              |  81%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |=============================================================         |  88%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |=================================================================     |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |=================================================================     |  94%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================|  99%
  |                                                                            
  |======================================================================| 100%
 > 
 > summary(kr.cv)
-                   [,1]
-mean_error  0.002008094
-MAE         0.291793494
-MSE         0.154896959
-MSNE        0.855526096
-cor_obspred 0.837448324
-cor_predres 0.040265220
-RMSE        0.393569510
-URMSE       0.393564387
-iqr         0.427507414
+            [,1]    
+mean_error  0.002008
+MAE         0.2918  
+MSE         0.1549  
+MSNE        0.8555  
+cor_obspred 0.8374  
+cor_predres 0.04027 
+RMSE        0.3936  
+URMSE       0.3936  
+iqr         0.4275  
 > summary(kr_dist.cv)
-                    [,1]
-mean_error  -0.003064504
-MAE          0.270693666
-MSE          0.142958068
-MSNE         1.070710153
-cor_obspred  0.850928062
-cor_predres -0.025131755
-RMSE         0.378097961
-URMSE        0.378085541
-iqr          0.403438548
+            [,1]     
+mean_error  -0.003065
+MAE         0.2707   
+MSE         0.143    
+MSNE        1.071    
+cor_obspred 0.8509   
+cor_predres -0.02513 
+RMSE        0.3781   
+URMSE       0.3781   
+iqr         0.4034   
 > summary(kr_dist_ffreq.cv)
-                     [,1]
-mean_error  -0.0008806295
-MAE          0.2345309406
-MSE          0.1032282022
-MSNE         1.0832114308
-cor_obspred  0.8948613597
-cor_predres -0.0277677803
-RMSE         0.3212914598
-URMSE        0.3212902530
-iqr          0.3628917263
+            [,1]      
+mean_error  -0.0008806
+MAE         0.2345    
+MSE         0.1032    
+MSNE        1.083     
+cor_obspred 0.8949    
+cor_predres -0.02777  
+RMSE        0.3213    
+URMSE       0.3213    
+iqr         0.3629    
 > 
 > compare.cv(kr.cv, kr_dist.cv, kr_dist_ffreq.cv)
-                  kr.cv   kr_dist.cv kr_dist_ffreq.cv
-mean_error  0.002008094 -0.003064504    -0.0008806295
-MAE         0.291793494  0.270693666     0.2345309406
-MSE         0.154896959  0.142958068     0.1032282022
-MSNE        0.855526096  1.070710153     1.0832114308
-cor_obspred 0.837448324  0.850928062     0.8948613597
-cor_predres 0.040265220 -0.025131755    -0.0277677803
-RMSE        0.393569510  0.378097961     0.3212914598
-URMSE       0.393564387  0.378085541     0.3212902530
-iqr         0.427507414  0.403438548     0.3628917263
+               kr.cv kr_dist.cv kr_dist_ffreq.cv
+mean_error  0.002008  -0.003065       -0.0008806
+MAE           0.2918     0.2707           0.2345
+MSE           0.1549      0.143           0.1032
+MSNE          0.8555      1.071            1.083
+cor_obspred   0.8374     0.8509           0.8949
+cor_predres  0.04027   -0.02513         -0.02777
+RMSE          0.3936     0.3781           0.3213
+URMSE         0.3936     0.3781           0.3213
+iqr           0.4275     0.4034           0.3629
 >