diff --git a/R/twdtwAssess.R b/R/twdtwAssess.R index 5e862b0..0de3772 100644 --- a/R/twdtwAssess.R +++ b/R/twdtwAssess.R @@ -96,9 +96,9 @@ setGeneric("twdtwAssess", #' twdtw_assess #' #' # Create latex tables -#' xtable(twdtw_assess, table.type="matrix") -#' xtable(twdtw_assess, table.type="accuracy") -#' xtable(twdtw_assess, table.type="area") +#' twdtwXtable(twdtw_assess, table.type="matrix") +#' twdtwXtable(twdtw_assess, table.type="accuracy") +#' twdtwXtable(twdtw_assess, table.type="area") #' #' } NULL diff --git a/R/twdtwCrossValidate.R b/R/twdtwCrossValidate.R index 20e894d..0b13c08 100644 --- a/R/twdtwCrossValidate.R +++ b/R/twdtwCrossValidate.R @@ -62,6 +62,10 @@ setGeneric("twdtwCrossValidate", #' #' plot(cross_validation) #' +#' twdtwXtable(cross_validation) +#' +#' twdtwXtable(cross_validation, show.overall=FALSE) +#' #' } NULL diff --git a/R/twdtwXtable.R b/R/twdtwXtable.R index aa78232..40dd87f 100644 --- a/R/twdtwXtable.R +++ b/R/twdtwXtable.R @@ -23,9 +23,14 @@ setGeneric("twdtwXtable", #' @param category.type a character defining the categories type "numeric" #' or "letter", if NULL then use the class names. Default is NULL. #' -#' @param show.prop if TRUE shows the the estimated proportion of area. +#' @param show.prop if TRUE shows the estimated proportion of area. #' Used with \code{table.type='accuracy'}. Default is TRUE. #' +#' @param show.overall if TRUE shows the overall accuracy of the cross-validation. +#' Default is TRUE. +#' +#' @param conf.int specifies the confidence level (0-1). +#' #' @param ... other arguments to pass to \code{\link[xtable]{xtable}}. #' #' @seealso \code{\link[dtwSat]{twdtwAssess}} and @@ -76,9 +81,9 @@ setGeneric("twdtwXtable", #' twdtw_assess #' #' # Create latex tables -#' xtable(twdtw_assess, table.type="matrix", category.type="letter") -#' xtable(twdtw_assess, table.type="accuracy", category.type="letter") -#' xtable(twdtw_assess, table.type="area", category.type="letter") +#' twdtwXtable(twdtw_assess, table.type="matrix", category.type="letter") +#' twdtwXtable(twdtw_assess, table.type="accuracy", category.type="letter") +#' twdtwXtable(twdtw_assess, table.type="area", category.type="letter") #' #' } NULL @@ -91,7 +96,6 @@ setMethod("twdtwXtable", signature = signature(object = "twdtwAssessment"), definition = function(object, table.type="accuracy", show.prop=TRUE, category.name=NULL, category.type=NULL, time.labels=NULL, ...){ - xtable(object@accuracySummary$ErrorMatrix, ...) y = object@accuracySummary if(!is.null(time.labels)) y = object@accuracyByPeriod[[time.labels]] @@ -114,6 +118,76 @@ setMethod("twdtwXtable", } ) +#' @aliases twdtwXtable-twdtwCrossValidation +#' @inheritParams twdtwXtable +#' @rdname twdtwXtable +#' @export +setMethod("twdtwXtable", + signature = signature(object = "twdtwCrossValidation"), + definition = function(object, conf.int=.95, show.overall=TRUE, + category.name=NULL, category.type=NULL, ...){ + y = summary(object, conf.int = conf.int) + n = nrow(y$Users) + if(is.null(category.name)) + category.name = rownames(y$Users) + if(!is.null(category.type)) + category.name = switch(pmatch(category.type,c("numeric","letter")), + as.character(seq(1:n)), + LETTERS[1:n] + ) + .xtable.crossvalidation(x=y, category.name, show.overall, conf.int, ...) + } +) + +.xtable.crossvalidation = function(x, category.name, show.overall, conf.int, ...){ + + ua = sprintf("%.2f", round(x$Users[["Accuracy"]],2)) + ua_sd = sprintf("(%.2f)", round(x$Users[["sd"]],2)) + ua_ci = sprintf("[%.2f-%.2f]", round(x$Users[["CImin"]],2), round(x$Users[["CImax"]],2)) + + pa = sprintf("%.2f", round(x$Producers[["Accuracy"]],2)) + pa_sd = sprintf("(%.2f)", round(x$Producers[["sd"]],2)) + pa_ci = sprintf("[%.2f-%.2f]", round(x$Producers[["CImin"]],2), round(x$Producers[["CImax"]],2)) + + tbl = data.frame(ua, ua_sd, ua_ci, pa, pa_sd, pa_ci) + table_columns = " & \\multicolumn{3}{c}{User's} & \\multicolumn{3}{c}{Producer's}" + n = 2 + + if(show.overall){ + oa = sprintf("%.2f", round(x$Overall[["Accuracy"]],2)) + oa_sd = sprintf("(%.2f)", round(x$Overall[["sd"]],2)) + oa_ci = sprintf("[%.2f-%.2f]", round(x$Overall[["CImin"]],2), round(x$Overall[["CImax"]],2)) + + tbl$oa = "" + tbl$oa_sd = "" + tbl$oa_ci = "" + + tbl$oa[1] = oa + tbl$oa_sd[1] = oa_sd + tbl$oa_ci[1] = oa_ci + + table_columns = paste0(table_columns, " & \\multicolumn{3}{c}{Overall}") + n = 3 + } + + comment = list() + comment$pos = list() + comment$pos[[1]] = c(0) + comment$pos[[2]] = c(nrow(tbl)) + comment$command = c(paste0(table_columns, "\\\\\n", + "Class", paste(rep(" & $\\mu$ & $\\sigma$ & ci*", n),collapse = ""),"\\\\\n"), + paste("\\hline \n", "\\multicolumn{",ncol(tbl)+1,"}{l}{* ",conf.int*100,"\\% confidence interval.}\n", sep = "")) + + rownames(tbl) = category.name + + tbl = xtable(tbl, ...) + + print.xtable(tbl, add.to.row = comment, include.rownames=TRUE, include.colnames = FALSE, + hline.after = c(-1, 0), sanitize.text.function = function(x) x) + +} + + .xtable.accuracy = function(x, category.name, show.prop, ...){ prop = x$ProportionMatrix diff --git a/man/twdtwAssess.Rd b/man/twdtwAssess.Rd index e06d003..99b6c76 100644 --- a/man/twdtwAssess.Rd +++ b/man/twdtwAssess.Rd @@ -86,9 +86,9 @@ twdtw_assess = twdtwAssess(object = r_lucc, y = validation_samples, twdtw_assess # Create latex tables -xtable(twdtw_assess, table.type="matrix") -xtable(twdtw_assess, table.type="accuracy") -xtable(twdtw_assess, table.type="area") +twdtwXtable(twdtw_assess, table.type="matrix") +twdtwXtable(twdtw_assess, table.type="accuracy") +twdtwXtable(twdtw_assess, table.type="area") } } diff --git a/man/twdtwCrossValidate.Rd b/man/twdtwCrossValidate.Rd index dd65c82..ac3806a 100644 --- a/man/twdtwCrossValidate.Rd +++ b/man/twdtwCrossValidate.Rd @@ -66,6 +66,10 @@ summary(cross_validation) plot(cross_validation) +twdtwXtable(cross_validation) + +twdtwXtable(cross_validation, show.overall=FALSE) + } } \author{ diff --git a/man/twdtwXtable.Rd b/man/twdtwXtable.Rd index 32e6092..8a2a69d 100644 --- a/man/twdtwXtable.Rd +++ b/man/twdtwXtable.Rd @@ -4,12 +4,17 @@ \name{twdtwXtable} \alias{twdtwXtable} \alias{twdtwXtable,twdtwAssessment-method} +\alias{twdtwXtable,twdtwCrossValidation-method} \alias{twdtwXtable-twdtwAssessment} +\alias{twdtwXtable-twdtwCrossValidation} \title{Latex table from accuracy metrics} \usage{ \S4method{twdtwXtable}{twdtwAssessment}(object, table.type = "accuracy", show.prop = TRUE, category.name = NULL, category.type = NULL, time.labels = NULL, ...) + +\S4method{twdtwXtable}{twdtwCrossValidation}(object, conf.int = 0.95, + show.overall = TRUE, category.name = NULL, category.type = NULL, ...) } \arguments{ \item{object}{an object of class twdtwAssessment.} @@ -18,7 +23,7 @@ 'matrix' for error matrix, and 'area' for area and uncertainty. Default is 'accuracy'.} -\item{show.prop}{if TRUE shows the the estimated proportion of area. +\item{show.prop}{if TRUE shows the estimated proportion of area. Used with \code{table.type='accuracy'}. Default is TRUE.} \item{category.name}{a character vector defining the class names. If NULL @@ -31,6 +36,11 @@ or "letter", if NULL then use the class names. Default is NULL.} include all classified periods. Default is NULL.} \item{...}{other arguments to pass to \code{\link[xtable]{xtable}}.} + +\item{conf.int}{specifies the confidence level (0-1).} + +\item{show.overall}{if TRUE shows the overall accuracy of the cross-validation. +Default is TRUE.} } \description{ Creates Latex table from accuracy metrics @@ -80,9 +90,9 @@ twdtw_assess = twdtwAssess(object = r_lucc, y = validation_samples, twdtw_assess # Create latex tables -xtable(twdtw_assess, table.type="matrix", category.type="letter") -xtable(twdtw_assess, table.type="accuracy", category.type="letter") -xtable(twdtw_assess, table.type="area", category.type="letter") +twdtwXtable(twdtw_assess, table.type="matrix", category.type="letter") +twdtwXtable(twdtw_assess, table.type="accuracy", category.type="letter") +twdtwXtable(twdtw_assess, table.type="area", category.type="letter") } }