diff --git a/NAMESPACE b/NAMESPACE index 7d0b373..9c16d2d 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -95,6 +95,7 @@ importFrom(mgcv,gam) importFrom(mgcv,predict.gam) importFrom(plyr,alply) importFrom(proxy,dist) +importFrom(proxy,pr_DB) importFrom(reshape2,melt) importFrom(scales,date_format) importFrom(scales,percent) diff --git a/R/twdtwDist.R b/R/twdtwDist.R index 786fad9..42a5931 100644 --- a/R/twdtwDist.R +++ b/R/twdtwDist.R @@ -1,12 +1,27 @@ - -.twdtwDist = function (object, ...) { - d = lapply(seq_along(object), function (i) { - x = subset(object, i) - y = subset(object, -i) - res = twdtwApply(x, y, n=1) - res[] - }) - d +.twdtwDist = function (x, y, ...) { + d = do.call("rbind", lapply(x, function (xx) { + sapply(y, function (yy) { + res = twdtwApply(twdtwTimeSeries(xx), twdtwTimeSeries(yy), n=1, ...) + res[[1]]$distance[1] + }) + })) + as.matrix(d) } +# library(dtwSat) +# library(dtw) +# library(dtwclust) +# log_fun = logisticWeight(-0.1, 100) +# phi = proxy::dist(x=MOD13Q1.patterns.list, y=MOD13Q1.patterns.list[1:2], method="TWDTW", weight.fun=log_fun) +# phi +# a=twdtwApply(twdtwTimeSeries(MOD13Q1.patterns.list), twdtwTimeSeries(MOD13Q1.patterns.list), n=1, weight.fun=log_fun) +# a[[3]] +# +# aux = c(MOD13Q1.patterns.list, MOD13Q1.patterns.list, MOD13Q1.patterns.list) +# a1 = tsclust(series = aux, distance = "DTW", trace = TRUE, type = "hierarchical", k = 3) +# log_fun = logisticWeight(-0.1, 100) +# a2 = tsclust(series = aux, distance = "TWDTW", trace = TRUE, type = "hierarchical", k = 3, weight.fun=log_fun, span = 300) +# a1 +# a2 + diff --git a/R/zzz.R b/R/zzz.R index 4a64a5f..c05433a 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -16,6 +16,15 @@ packageStartupMessage( sprintf("Loaded dtwSat v%s. See ?dtwSat for help, citation(\"dtwSat\") for use in publication.\n", utils::packageDescription("dtwSat")$Version) ) + + ## Register TWDTW as a distance function into package proxy + pr_DB$set_entry(FUN = .twdtwDist, + names = c("TWDTW","twdtw"), + loop = FALSE, + type = "metric", + description = "Time-Weighted Dynamic Time Warping", + reference = "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. IEEE Journal of Selected Topics in Applied Earth Observations and Remote Sensing, 9 (8), pp. 3729--3739. ." + ); } #' @import zoo @@ -23,7 +32,7 @@ #' @import ggplot2 #' @import methods #' @import rgdal -#' @importFrom proxy dist +#' @importFrom proxy dist pr_DB #' @importFrom reshape2 melt #' @importFrom scales pretty_breaks date_format percent #' @importFrom grDevices terrain.colors gray.colors