Skip to content

Instantly share code, notes, and snippets.

@stephenjbarr
Created March 24, 2014 20:09
Show Gist options
  • Save stephenjbarr/9748022 to your computer and use it in GitHub Desktop.
Save stephenjbarr/9748022 to your computer and use it in GitHub Desktop.
library(ggplot2)
library(cumplyr)
library(scales)
library(reshape2)
library(RColorBrewer)
library(tools)
library(gridExtra)
do.call(rbind,lapply(strsplit(as.character(d2$taulist), "::"), as.numeric))
data <- read.csv(file="prob4-23_data.csv")
d2 <- read.csv(file="p4-23-all2.csv")
lts <- do.call(rbind,lapply(strsplit(as.character(d2$taulist), "::"), as.numeric))
N <- 2
colnames(lts) <- paste("L", 1:N, sep="")
wts <- do.call(rbind,lapply(strsplit(as.character(d2$wtlist), "::"), as.numeric))
colnames(wts) <- paste("W", 1:N, sep="")
d2 <- cbind(d2,lts,wts)
myPalette <- colorRampPalette(rev(brewer.pal(11, "Spectral")))
p1 = ggplot(data, aes(x=q,y=r,fill=cost)) + geom_tile()
p1 <- p1 + scale_fill_gradientn(colours = myPalette(10))
p1 <- p1 + ggtitle("HW 4.23, default parameterization")
ggsave(filename="hw4-23-p1_default.png", plot=p1)
p2 <- ggplot(d2, aes(x=q,y=r,fill=cost)) + geom_tile() + facet_wrap(~W1)
p2 <- p2 + scale_fill_gradientn(colours = myPalette(10))
d2s <- subset(d2, q < 30)
p2 <- ggplot(d2s, aes(x=q,y=r,fill=cost)) + geom_tile() + facet_wrap(~W1)
p2 <- p2 + scale_fill_gradientn(colours = myPalette(10)) + ggtitle("Solving 4-23 for varying weights")
ggsave(filename="hw4-23-p2.png", plot=p2)
uuids <- unique(d2s$uuid)l
df.list <- lapply(as.list(uuids), function(x) { subset(d2s, uuid==x)})
mincosts <- do.call(rbind,lapply(df.list, function(x) { x[which.min(x$cost), ]}))
## p.q <- ggplot(mincosts, aes(x=W1,y=q)) + geom_line() + ylim(0, 1.2 * max(mincosts$q)) + ggtitle("Weight 1 vs q")
## p.r <- ggplot(mincosts, aes(x=W1,y=r)) + geom_line() + ylim(0, 1.2 * max(mincosts$r)) + ggtitle("Weight 1 vs r")
## p.qr <- ggplot(mincosts, aes(W1)) + geom_line(aes(y=q, color="red")) + geom_line(aes(y=r, color="blue"))
mcd <- melt(mincosts, id="W1")
mcd <- subset(mcd, variable=="q" | variable =="r")
mcdplot <- ggplot(data=mcd, aes(x=W1, y=value, group=variable)) + geom_line(aes( color=variable)) + ggtitle("W1 vs Q and R")
ggsave(filename="hw4-23-q_and_r.png", plot=mcdplot)
print(xtable(mincosts[,-c(5,6)]), type="html")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment