Last active
April 17, 2017 12:14
-
-
Save charly06/f56b95e491acb4e0ef8f284a21f1314c to your computer and use it in GitHub Desktop.
A function for calculating a distance based on dtw for time series data with different length. This function can be used in dist(...) in R.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' See: https://stats.stackexchange.com/a/58786/103349 | |
dtw.dist.naomit <- function(query, ref, ...) { | |
queryTrimmed <- na.omit(query) | |
refTrimmed <- na.omit(ref) | |
dif <- length(queryTrimmed) - length(refTrimmed) | |
if (dif > 0) { | |
# refTrimmed is too short and has to be extended in order to avoid | |
# (out of warping window) errors during dtw | |
refTrimmed <- c(refTrimmed, rep(0, abs(dif))) | |
} else if (dif < 0) { | |
# queryTrimmed is too short | |
queryTrimmed <- c(queryTrimmed, rep(0, abs(dif))) | |
} | |
return(dtw(queryTrimmed, refTrimmed, ...)$distance) | |
} | |
if(pr_DB$entry_exists("dtwDistNA")) { | |
pr_DB$delete_entry("dtwDistNA") | |
} | |
pr_DB$set_entry(FUN = dtw.dist.naomit, names = c("dtwDistNA")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment