Learn R Programming

SpatioTemporal (version 0.9.2)

createCV: Define Cross-Validation Groups

Description

Creates a matrix that specifies cross-validation schemes.

Usage

createCV(mesa.data.model, groups = 10, min.dist = 0.1,
         random = FALSE, subset=NA, option="all")

Arguments

mesa.data.model
Data structure holding observations, and information regarding the observation locations. See create.data.model and mesa.data.model
groups
Number of cross-validation groups, zero gives leave-one-out cross-validation.
min.dist
Minimum distance between locations for them to end up in separate groups. Points closer than min.dist will be forced into the same group. A high value for min.dist can result in fewer cross-validation groups than spec
random
If FALSE repeated calls to the function will return the same grouping, if TRUE repeated calls will give different CV-groupings. Ensures that simulation studies are reproducable.
subset
A subset of locations for which to define the cross-validation setup. Only sites listed in subset are dropped from one of the cross-validation groups; in other words sites not in subset are used for estimatio
option
For internal MESA Air usage, see Details below.

Value

  • Return a (number or observations) - by - (groups) logical matrix. Each column defines a cross-validation set with the TRUE values marking the observations to be left out.

encoding

latin1

Details

The number of observations left out of each group is can be rather uneven; the main goal of createCV is to create CV-groups such that the groups contain roughly the same number of locations ignoring the number of observations at each location. If there are large differences in the number of observations at differnt locations one could use the subset option to create different CV-groupings for different types of locations. The groups can then be combined as I.final = I.1 | I.2 | I.3.

If random=FALSE the function initially sets set.seed(0, kind = "Mersenne-Twister"), and resets the random-seed using .Random.seed and set.seed before terminating.

The option input determines which sites to include in the cross-validation. Possible options are "all", "fixed", "comco", "snapshot" and "home". [object Object],[object Object],[object Object],[object Object]

See Also

See also estimateCV, and predictCV. For computing CV statistics, see also compute.ltaCV, predictNaive, and for further illustration see plotCV, CVresiduals.qqnorm, and summaryStatsCV.

Examples

Run this code
##load the data
data(mesa.data.model)

##create a matrix with the CV-schemes
I.cv <- createCV(mesa.data.model, groups=10)

##number of observations in each CV-group
colSums(I.cv)

##Which sites belong to which groups?
ID.cv <- lapply(apply(I.cv,2,list),function(x) 
                unique(mesa.data.model$obs$ID[x[[1]]]))
print(ID.cv)

##Note that the sites with distance 0.084<min.dist 
##are grouped together (in group 10).
mesa.data.model$dist[ID.cv[[10]],ID.cv[[10]]]

##Find out which location belongs to which cv group
I.col <- apply(sapply(ID.cv,function(x) mesa.data.model$location$ID
               %in% x), 1, function(x) if(sum(x)==1) which(x) else 0)
names(I.col) <- mesa.data.model$location$ID
print(I.col)

##Plot the locations, colour coded by CV-grouping
plot(mesa.data.model$location$long, mesa.data.model$location$lat,
     pch=23+floor(I.col/max(I.col)+.5), bg=I.col, 
     xlab="Longitude",ylab="Latitude")

Run the code above in your browser using DataLab