Skip to content

Commit

Permalink
Fix class order of class definitions.
Browse files Browse the repository at this point in the history
  • Loading branch information
vwmaus committed Nov 27, 2016
1 parent fc96862 commit edb45e2
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 133 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ Maintainer: Victor Maus <[email protected]>
LazyData: true
RoxygenNote: 5.0.1
Collate:
'class-twdtwTimeSeries.R'
'class-twdtwMatches.R'
'class-twdtwRaster.R'
'class-twdtwTimeSeries.R'
'createPatterns.R'
'crossValidation.R'
'data.R'
'dtw.R'
'dwtSat.R'
Expand Down
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ exportMethods(show)
exportMethods(splitDataset)
exportMethods(subset)
exportMethods(twdtwApply)
exportMethods(twdtwAssess)
exportMethods(twdtwClassify)
exportMethods(twdtwCrossValidation)
exportMethods(twdtwMatches)
exportMethods(twdtwRaster)
exportMethods(twdtwTimeSeries)
Expand Down
5 changes: 3 additions & 2 deletions R/class-twdtwMatches.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#' the same length as \code{timeseries} or a list of twdtwMatches.
#' @param ... objects of class twdtwMatches.
#'
#' @include class-twdtwTimeSeries.R
#'
#' @section Slots :
#' \describe{
#' \item{\code{timeseries}:}{An object of class \code{\link[dtwSat]{twdtwTimeSeries-class}} with the satellite time series.}
Expand Down Expand Up @@ -73,8 +75,7 @@
#' length(matches)
#' matches
NULL
setOldClass("twdtwTimeSeries")
twdtwMatches = setClass(
setClass(
Class = "twdtwMatches",
slots = c(timeseries="twdtwTimeSeries",
patterns = "twdtwTimeSeries",
Expand Down
3 changes: 2 additions & 1 deletion R/class-twdtwRaster.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
#' rts = new("twdtwRaster", timeseries = evi, timeline = timeline)
#'
NULL
twdtwRaster = setClass(
setClass(
Class = "twdtwRaster",
slots = c(timeseries = "list", timeline="Date", layers = "character", labels = "character", levels="numeric"),
validity = function(object){
Expand Down Expand Up @@ -158,6 +158,7 @@ setGeneric(name = "twdtwRaster",


#' @inheritParams twdtwRaster
#' @aliases twdtwRaster-create
#' @describeIn twdtwRaster Create object of class twdtwRaster.
#'
#' @examples
Expand Down
6 changes: 3 additions & 3 deletions R/class-twdtwTimeSeries.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# #
###############################################################


#' @title class "twdtwTimeSeries"
#' @name twdtwTimeSeries-class
#' @aliases twdtwTimeSeries
Expand Down Expand Up @@ -50,7 +49,7 @@
#' ncol(ptt)
#' dim(ptt)
NULL
twdtwTimeSeries = setClass(
setClass(
Class = "twdtwTimeSeries",
slots = c(timeseries = "list", labels = "factor"),
validity = function(object){
Expand Down Expand Up @@ -96,6 +95,7 @@ setGeneric(name = "twdtwTimeSeries",
)

#' @inheritParams twdtwTimeSeries-class
#' @aliases twdtwTimeSeries-create
#' @describeIn twdtwTimeSeries Create object of class twdtwTimeSeries.
#'
#' @examples
Expand Down Expand Up @@ -146,4 +146,4 @@ setMethod(f = "twdtwTimeSeries",





110 changes: 110 additions & 0 deletions R/crossValidation.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#' @title Cross-validation
#' @name Cross-validation
#'
#' @author Victor Maus, \email{vwmaus1@@gmail.com}
#'
#' @description This functions create data partitions and compute Cross-validation metrics.
#'
#' @param object an object of class \code{\link[dtwSat]{twdtwTimeSeries}} or
#' \code{\link[dtwSat]{twdtwMatches}}.
#'
#' @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}}.
#'
#' @param matrix logical. If TRUE retrieves the confusion matrix.
#' FALSE retrieves User's Accuracy (UA) and Producer's Accuracy (PA).
#' Dafault is FALSE.
#'
#' @details
#' \describe{
#' \item{\code{splitDataset}:}{This function splits the a set of time
#' series into training and validation. The function uses stratified
#' sampling and a simple random sampling for each stratum. Each data partition
#' returned by this function has the temporal patterns and a set of time series for
#' validation.}
#' \item{\code{twdtwCrossValidation}:}{The function \code{splitDataset} performs the Cross-validation of
#' the classification based on the labels of the classified time series
#' (Reference) and the labels of the classification (Predicted). This function
#' returns a data.frame with User's and Produce's Accuracy or a list for confusion
#' matrices.}
#' }
#'
#' @seealso
#' \code{\link[dtwSat]{twdtwMatches-class}},
#' \code{\link[dtwSat]{twdtwApply}}, and
#' \code{\link[dtwSat]{twdtwClassify}}.
#'
#' @examples
#' \dontrun{
#' load(system.file("lucc_MT/field_samples_ts.RData", package="dtwSat"))
#' set.seed(1)
#' partitions = splitDataset(field_samples_ts, p=0.1, times=5,
#' freq = 8, formula = y ~ s(x, bs="cc"))
#' log_fun = logisticWeight(alpha=-0.1, beta=50)
#' twdtw_res = lapply(partitions, function(x){
#' res = twdtwApply(x = x$ts, y = x$patterns, weight.fun = log_fun, n=1)
#' twdtwClassify(x = res)
#' })
#' cross_validation = twdtwCrossValidation(twdtw_res)
# head(cross_validation, 5)
#' }
NULL

setGeneric("splitDataset", function(object, times, p, ...) standardGeneric("splitDataset"))

#' @rdname Cross-validation
#' @aliases splitDataset
#' @export
setMethod("splitDataset", "twdtwTimeSeries",
function(object, times=1, p=0.1, ...) splitDataset.twdtwTimeSeries(object, times=times, p=p, ...))

splitDataset.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, ...)
list(patterns=patt, ts=validation_ts)
})

res
}

setGeneric("twdtwCrossValidation", function(object, matrix=FALSE) standardGeneric("twdtwCrossValidation"))

#' @rdname Cross-validation
#' @aliases twdtwCrossValidation
#' @export
setMethod("twdtwCrossValidation", "list",
function(object, matrix) twdtwCrossValidation.twdtwTimeSeries(object, matrix=matrix))

twdtwCrossValidation.twdtwTimeSeries = function(object, matrix){

res = lapply(object, function(x){
ref = labels(x)$timeseries
levels = sort(as.character(unique(ref)))
labels = levels
# pred = factor(do.call("rbind", x[])$label, levels, labels)
pred = do.call("rbind", lapply(x[], function(xx) as.character(xx$label[which.min(xx$distance)])) )
ref = factor(ref, levels, labels)
table(Reference=ref, Predicted=pred)
})

if(!matrix){
res = do.call("rbind", lapply(seq_along(res), function(i){
x = res[[i]]
Users = diag(x) / rowSums(x)
Producers = diag(x) / colSums(x)
data.frame(resample=i,label=names(Users), UA = Users, PA = Producers, row.names=NULL)
}))
}

res

}
112 changes: 1 addition & 111 deletions R/miscellaneous.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ setMethod("shiftDates", "list",
function(object, year)
shiftDates(twdtwTimeSeries(object), year=year)[])

setOldClass("zoo")
#' @rdname shiftDates
#' @aliases shiftDates-zoo
#' @export
Expand All @@ -104,115 +105,4 @@ shiftDates.twdtwTimeSeries = function(x, year){
}


#' @title Classification assessment
#' @name Assessment
#'
#' @author Victor Maus, \email{vwmaus1@@gmail.com}
#'
#' @description This functions create data partitions and compute assessment metrics.
#'
#' @param object an object of class \code{\link[dtwSat]{twdtwTimeSeries}} or
#' \code{\link[dtwSat]{twdtwMatches}}.
#'
#' @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}}.
#'
#' @param matrix logical. If TRUE retrieves the confusion matrix.
#' FALSE retrieves User's Accuracy (UA) and Producer's Accuracy (PA).
#' Dafault is FALSE.
#'
#' @details
#' \describe{
#' \item{\code{splitDataset}:}{This function splits the a set of time
#' series into training and validation. The function uses stratified
#' sampling and a simple random sampling for each stratum. Each data partition
#' returned by this function has the temporal patterns and a set of time series for
#' validation.}
#' \item{\code{twdtwAssess}:}{The function \code{splitDataset} performs the assessment of
#' the classification based on the labels of the classified time series
#' (Reference) and the labels of the classification (Predicted). This function
#' returns a data.frame with User's and Produce's Accuracy or a list for confusion
#' matrices.}
#' }
#'
#' @seealso
#' \code{\link[dtwSat]{twdtwMatches-class}},
#' \code{\link[dtwSat]{twdtwApply}}, and
#' \code{\link[dtwSat]{twdtwClassify}}.
#'
#' @examples
#' \dontrun{
#' load(system.file("lucc_MT/field_samples_ts.RData", package="dtwSat"))
#' set.seed(1)
#' partitions = splitDataset(field_samples_ts, p=0.1, times=5,
#' freq = 8, formula = y ~ s(x, bs="cc"))
#' log_fun = logisticWeight(alpha=-0.1, beta=50)
#' twdtw_res = lapply(partitions, function(x){
#' res = twdtwApply(x = x$ts, y = x$patterns, weight.fun = log_fun, n=1)
#' twdtwClassify(x = res)
#' })
#' assessment = twdtwAssess(twdtw_res)
#' head(assessment, 5)
#' }
NULL

setGeneric("splitDataset", function(object, times, p, ...) standardGeneric("splitDataset"))

#' @rdname Assessment
#' @aliases splitDataset
#' @export
setMethod("splitDataset", "twdtwTimeSeries",
function(object, times=1, p=0.1, ...) splitDataset.twdtwTimeSeries(object, times=times, p=p, ...))

splitDataset.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, ...)
list(patterns=patt, ts=validation_ts)
})

res
}


setGeneric("twdtwAssess", function(object, matrix=FALSE) standardGeneric("twdtwAssess"))

#' @rdname Assessment
#' @aliases twdtwAssess
#' @export
setMethod("twdtwAssess", "list",
function(object, matrix) twdtwAssess.twdtwTimeSeries(object, matrix=matrix))

twdtwAssess.twdtwTimeSeries = function(object, matrix){

res = lapply(object, function(x){
ref = labels(x)$timeseries
levels = sort(as.character(unique(ref)))
labels = levels
# pred = factor(do.call("rbind", x[])$label, levels, labels)
pred = do.call("rbind", lapply(x[], function(xx) as.character(xx$label[which.min(xx$distance)])) )
ref = factor(ref, levels, labels)
table(Reference=ref, Predicted=pred)
})

if(!matrix){
res = do.call("rbind", lapply(seq_along(res), function(i){
x = res[[i]]
Users = diag(x) / rowSums(x)
Producers = diag(x) / colSums(x)
data.frame(resample=i,label=names(Users), UA = Users, PA = Producers, row.names=NULL)
}))
}

res

}

21 changes: 10 additions & 11 deletions man/Assessment.Rd → man/Cross-validation.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/twdtwRaster-class.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/twdtwTimeSeries-class.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit edb45e2

Please sign in to comment.