Skip to content

Instantly share code, notes, and snippets.

@jkeirstead
Last active December 18, 2015 19:08
Show Gist options
  • Save jkeirstead/5830344 to your computer and use it in GitHub Desktop.
Save jkeirstead/5830344 to your computer and use it in GitHub Desktop.
Demonstration of analytic hierarchy process applied to district heat load forecasting
## District heating AHP example
##
## This example demonstrates how the analytic hierarchy process can be
## used to select a preferred technique for forecasting the thermal
## load of a district heating system.
##
## James Keirstead ([email protected])
## Presented 25 June 2013 at ISIE Conference, Ulsan, South Korea
##
## Load required libraries
library(devtools)
library(plyr)
## The AHP code is available from Github
gist_url <- "https://gist.github.com/jkeirstead/5827295"
source_gist(gist_url)
## Define the criteria and methods to be evaluated
criteria <- c("Accuracy", "Speed", "Theory")
methods <- c("Linear regression", "Time series", "Neural networks")
## Calculate the weights for each method by criteria
criteria_weights <- data.frame()
for (crit in criteria) {
w <- AHP(methods, crit)
criteria_weights <- rbind(criteria_weights,
data.frame(criteria=crit, methods=methods, weights=w$weight))
}
## Calculate the weights of the criteria to the goal
overall_weights <- AHP(criteria, "Thermal load prediction")
## Combine everything and run it
tmp <- merge(criteria_weights,
data.frame(criteria=criteria, cr.weight=overall_weights$weight))
tmp2 <- transform(tmp, total=weights*cr.weight)
result <- ddply(tmp2, .(methods), summarize, score=sum(total))
## Tidy the result and print
result <- arrange(result, desc(score))
print(result)
## Final plot
library(ggplot2)
gg <- ggplot(result, aes(x=methods, y=score)) +
geom_bar(stat="identity") +
theme_bw() +
labs(x="", y="AHP Score")
ggsave("district-heat-ahp.pdf", gg, width=8, height=6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment