Skip to content

Commit

Permalink
Include accuracy assessment
Browse files Browse the repository at this point in the history
  • Loading branch information
vwmaus committed Feb 2, 2017
1 parent ea7917b commit e8ef1fb
Show file tree
Hide file tree
Showing 8 changed files with 504 additions and 94 deletions.
3 changes: 3 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ LazyData: true
RoxygenNote: 5.0.1
Collate:
'class-crossValidation.R'
'class-twdtwAccuracy.R'
'class-twdtwTimeSeries.R'
'class-twdtwMatches.R'
'class-twdtwRaster.R'
Expand Down Expand Up @@ -86,6 +87,8 @@ Collate:
'subset.R'
'twdtw.R'
'twdtwApply.R'
'twdtwAssessment.R'
'twdtwClassify.R'
'twdtwCrossValidation.R'
'zzz.R'
VignetteBuilder: knitr
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ exportMethods(show)
exportMethods(subset)
exportMethods(summary)
exportMethods(twdtwApply)
exportMethods(twdtwAssessment)
exportMethods(twdtwClassify)
exportMethods(twdtwCrossValidation)
exportMethods(twdtwMatches)
Expand Down
84 changes: 0 additions & 84 deletions R/class-crossValidation.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,87 +80,3 @@ setMethod("initialize",
}
)

setGeneric("twdtwCrossValidation",
def = function(object, ...) standardGeneric("twdtwCrossValidation")
)

#' @inheritParams twdtwCrossValidation-class
#' @aliases twdtwCrossValidation-create
#'
#' @describeIn twdtwCrossValidation Splits the set of time
#' series into training and validation. The function uses stratified
#' sampling and a simple random sampling for each stratum. For each data partition
#' this function performs a TWDTW analysis and returns the Overall Accuracy,
#' User's Accuracy, Produce's Accuracy, error matrix (confusion matrix), and a
#' \code{\link[base]{data.frame}} with the classification (Predicted), the
#' reference classes (Reference), and some TWDTW information.
#'
#' @examples
#' \dontrun{
#' # Data folder
#' data_folder = system.file("lucc_MT/data", package = "dtwSat")
#'
#' # Read dates
#' dates = scan(paste(data_folder,"timeline", sep = "/"), what = "dates")
#'
#' # Read raster time series
#' evi = brick(paste(data_folder,"evi.tif", sep = "/"))
#' raster_timeseries = twdtwRaster(evi, timeline = dates)
#'
#' # Read field samples
#' field_samples = read.csv(paste(data_folder,"samples.csv", sep = "/"))
#' table(field_samples[["label"]])
#'
#' # Read field samples projection
#' proj_str = scan(paste(data_folder,"samples_projection", sep = "/"),
#' what = "character")
#'
#' # Get sample time series from raster time series
#' field_samples_ts = getTimeSeries(raster_timeseries,
#' y = field_samples, proj4string = proj_str)
#' field_samples_ts
#'
#' # Run cross validation
#' set.seed(1)
#' # Define TWDTW weight function
#' log_fun = logisticWeight(alpha=-0.1, beta=50)
#' cross_validation = twdtwCrossValidation(field_samples_ts, times=3, p=0.1,
#' freq = 8, formula = y ~ s(x, bs="cc"), weight.fun = log_fun)
#' cross_validation
#'
#' summary(cross_validation)
#'
#' }
#' @export
setMethod(f = "twdtwCrossValidation",
definition = function(object, times, p, ...) twdtwCrossValidation.twdtwTimeSeries(object, times, p, ...))

