# Modified versions of the original audpc and audps functions of the agricolae package AUDPC <- function (evaluation, dates, type = "absolute", ymax = 100){ if (!(is.matrix(evaluation) | is.data.frame(evaluation))) { evaluation <- rbind(evaluation) } n <- length(dates) k <- ncol(evaluation) if (n != k) { cat("Error:\nThe number of dates of evaluation \nmust agree with the number of evaluations\n") return() } audpc <- 0 area.total <- ymax * (dates[n] - dates[1]) for (i in 1:(n - 1)) { audpc <- audpc + (evaluation[, i] + evaluation[, i + 1]) * (dates[i + 1] - dates[i])/2 } if (type == "relative") audpc <- audpc/area.total if (type == "std") audpc <- audpc/(dates[n] - dates[1]) if (type == "absolute" | type == "relative" | type == "std") { return(audpc) } else cat("Error: type is 'absolute' or 'relative' or 'std'\n\n") } AUDPS <- function (evaluation, dates, type = "absolute", ymax = 100){ if (!(is.matrix(evaluation) | is.data.frame(evaluation))) { evaluation <- rbind(evaluation) } n <- length(dates) k <- ncol(evaluation) if (n != k) { cat("Error:\nThe number of dates of evaluation \nmust agree with the number of evaluations\n") return() } d1 <- (dates[2] - dates[1])/2 d2 <- (dates[n] - dates[n - 1])/2 d <- d1 + d2 + dates[n] - dates[1] audps <- 0 for (i in 1:(n - 1)) { audps <- audps + evaluation[, i] * (dates[i + 1] - dates[i]) } audps <- audps + evaluation[, n] * (dates[n] - dates[n - 1]) if (type == "relative") audps <- audps/(d * ymax) if (type == "std") audps <- (audps*(n-1))/((dates[n] - dates[1])*n) if (type == "absolute" | type == "relative" | type == "std") { return(audps) } else cat("Error: type is 'absolute' or 'relative' or 'std'\n\n") } # Examples Estimates <- c(1, 1, 3, 4, 5, 5) Time <- c(1:6) AUDPC(Estimates, Time, ymax=5) AUDPC(Estimates, Time, type="relative", ymax=5) AUDPC(Estimates, Time, type="std", ymax=5) AUDPS(Estimates, Time, ymax=5) AUDPS(Estimates, Time, type="relative", ymax=5) AUDPS(Estimates, Time, type="std", ymax=5) plot(Time, Estimates, ylim=c(0, 5), xlab="Time", ylab="Disease Severity", type="o", pch=19, lwd=3) title(paste("AUDPC=", AUDPC(Estimates, Time))) for(i in 1:length(Estimates)){ rect(Time[i], 0, Time[i+1], ((Estimates[i]+Estimates[i+1])/2)) } # Compare with original functions if(!require("agricolae")){ # Download and install the package install.packages(pkgs="agricolae", dependencies=TRUE) } library(agricolae) # Attach the package audpc(Estimates, Time) audps(Estimates, Time) data(disease) dates <- c(1, 2, 3) evaluation <- disease[, c(4, 5, 6)] audpc(evaluation, dates, type="relative") AUDPC(evaluation, dates, type="std") audps(evaluation, dates, type="relative") AUDPS(evaluation, dates, type="std")