Skip to content

Commit

Permalink
Include twdtwXtable to create latex tables from accuracy metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
vwmaus committed Feb 8, 2017
1 parent 59ece52 commit 665d250
Show file tree
Hide file tree
Showing 16 changed files with 659 additions and 288 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ Collate:
'twdtwApply.R'
'twdtwAssess.R'
'twdtwClassify.R'
'twdtwCrossValidation.R'
'xtable.R'
'twdtwCrossValidate.R'
'twdtwXtable.R'
'zzz.R'
VignetteBuilder: knitr
4 changes: 2 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ exportMethods(summary)
exportMethods(twdtwApply)
exportMethods(twdtwAssess)
exportMethods(twdtwClassify)
exportMethods(twdtwCrossValidation)
exportMethods(twdtwCrossValidate)
exportMethods(twdtwMatches)
exportMethods(twdtwRaster)
exportMethods(twdtwTimeSeries)
exportMethods(xtable)
exportMethods(twdtwXtable)
import(ggplot2)
import(methods)
import(raster)
Expand Down
46 changes: 37 additions & 9 deletions R/class-crossValidation.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,12 @@
#'
#' @description This class stores the results of the cross-validation.
#'
#' @param object an object of class \code{\link[dtwSat]{twdtwTimeSeries}}.
#'
#' @param times Number of partitions to create.
#'
#' @param p the percentage of data that goes to training.
#' See \code{\link[caret]{createDataPartition}} for details.
#' @param object an object of class twdtwCrossValidation.
#'
#' @param conf.int specifies the confidence level (0-1) for interval estimation of the
#' population mean. For more details see \code{\link[ggplot2]{mean_cl_boot}}.
#'
#' @param ... Other arguments to be passed to \code{\link[dtwSat]{createPatterns}} and
#' to \code{\link[dtwSat]{twdtwApply}}.
#' @param ... Other arguments. Not used.
#'
#' @seealso
#' \code{\link[dtwSat]{twdtwMatches-class}},
Expand All @@ -45,8 +39,42 @@
#' data partitions.}
#' }
#'
#' @examples
#' @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 = twdtwCrossValidate(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)
#'
#' plot(cross_validation)
#'
#' }
NULL
Expand Down
35 changes: 5 additions & 30 deletions R/class-twdtwAssessment.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,13 @@
#' @aliases twdtwAssessment
#' @author Victor Maus, \email{vwmaus1@@gmail.com}
#'
#' @description This class stores the map assessment.
#' @description This class stores the map assessment metrics.
#'
#' @param object an object of class \code{\link[dtwSat]{twdtwRaster}} resulting from
#' the classification, i.e. \code{\link[dtwSat]{twdtwClassify}}.
#' @param object an object of class twdtwAssessment.
#'
#' @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 labels character vector with time series labels. For signature
#' \code{\link[dtwSat]{twdtwRaster}} this argument can be used to set the
#' labels for each sample in \code{y}, or it can be combined with \code{id.labels}
#' to select samples with a specific label.
#'
#' @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 (0-1).
#'
#' @seealso
#' \code{\link[dtwSat]{twdtwRaster-class}}, and
#' \code{\link[dtwSat]{twdtwClassify}}.
#' @seealso \code{\link[dtwSat]{twdtwClassify}},
#' \code{\link[dtwSat]{twdtwAssess}}, and
#' \code{\link[dtwSat]{twdtwXtable}}.
#'
#' @section Slots :
#' \describe{
Expand All @@ -63,10 +42,6 @@
#' surface area in km2 of each cell (pixel). If the twdtwRaster is projected the estimated area is calculated
#' using the the pixel resolution in the map unit.
#'
#' @examples
#' \dontrun{
#'
#' }
NULL
setClass(
Class = "twdtwAssessment",
Expand Down
21 changes: 15 additions & 6 deletions R/methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -449,13 +449,22 @@ show.twdtwAssessment = function(object){
cat("Number of classification intervals:",length(object@accuracyByPeriod),"\n")
cat("Accuracy metrics summary\n")
cat("\nOverall\n")
print(object@accuracySummary$OverallAccuracy, digits=2)
cat("\nUsers\n")
print(object@accuracySummary$UsersAccuracy, digits=2)
cat("\nProducers\n")
print(object@accuracySummary$ProducersAccuracy, digits=2)
aux = object@accuracySummary$OverallAccuracy
names(aux) = gsub("ci", "ci*", names(aux))
print(aux, digits=2)
cat("\nUser's\n")
aux = object@accuracySummary$UsersAccuracy
colnames(aux) = gsub("ci", "ci*", colnames(aux))
print(aux, digits=2)
cat("\nProducer's\n")
aux = object@accuracySummary$ProducersAccuracy
colnames(aux) = gsub("ci", "ci*", colnames(aux))
print(aux, digits=2)
cat("\nArea and uncertainty\n")
print(object@accuracySummary$AreaUncertainty, digits=2)
aux = object@accuracySummary$AreaUncertainty
colnames(aux) = gsub("ci", "ci*", colnames(aux))
print(aux, digits=2)
cat("\n*",100*object@accuracySummary$conf.int,"% confidence interval\n")
invisible(NULL)
}

Expand Down
4 changes: 2 additions & 2 deletions R/twdtwApply.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@
#' @references
#' [1] Maus V, Camara G, Cartaxo R, Sanchez A, Ramos FM, de Queiroz, GR.
#' (2016). A Time-Weighted Dynamic Time Warping method for land use and land cover
#' mapping. Selected Topics in Applied Earth Observations and Remote Sensing,
#' IEEE Journal of, vol.PP, no.99, pp.1-11.
#' mapping. IEEE Journal of Selected Topics in Applied Earth Observations and Remote
#' Sensing, vol.9, no.8, pp.3729-3739.
#' @references
#' [2] Giorgino, T. (2009). Computing and Visualizing Dynamic Time Warping Alignments in R:
#' The dtw Package. Journal of Statistical Software, 31, 1-24.
Expand Down
93 changes: 77 additions & 16 deletions R/twdtwAssess.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,54 @@ setGeneric("twdtwAssess",
def = function(object, ...) standardGeneric("twdtwAssess")
)

#' @inheritParams twdtwAssessment-class
#' @aliases twdtwAssess
#'
#' @describeIn twdtwAssessment This function performs an accuracy assessment
#' @title Assess TWDTW classification
#' @name twdtwAssess
#' @author Victor Maus, \email{vwmaus1@@gmail.com}
#'
#' @description Performs an accuracy assessment
#' of the classified maps. The function returns Overall Accuracy,
#' User's Accuracy, Produce's Accuracy, error matrix (confusion matrix),
#' and estimated area according to [1]. The function returns the metrics
#' and estimated area according to [1-2]. The function returns the metrics
#' for each time interval and a summary considering all classified intervals.
#'
#' @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 labels character vector with time series labels. For signature
#' \code{\link[dtwSat]{twdtwRaster}} this argument can be used to set the
#' labels for each sample in \code{y}, or it can be combined with \code{id.labels}
#' to select samples with a specific label.
#'
#' @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 (0-1).
#'
#' @references
#' [1] Olofsson, P., Foody, G.M., Stehman, S.V., Woodcock, C.E. (2013).
#' Making better use of accuracy data in land change studies: Estimating
#' accuracy and area and quantifying uncertainty using stratified estimation.
#' Remote Sensing of Environment, 129, pp.122-131.
#'
#' @references
#' [2] Olofsson, P., Foody G.M., Herold M., Stehman, S.V., Woodcock, C.E., Wulder, M.A. (2014)
#' Good practices for estimating area and assessing accuracy of land change. Remote Sensing of
#' Environment, 148, pp. 42-57.
#'
#' @seealso \code{\link[dtwSat]{twdtwClassify}},
#' \code{\link[dtwSat]{twdtwAssessment}}, and
#' \code{\link[dtwSat]{twdtwXtable}}.
#'
#' @examples
#' \dontrun{
#'
Expand Down Expand Up @@ -52,15 +91,21 @@ setGeneric("twdtwAssess",
#' plot(r_lucc)
#'
#' # Assess classification
#' twdtw_assess = twdtwAssess(r_lucc, validation_samples, proj4string=proj_str)
#' twdtw_assess = twdtwAssess(object = r_lucc, y = validation_samples,
#' proj4string = proj_str, conf.int=.95)
#' twdtw_assess
#'
#'
#' xtable(twdtw_assess, type="matrix")
#' xtable(twdtw_assess, type="accuracy")
#' xtable(twdtw_assess, type="area")
#' # Create latex tables
#' xtable(twdtw_assess, table.type="matrix")
#' xtable(twdtw_assess, table.type="accuracy")
#' xtable(twdtw_assess, table.type="area")
#'
#' }
NULL

#' @aliases twdtwAssess-twdtwRaster
#' @inheritParams twdtwAssess
#' @rdname twdtwAssess
#' @export
setMethod(f = "twdtwAssess", signature = "twdtwRaster",
definition = function(object, y, labels=NULL, id.labels=NULL, proj4string=NULL, conf.int=.95)
Expand Down Expand Up @@ -111,6 +156,7 @@ twdtwAssess.twdtwRaster = function(object, y, labels, id.labels, proj4string, co

# Compute accuracy assessment
accuracy_by_period = lapply(seq_along(error_matrix_by_period), function(i) .twdtwAssess(x = error_matrix_by_period[[i]], a_by_interval[[i]], conf.int=conf.int))
names(accuracy_by_period) = index(object)
accuracy_summary = .twdtwAssess(error_matrix_summary, area_by_class, conf.int=conf.int)

new("twdtwAssessment", accuracySummary=accuracy_summary, accuracyByPeriod=accuracy_by_period, data=samples_all)
Expand Down Expand Up @@ -214,13 +260,28 @@ twdtwAssess.twdtwRaster = function(object, y, labels, id.labels, proj4string, co
}

.getPredRefClasses = function(i, r_intervals, pred, pred_distance, y, rlevels, rnames){
I = which((r_intervals$to[i] - as.Date(y$from) > 30) & (as.Date(y$to) - r_intervals$from[i] > 30) )
if(length(I)<1)
i_leng = as.numeric(r_intervals$to[i] - r_intervals$from[i])
from = as.Date(y$from)
to = as.Date(y$to)
# Select overlapping alignments
J = which(from <= r_intervals$to[i] & to >= r_intervals$from[i])
# Adjust overlapping
from = sapply(from[J], function(x) ifelse(x < r_intervals$from[i], r_intervals$from[i], x))
to = sapply(to[J], function(x) ifelse(x > r_intervals$to[i], r_intervals$to[i], x))
# Compute overlapping proportion
i_over = to - from
print(i_leng)
print(i_over)
prop_over = abs(i_over / i_leng)
# Select alignments
I = which(prop_over > .5)
# I = which((r_intervals$to[i] - as.Date(y$from) > 30) & (as.Date(y$to) - r_intervals$from[i] > 30) )
if(length(J[I])<1)
return(NULL)
J = match(pred[I,i], rlevels)
Predicted = factor(as.character(rnames[J]), levels = rnames, labels = rnames)
Reference = factor(as.character(y$label[I]), levels = rnames, labels = rnames)
#d = pred_distance[J]
K = match(pred[J[I],i], rlevels)
Predicted = factor(as.character(rnames[K]), levels = rnames, labels = rnames)
Reference = factor(as.character(y$label[J[I]]), levels = rnames, labels = rnames)
#d = pred_distance[K]
data.frame(Period=i, from=r_intervals$from[i], to=r_intervals$to[i], Predicted, Reference)
}

Expand Down
45 changes: 30 additions & 15 deletions R/twdtwCrossValidation.R → R/twdtwCrossValidate.R
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@

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

#' @inheritParams twdtwCrossValidation-class
#' @aliases twdtwCrossValidation
#' @title Cross Validate temporal patterns
#' @name twdtwCrossValidate
#' @author Victor Maus, \email{vwmaus1@@gmail.com}
#'
#' @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),
#' @description Splits the set of time series into training and validation and
#' compute accuracy metrics. 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 the results of the TWDTW analysis.
#'
#' @param object an object of class \code{\link[dtwSat]{twdtwTimeSeries}}.
#'
#' @param times Number of partitions to create.
#'
#' @param p the percentage of data that goes to training.
#' See \code{\link[caret]{createDataPartition}} for details.
#'
#' @param ... Other arguments to be passed to \code{\link[dtwSat]{createPatterns}} and
#' to \code{\link[dtwSat]{twdtwApply}}.
#'
#' @examples
#' \dontrun{
#' # Data folder
Expand Down Expand Up @@ -44,7 +54,7 @@ setGeneric("twdtwCrossValidation",
#' 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,
#' cross_validation = twdtwCrossValidate(field_samples_ts, times=3, p=0.1,
#' freq = 8, formula = y ~ s(x, bs="cc"), weight.fun = log_fun)
#' cross_validation
#'
Expand All @@ -53,11 +63,16 @@ setGeneric("twdtwCrossValidation",
#' plot(cross_validation)
#'
#' }
NULL

#' @aliases twdtwCrossValidate-twdtwTimeSeries
#' @inheritParams twdtwCrossValidate
#' @rdname twdtwCrossValidate
#' @export
setMethod(f = "twdtwCrossValidation",
definition = function(object, times, p, ...) twdtwCrossValidation.twdtwTimeSeries(object, times, p, ...))
setMethod(f = "twdtwCrossValidate", signature = "twdtwTimeSeries",
definition = function(object, times, p, ...) twdtwCrossValidate.twdtwTimeSeries(object, times, p, ...))

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

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

Expand Down
Loading

0 comments on commit 665d250

Please sign in to comment.