Here is a (by no means exhaustive) list of examples for distance functions.

# mean absolute distance
ssMeanAbsDistance <- function(sumStats, dataObs, modelObs) {
  # calculate the absolute distance of each summary statistic,
  # and take the mean
  res <- mean(sapply(sumStats, function(x) {
    abs(x(modelObs) - x(dataObs))
  }))

  # return mean distance
  return(res)
}

# vector of absolute distances
ssAbsDistances <- function(sumStats, dataObs, modelObs) {
  # calculate the absolute distance of each summary statistic
  res <- sapply(sumStats, function(x) {
    abs(x(modelObs) - x(dataObs))
  })

  # set names of the vector of distances
  names(res) <- names(sumStats)

  # return vector of distance
  return(res)
}

# mean relative distance
ssMeanRelDistance <- function(sumStats, dataObs, modelObs) {
  # calculate the relative distance of each summary statistic,
  # and take the mean
  res <- mean(sapply(sumStats, function(x) {
    abs((x(modelObs) - x(dataObs)) / x(dataObs))
  }))

  # return mean distance
  return(res)
}

# vector of relative distances
ssRelDistances <- function(sumStats, dataObs, modelObs) {
  # calculate the relative distance of each summary statistic,
  res <- sapply(sumStats, function(x) {
    abs((x(obsTraj) - x(data)) / x(data))
  })

  # set names of the vector of distances
  names(res) <- names(sumStats)

  # return vector of distance
  return(res)
}

Copy and paste any of these you would like to test.

Return to the ABC session.

 

This web site and the material contained in it were originally created in support of an annual short course on Model Fitting and Inference for Infectious Disease Dynamics at the London School of Hygiene & Tropical Medicine. All material is under a MIT license. Please report any issues or suggestions for improvement on the corresponding GitHub issue tracker. We are always keen to hear about any uses of the material here, so please do get in touch using the Discussion board if you have any questions or ideas, or if you find the material here useful or use it in your own teaching.