ac <- function(p, n, D, d0, power = 2, family = "SAM") {

  family <- match.arg(family, c("SAM", "2SFCA", "KD2SFCA", "Hansen", "log"))
  D <- as.matrix(D)

  if (length(p) != nrow(D)) {
    stop("length of 'p' must equal nrow(D)")
  }
  if (length(n) != ncol(D)) {
    stop("length of 'n' must equal ncol(D)")
  }
  if (family %in% c("2SFCA", "KD2SFCA") && missing(d0)) {
    stop("'d0' is required for family '2SFCA' and 'KD2SFCA'")
  }

  if (family == "SAM") {
    ac.matrix <- t(n / t(p * (D^power)))
    ac.measure <- rowSums(ac.matrix)
  } else if (family == "2SFCA") {
    D[D > d0] <- 0
    D[D > 0] <- 1
    step1 <- p * D
    Rj <- n / colSums(step1)
    step2 <- t(Rj * t(D))
    step2[is.nan(step2)] <- 0
    ac.measure <- rowSums(step2)
  } else if (family == "KD2SFCA") {
    D[D > d0] <- 0
    D0 <- D
    D0[D > 0] <- 1
    step1 <- p * exp(-D^power)
    step1 <- step1 * D0
    Rj <- n / colSums(step1)
    step2 <- t(Rj * t(exp(-D^power)))
    step2 <- step2 * D0
    step2[is.nan(step2)] <- 0
    ac.measure <- rowSums(step2)
  } else if (family == "Hansen") {
    ac.matrix <- n * exp(-power * D)
    ac.measure <- rowSums(ac.matrix)
  } else {
    ac.matrix <- log(n * exp(-power * D)) / power
    ac.measure <- rowSums(ac.matrix)
  }

  return(ac.measure)
}