twdtwCrossValidation.twdtwTimeSeries = function(object, times, p, ...){

partitions = createDataPartition(y = labels(object), times, p, list = TRUE)

res = lapply(partitions, function(I){
training_ts = subset(object, I)
validation_ts = subset(object, -I)
patt = createPatterns(training_ts, ...)
twdtw_res = twdtwApply(x = validation_ts, y = patt, n=1, ...)
df = do.call("rbind", lapply(twdtw_res[], function(xx) xx[which.min(xx$distance),]) )
ref = labels(twdtw_res)$timeseries
pred = df$label
data = data.frame(.adjustFactores(ref, pred, levels=NULL, labels=NULL), df[,!names(df)%in%"labels"])
error.matrix = table(Predicted=data$Predicted, Reference=data$Reference)
UA = diag(error.matrix) / rowSums(error.matrix)
PA = diag(error.matrix) / colSums(error.matrix)
O = sum(diag(error.matrix)) / sum(rowSums(error.matrix))
list(OverallAccuracy=O, UsersAccuracy=UA, ProducersAccuracy=PA, ErrorMatrix=error.matrix, data=data)
})

new("twdtwCrossValidation", partitions=partitions, accuracy=res)

}






87 changes: 87 additions & 0 deletions R/class-twdtwAccuracy.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
###############################################################
# #
# (c) Victor Maus <[email protected]> #
# Institute for Geoinformatics (IFGI) #
# University of Muenster (WWU), Germany #
# #
# Earth System Science Center (CCST) #
# National Institute for Space Research (INPE), Brazil #
# #
# #
# R Package dtwSat - 2017-01-18 #
# #
###############################################################


#' @title class "twdtwAssessment"
#' @name twdtwAssessment-class
#' @aliases twdtwAssessment
#' @author Victor Maus, \email{vwmaus1@@gmail.com}
#'
#' @description This class stores the map assessment.
#'
#' @param object an object of class \code{\link[dtwSat]{twdtwRaster}} resulting from
#' the classification, i.e. \code{\link[dtwSat]{twdtwClassify}}.
#'
#' @param y a \code{\link[base]{data.frame}} whose attributes are: longitude,
#' latitude, the start ''from'' and the end ''to'' of the time interval
#' for each sample. This can also be a \code{\link[sp]{SpatialPointsDataFrame}}
#' whose attributes are the start ''from'' and the end ''to'' of the time interval.
#' If missing ''from'' and/or ''to'', they are set to the time range of the
#' \code{object}.
#'
#' @param id.labels a numeric or character with an column name from \code{y} to
#' be used as samples labels. Optional.
#'
#' @param proj4string projection string, see \code{\link[sp]{CRS-class}}. Used
#' if \code{y} is a \code{\link[base]{data.frame}}.
#'
#' @param conf.int specifies the confidence level.
#'
#' @seealso
#' \code{\link[dtwSat]{twdtwRaster-class}}, and
#' \code{\link[dtwSat]{twdtwClassify}}.
#'
#' @section Slots :
#' \describe{
#' \item{\code{accuracy}:}{A list with the accuracy for each classified time period.}
#' \item{\code{data}:}{A \code{\link[base]{data.frame}} with reference labels, predicted labels,
#' and other TWDTW information.}
#' }
#'
#' @examples
#' \dontrun{
#'
#' }
NULL
setClass(
Class = "twdtwAssessment",
slots = c(accuracy = "list", data = "list"),
validity = function(object){
if(!is(object@partitions, "list")){
stop("[twdtwTimeSeries: validation] Invalid partitions, class different from list.")
}else{}
if(!is(object@accuracy, "list")){
stop("[twdtwTimeSeries: validation] Invalid accuracy, class different from list.")
}else{}
return(TRUE)
}
)

setMethod("initialize",
signature = "twdtwAssessment",
definition =
function(.Object, partitions, accuracy){
.Object@partitions = list(Resample1=NULL)
.Object@accuracy = list(OverallAccuracy=NULL, UsersAccuracy=NULL, ProducersAccuracy=NULL,
error.matrix=table(NULL), data=data.frame(NULL))
if(!missing(partitions))
.Object@partitions = partitions
if(!missing(accuracy))
.Object@accuracy = accuracy
validObject(.Object)
return(.Object)
}
)


Loading

0 comments on commit e8ef1fb

Please sign in to comment.