Skip to content

Instantly share code, notes, and snippets.

@MarkEdmondson1234
Created September 22, 2015 06:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarkEdmondson1234/86ae2c685e60e400dba4 to your computer and use it in GitHub Desktop.
Save MarkEdmondson1234/86ae2c685e60e400dba4 to your computer and use it in GitHub Desktop.
## function to get plot data format
getCompareTable <- function (test_data, prediction) {
require(dplyr)
## plot real vs model bought Sku
actual_freq <- table(model_data$boughtSku)
predicted_freq <- table(prediction)
actual_freq <- actual_freq[order(actual_freq)]
predicted_freq <- predicted_freq[order(predicted_freq)]
actual_freq_s <- data.frame(sku = names(actual_freq),
actual = as.vector(actual_freq),
stringsAsFactors = F)
predicted_freq_s <- data.frame(sku = names(predicted_freq),
predict = as.vector(predicted_freq),
stringsAsFactors = F)
actual_freq_s$actual <- unname(actual_freq_s$actual)
predicted_freq_s$predict <- unname(predicted_freq_s$predict)
compare <- dplyr::left_join(actual_freq_s, predicted_freq_s, by = "sku")
compare
}
## use function to get plot data
compare <- getCompareTable(test, prediction)
## plot the predicted vs actual in test set
library(ggplot2)
library(reshape2)
compare_long <- melt(compare)
g <- ggplot(data = compare_long, aes(x=sku, y = value, colour = variable, group = variable)) + theme_bw()
g <- g + geom_bar(stat = "identity", position = "dodge", aes(fill=variable))
g
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment