Raw File
Tip revision: d32f6f791b0250eb5754ca1cc161e6f2dffca74d authored by Toni Giorgino on 01 September 2015, 17:55:23 UTC
version 1.18-1
Tip revision: d32f6f7
\title{Compute a dissimilarity matrix}

\description{Compute the dissimilarity matrix  between
  a set of single-variate timeseries.  }

# dist(mx,my=mx,method="DTW",...)
  \item{mx}{numeric matrix, containing timeseries as rows}
  \item{my}{numeric matrix, containing timeseries as rows (for cross-distance)}
  \item{...}{arguments passed to the \code{\link{dtw}} call}

 A square matrix whose element \code{[i,j]} holds the Dynamic Time Warp
 distance between row \code{i} (query) and \code{j} (reference) of
 \code{mx} and \code{my}, i.e.  \code{dtw(mx[i,],my[j,])$distance}.


  \code{dtwDist} computes a dissimilarity matrix, akin to
  \code{\link{dist}}, based on the Dynamic Time Warping definition of a
  distance between single-variate timeseries.

  The \code{dtwDist} command is a synonym for the
  \code{\link[proxy]{dist}} function of package \pkg{proxy}; the DTW
  distance is registered as \code{method="DTW"} (see examples below).

  The timeseries are stored as rows in the matrix argument \code{m}. In
  other words, if \code{m} is an N * T matrix, \code{dtwDist} will build
  N*N ordered pairs of timeseries, perform the corresponding N*N
  \code{dtw} alignments, and return all of the results in a matrix. Each
  of the timeseries is T elements long.

  \code{dtwDist} returns a square matrix, whereas the \code{dist} object
  is lower-triangular. This makes sense because in general the DTW
  "distance" is not symmetric (see e.g.  asymmetric step patterns).  To
  make a square matrix with the \code{\link[proxy]{dist}} function
  sematics, use the two-arguments call as \code{dist(m,m)}. This will
  return a square \code{crossdist} object.  }

  To convert a square cross-distance matrix (\code{crossdist} object) to
  a symmetric \code{\link{dist}} object, use a suitable conversion
  strategy (see examples).

\seealso{Other "distance" functions are: \code{\link{dist}},
  \code{\link[vegan]{vegdist}} in package \code{vegan},
  \code{\link[analogue]{distance}} in package \code{analogue}, etc.


## Symmetric step pattern => symmetric dissimilarity matrix;
## no problem coercing it to a dist object:

m <- matrix(0,ncol=3,nrow=4)
m <- row(m)

# Old-fashioned call style would be:
#   dtwDist(m)
#   as.dist(dtwDist(m))

## Find the optimal warping _and_ scale factor at the same time.
## (There may be a better, analytic way)

# Prepare a query and a reference


# Make a set of several references, scaled from 0 to 3 in .1 increments.
# Put them in a matrix, in rows

scaleSet <- seq(0.1,3,by=.1)

# The query has to be made into a 1-row matrix.
# Perform all of the alignments at once, and normalize the result.


# The optimal scale for the reference is 1.0
  xlab="Reference scale factor (denominator)",
  ylab="DTW distance",type="o",
  main="Sine vs scaled cosine alignment, 0 to 4 pi")

## Asymmetric step pattern: we can either disregard part of the pairs
## (as.dist), or average with the transpose

mm <- matrix(runif(12),ncol=3)
dm <- dist(mm,mm,method="DTW",step=asymmetric); # a crossdist object

# Old-fashioned call style would be:
#   dm <- dtwDist(mm,step=asymmetric)
#   as.dist(dm)

## Symmetrize by averaging:

## check definition


\author{Toni Giorgino}
back to top