d.combo.public <- read.csv(file=paste(path,"d.combo.public.csv",sep="") )
d <- d.combo.public

# Mhobs := number of impressions per placement
# mhobs := number of impressions per placement-creative pair (532 uniques)
# yhobs := number of conversions per placement-creative pair (532 uniques)

## Recall: 'h' indicates incremental counts per period and 't' is cumulative counts
# e.g., m mhobs.combo

# For batching at weekly level
Mhobs.combo <- read.csv(file=paste(path,"capMhobs.combo.csv",sep="") )
mhobs.combo <- read.csv(file=paste(path,"mhobs.combo.csv",sep="") )
yhobs.combo <- read.csv(file=paste(path,"yhobs.combo.csv",sep="") )

# # For batching at daily level
# Mhobs.combo <- read.csv( file=paste(path,"capMhobs.combo.daily.csv",sep="") )
# mhobs.combo <- read.csv( file=paste(path,"mhobs.combo.daily.csv",sep="") )
# yhobs.combo <- read.csv( file=paste(path,"yhobs.combo.daily.csv",sep="") )

hend <- ncol(Mhobs.combo) # number of time periods

K <- 4 # number of unique ad creatives per placement

allplacements <- d$placement
n.placements <- length(unique(allplacements))

J <- n.placements # number of placements: 133

mtotalvec <- mtobs.combo[,hend]
muobsagg <- sum(ytobs.combo[,hend])/sum(mtobs.combo[,hend])

muobsvec <- ytobs.combo[,hend]/mtobs.combo[,hend]
muobsJbyK <- matrix(muobsvec, ncol=4, byrow=T)

yobs_byplace <- tapply(ytobs.combo[,hend],IND=d$placeid,FUN=sum)
mobs_byplace <- tapply(mtobs.combo[,hend],IND=d$placeid,FUN=sum)
muobs_byplace <- yobs_byplace/mobs_byplace

# set truth for data-generating process
mutruevec <- muobsvec
mutruemat <- matrix(muobsvec, nrow=numobs, ncol=hend, byrow=F)

## ==== SUMMARIZE RAW DATA ======

# cumulative conversions
#ytobs have cumulative conversions
#mtobs have cumulative impressions

yaggovertimecum.combo <- apply(ytobs.combo,2,sum)
maggovertimecum.combo <- apply(mtobs.combo, 2, sum)
paggovertimecum.combo <- yaggovertimecum.combo/maggovertimecum.combo

## Allocation over time
## Impressions by concept over time
impvol_concept_by_time.combo <- cbind(
  apply( mhobs.combo[d$concept==levels(d$concept)[1],], 2, sum),
  apply( mhobs.combo[d$concept==levels(d$concept)[2],], 2, sum),
  apply( mhobs.combo[d$concept==levels(d$concept)[4],], 2, sum),
  apply( mhobs.combo[d$concept==levels(d$concept)[4],], 2, sum)
)
maggovertime.combo <- apply(mhobs.combo, 2, sum)
impalloc_concept_by_time.combo <- cbind(
  maggovertime.combo,
  impvol_concept_by_time/maggovertime.combo
  )

windows()
ylimcur <- 1e6*range(paggovertimecum.test,paggovertimecum.control)
plot( 1:hend, 1e6*paggovertimecum.control, type="n",
      ylim=ylimcur, frame.plot=F,axes=F,ylab="",xlab="",main="")
axis(1,1:hend)
axis(2)
lines(1:hend, 1e6*paggovertimecum.control, lty=2, lwd=1)
lines(1:hend, 1e6*paggovertimecum.test, lty=1, lwd=2)
points(1:hend, 1e6*paggovertimecum.control, pch=15)
points(1:hend, 1e6*paggovertimecum.test, pch=16)
title(xlab="time periods", ylab="conversion rate (per million)")

# change relative to starting points
paggovertimecumrel.control <- 100*(paggovertimecum.control-paggovertimecum.control[1])/paggovertimecum.control[1]
paggovertimecumrel.test <- 100*(paggovertimecum.test-paggovertimecum.test[1])/paggovertimecum.test[1]

windows()
ylimcur <- range(paggovertimecumrel.test,paggovertimecumrel.control)
plot( 1:hend, paggovertimecumrel.control, type="n",
      ylim=ylimcur, frame.plot=F,axes=F,ylab="",xlab="",main="")
axis(1,1:hend)
axis(2)
lines(1:hend, paggovertimecumrel.control, lty=2, lwd=1)
lines(1:hend, paggovertimecumrel.test, lty=1, lwd=2)
points(1:hend, paggovertimecumrel.control, pch=15)
points(1:hend, paggovertimecumrel.test, pch=16)
title(xlab="time periods", ylab="relative change (as % over starting conversion rate)")

# impressions over time and absolute conversion rates
windows()
xscale <- cumsum(maggovertime.combo)/1e6
ylimcur <- 1e6*range(paggovertimecum.test,paggovertimecum.control)
plot( xscale, 1e6*paggovertimecum.control, type="l",col="white",
      ylim=ylimcur, frame.plot=F,axes=F,ylab="",xlab="",main="")
axis(1,xscale, at=seq(0,900,by=100), labels=seq(0,900,by=100) )
axis(2)
lines(xscale, 1e6*paggovertimecum.control, lty=2, lwd=1)
lines(xscale, 1e6*paggovertimecum.test, lty=1, lwd=2)
points(xscale, 1e6*paggovertimecum.control, pch=15)
points(xscale, 1e6*paggovertimecum.test, pch=16)
title(xlab="impressions over time (millions)", ylab="conversion rate (per million)")

# impressions over time and relative conversion rates
windows()
xscale <- cumsum(maggovertime.combo)/1e6
ylimcur <- range(paggovertimecumrel.test,paggovertimecumrel.control)
plot( xscale, paggovertimecumrel.control, type="l",col="white",
      ylim=ylimcur, frame.plot=F,axes=F,ylab="",xlab="",main="")
axis(1,xscale, at=seq(0,900,by=100), labels=seq(0,900,by=100) )
axis(2)
lines(xscale, paggovertimecumrel.control, lty=2, lwd=1)
lines(xscale, paggovertimecumrel.test, lty=1, lwd=2)
points(xscale, paggovertimecumrel.control, pch=15)
points(xscale, paggovertimecumrel.test, pch=16)
title(xlab="impressions over time (millions)", ylab="relative change (as % over starting conversion rate)")

# change relative to starting points
paggovertimeincrel.control <- 100*(paggovertime.control-paggovertime.control[1])/paggovertime.control[1]
paggovertimeincrel.test <- 100*(paggovertime.test-paggovertime.test[1])/paggovertime.test[1]

# NOT CUMULATIVE impressions over time and relative conversion rates
windows()
xscale <- cumsum(maggovertime.combo)/1e6
ylimcur <- range(paggovertimeincrel.test,paggovertimeincrel.control)
plot( xscale, paggovertimeincrel.control, type="l",col="white",
      ylim=ylimcur, frame.plot=F,axes=F,ylab="",xlab="",main="")
axis(1,xscale, at=seq(0,900,by=100), labels=seq(0,900,by=100) )
axis(2)
lines(xscale, paggovertimeincrel.control, lty=2, lwd=1)
lines(xscale, paggovertimeincrel.test, lty=1, lwd=2)
points(xscale, paggovertimeincrel.control, pch=15)
points(xscale, paggovertimeincrel.test, pch=16)
title(xlab="impressions over time (millions)", ylab="relative change (as % over starting conversion rate)")

#====== plot distribution of conversion rates ====

summary(muobsvec)

numobs <- nrow(d)
sum(muobsvec == 0)
sum(muobsvec < 1e-6)
sum(muobsvec < 1e-5)
sum(muobsvec < 1e-4)
sum(muobsvec == 0)/numobs
sum(muobsvec < 1e-6)/numobs
sum(muobsvec < 1e-5)/numobs
sum(muobsvec < 1e-4)/numobs

cvr1 <- muobsvec[muobsvec<1e-5]
cvr2 <- muobsvec[muobsvec >= 1e-5]

hist(muobsvec[muobsvec<1e-5 & muobsvec>0])

### --- FIX THIS AND RESAVE IT AS 10e-5 correctly as 1/100,000
pdf(file=paste(path,"cvrhist.pdf",sep=""),w=7,h=4)
  par(mfrow=c(1,2))
  hist(cvr1, main="Rates less than 1/100,000",xlab="Obs. conversion rate")
  hist(cvr2, main="Rates greater than 1/100,000",xlab="Obs. conversion rate",ylab="")
dev.off()

pdf(file=paste(path,"cvrbyimp.pdf",sep=""))
  plot(mtotalvec,muobsvec,log="xy",ylim=c(2e-7,max(muobsvec)), axes=F,ylab="",xlab="")
  title(xlab="Total impressions (powers of 10)", ylab="Obs. conversion rate (powers of 10)")
  axis(1,at=10^(1:8),labels=1:8)
  axis(2,at=10^(-7:-3),labels=-7:-3)
  abline(h=muobsagg,col="darkgray",lty=2)
  points(mtotalvec,muobsvec)
  points(mtotalvec[muobsvec==0], rep(2e-7,sum(muobsvec==0)) , pch=2)
  abline(h=3e-7)
dev.off()

pdf(file=paste(path,"cvrbyimp_byplace.pdf",sep=""))
  plot(mobs_byplace,muobs_byplace,log="xy",ylim=c(2e-7,max(muobs_byplace)), axes=F,ylab="",xlab="")
  title(xlab="Total impressions (powers of 10)", ylab="Obs. conversion rate (powers of 10)")
  axis(1,at=10^(1:8),labels=1:8)
  axis(2,at=10^(-7:-3),labels=-7:-3)
  abline(h=muobsagg,col="darkgray",lty=2)
  points(mobs_byplace,muobs_byplace)
  points(mobs_byplace[muobs_byplace==0], rep(2e-7,sum(muobs_byplace==0)) , pch=2)
  abline(h=3e-7)
dev.off()

## clicks #######
cor.test(dall$conversions/dall$impressions, dall$clicks/dall$impressions)

conversions.combo <- (dall$conversions[indTest] + dall$conversions[indControl])
impressions.combo <- (dall$impressions[indTest] + dall$impressions[indControl])
clicks.combo <- (dall$clicks[indTest] + dall$clicks[indControl])
cvr.combo <- conversions.combo/impressions.combo
ctr.combo <- clicks.combo/impressions.combo

cor.test(cvr.combo,ctr.combo)

pdf(file=paste(path,"ctrhist.pdf",sep=""),w=7,h=4)
  par(mfrow=c(1,2))
  ctr1 <- ctr.combo[ctr.combo<1e-3]
  ctr2 <- ctr.combo[ctr.combo >= 1e-3]
  hist(ctr1, main="Rates less than 1/1,000",xlab="Obs. click through rate")
  hist(ctr2, main="Rates greater than 1/1,000",xlab="Obs. click through rate",ylab="")
dev.off()

pdf(file=paste(path,"ctrbyimp.pdf",sep=""))
 plot(mtotalvec,ctr.combo,log="xy",ylim=c(1e-5,max(ctr.combo)), axes=F,ylab="",xlab="")
  title(xlab="Total impressions (powers of 10)", ylab="Obs. click through rate (powers of 10)")
  axis(1,at=10^(1:8),labels=1:8)
  axis(2,at=10^(-7:-1),labels=-7:-1)
  abline(h=muobsagg,col="darkgray",lty=2)
  points(mtotalvec,ctr.combo)
dev.off()

pdf(file=paste(path,"cvrbyctr.pdf",sep=""))
  plot( cvr.combo, ctr.combo,
    xlim=range(1e-7,5e-4), ylim=c(1e-5,5e-3),log="xy", axes=F, ylab="",xlab="")
  title(xlab="Obs. conversion rate (powers of 10)", ylab="Obs. click through rate (powers of 10)")
  axis(1,at=10^(-7:-1),labels=-7:-1)
  axis(2,at=10^(-7:-1),labels=-7:-1)
  points( rep(1e-7,sum(cvr.combo==0)), ctr.combo[cvr.combo==0], pch=2)
dev.off()

##=============================
##
## show uncertainty around actual outcomes
# path of cumulative conversions obtained over time
# save each "policy" result as a matrix of G worlds by T time

# data = Test, Control, Combined

obspropvec.control <- ytobs[indControl,hend]/mtobs[indControl,hend]
obspropvec.test <- ytobs[indTest,hend]/mtobs[indTest,hend]
obspropvec.combo <- ytobs.combo[,hend]/mtobs.combo[,hend]

cbind(obspropvec.combo, obspropvec.test)
plot(obspropvec.combo, obspropvec.test)
cor.test(  sort(obspropvec.test, method = "sh", index.return=TRUE)$ix,
           sort(obspropvec.control, method = "sh", index.return=TRUE)$ix )
cor.test(  sort(obspropvec.test, method = "sh", index.return=TRUE)$ix,
           sort(obspropvec.combo, method = "sh", index.return=TRUE)$ix )
#recall
# J <- 133
# K <- 4
nworlds <- 100

obspropmat.combo <- matrix(obspropvec.combo, J*K, hend) #columns are identical, stationarity
obspropmat.test <- matrix(obspropvec.test, J*K, hend)
obspropmat.control <- matrix(obspropvec.control, J*K, hend)

write.csv(obspropvec.combo, paste(path,"mutruevec_obspropvec_combo.csv",sep="") )

##===============

################################################

mutruemat <- obspropmat.combo
mutruevec <- obspropvec.combo
Mhobs.cur <- Mhobs.combo

################################################

wmat.balanced <- matrix(1/K,J*K,hend)
mhstar.cur <- round(Mhobs.cur*wmat.balanced)

ystarovertime <- matrix(0,nworlds,hend)
yhstar.all <- array(0, dim=c(J*K,hend,nworlds))
for (i in 1:nworlds){
  yhstarcur <- matrix(rbinom(J*K*hend, mhstar.cur, mutruemat) , J*K, hend, byrow=F)
  ystarovertime[i,] <- apply(yhstarcur,2,sum)
  yhstar.all[,,i] <- yhstarcur
}

ytotaldist.balanced <- apply(yhstar.all,3,sum)
ptotaldist.balanced <- apply(yhstar.all,3,sum)/sum(mhstar.cur)

 #**** be sure to convert vector to matrix correctly **** #
maggovertime <- apply(mhstar.cur, 2, sum)
maggovertime.mat <- matrix(maggovertime,nworlds,hend, byrow=T)

yaggovertime.combo <- apply(yhobs.combo,2,sum)
paggovertime.combo <- yaggovertime.combo/maggovertime

ystarovertime.min <- apply(ystarovertime, 2, min)
ystarovertime.max <- apply(ystarovertime, 2, max)

pstarovertime <- ystarovertime/maggovertime.mat
pstarovertime.min <- apply(pstarovertime, 2, min)
pstarovertime.max <- apply(pstarovertime, 2, max)
pstarovertime.mean <- apply(pstarovertime, 2, mean)
pstarovertime.95top <- apply(pstarovertime, 2, quantile, .975)
pstarovertime.95bot <- apply(pstarovertime, 2, quantile, .025)

ylimcur <- 1e6*range(c(pstarovertime,paggovertime.combo))
plot( 1:hend, 1e6*paggovertime.combo, type="l",
      ylim=ylimcur, frame.plot=F,axes=F,ylab="",xlab="",main="")
axis(1,1:hend)
axis(2)
lines(1:hend, 1e6*pstarovertime.95bot, lty=2)
lines(1:hend, 1e6*pstarovertime.95top, lty=2)
lines(1:hend, 1e6*pstarovertime.mean, lty=2, lwd=2)
lines(1:hend, 1e6*paggovertime.combo, lty=1, lwd=2)
title(xlab="time periods", ylab="conversion rate (per million)")

# cumulative conversions

 #**** be sure to convert vector to matrix correctly **** #
maggovertimecum <- cumsum( apply(mhstar.cur, 2, sum) )
maggovertimecum.mat <- matrix(maggovertimecum,nworlds,hend, byrow=T)

yaggovertimecum.combo <- cumsum( apply(yhobs.combo,2,sum) )
paggovertimecum.combo <- yaggovertimecum.combo/maggovertimecum

ystarovertimecum <- t( apply(ystarovertime, 1, cumsum) )

ystarovertimecum.min <- apply(ystarovertimecum, 2, min)
ystarovertimecum.max <- apply(ystarovertimecum, 2, max)

rbind(ystarovertimecum.min,
      yaggovertimecum.combo,
      ystarovertimecum.max)

pstarovertimecum <- ystarovertimecum/maggovertimecum.mat
pstarovertimecum.min <- apply(pstarovertimecum, 2, min)
pstarovertimecum.max <- apply(pstarovertimecum, 2, max)
pstarovertimecum.mean <- apply(pstarovertimecum, 2, mean)
pstarovertimecum.95top <- apply(pstarovertimecum, 2, quantile, .975)
pstarovertimecum.95bot <- apply(pstarovertimecum, 2, quantile, .025)

windows()
ylimcur <- 1e6*range(c(pstarovertimecum,paggovertimecum.combo,paggovertimecum.control,paggovertimecum.test))
plot( 1:hend, 1e6*paggovertimecum.combo, type="n",
      ylim=ylimcur, frame.plot=F,axes=F,ylab="",xlab="",main="")
axis(1,1:hend)
axis(2)
lines(1:hend, 1e6*pstarovertimecum.95bot, lty=2)
lines(1:hend, 1e6*pstarovertimecum.95top, lty=2)
lines(1:hend, 1e6*pstarovertimecum.mean, lty=2, lwd=1)
lines(1:hend, 1e6*paggovertimecum.control, lty=2, lwd=2)
lines(1:hend, 1e6*paggovertimecum.test, lty=1, lwd=2)
title(xlab="time periods", ylab="cumulative conversion rate (per million)")

windows()
ylimcur <- 1e6*range(c(pstarovertimecum,paggovertimecum.combo,paggovertimecum.control,paggovertimecum.test))
xscale <- cumsum(maggovertime.combo)/1e6
plot( xscale, 1e6*paggovertimecum.combo, type="n",
      ylim=ylimcur, frame.plot=F,axes=F,ylab="",xlab="",main="")
axis(1,xscale, at=seq(0,900,by=100), labels=seq(0,900,by=100) )
axis(2)
lines(xscale, 1e6*pstarovertimecum.95bot, lty=2)
lines(xscale, 1e6*pstarovertimecum.95top, lty=2)
lines(xscale, 1e6*pstarovertimecum.mean, lty=2, lwd=1)
lines(xscale, 1e6*paggovertimecum.control, lty=2, lwd=2)
lines(xscale, 1e6*paggovertimecum.test, lty=1, lwd=2)
title(xlab="impressions through time (millions)", ylab="cumulative conversion rate (per million)")

# change relative to starting points
#****  for denominator, want all columns to be identical to first period ****
pstarovertimecumrel <- 100*(pstarovertimecum-pstarovertimecum[,1]) / matrix(pstarovertimecum[,1],nworlds,hend,byrow=F)
pstarovertimecumrel.min <- apply(pstarovertimecumrel, 2, min)
pstarovertimecumrel.max <- apply(pstarovertimecumrel, 2, max)
pstarovertimecumrel.mean <- apply(pstarovertimecumrel, 2, mean)
pstarovertimecumrel.95top <- apply(pstarovertimecumrel, 2, quantile, .975)
pstarovertimecumrel.95bot <- apply(pstarovertimecumrel, 2, quantile, .025)

pdf( file=paste(path,"fieldwithbounds.pdf",sep="") )
par(mar=c(5,5,4,2)+0.1)  #c(bottom, left, top, right), The default is c(5, 4, 4, 2) + 0.1.
ylimcur <- range(c(pstarovertimecumrel,paggovertimecumrel.control,paggovertimecumrel.test))
xscale <- cumsum(maggovertime.combo)/1e6
plot( xscale, paggovertimecumrel.test, type="n",
      ylim=ylimcur, frame.plot=F,axes=F,ylab="",xlab="",main="")
xscale <- cumsum(maggovertime.combo)/1e6
axis(1,xscale, at=seq(0,900,by=100), labels=seq(0,900,by=100) )
axis(2)
lines(xscale, pstarovertimecumrel.95bot, lty=2)
lines(xscale, pstarovertimecumrel.95top, lty=2)
lines(xscale, pstarovertimecumrel.mean, lty=2, lwd=1)
lines(xscale, paggovertimecumrel.control, lty=2, lwd=2)
lines(xscale, paggovertimecumrel.test, lty=1, lwd=2)
points(xscale, paggovertimecumrel.test, pch=1)
title(xlab="cumulative impressions through time (millions)",
      ylab="change in cumulative conversion rate \n (% of conversion rate in initial period)")
dev.off()

pdf( file=paste(path,"fieldrelative.pdf",sep="") )
par(mar=c(5,5,4,2)+0.1)  #c(bottom, left, top, right), The default is c(5, 4, 4, 2) + 0.1.
ylimcur <- range(c(pstarovertimecumrel,paggovertimecumrel.control,paggovertimecumrel.test))
xscale <- cumsum(maggovertime.combo)/1e6
plot( xscale, paggovertimecumrel.test, type="n",
      ylim=ylimcur, frame.plot=F,axes=F,ylab="",xlab="",main="")
xscale <- cumsum(maggovertime.combo)/1e6
axis(1,xscale, at=seq(0,900,by=100), labels=seq(0,900,by=100) )
axis(2)
lines(xscale, paggovertimecumrel.control, lty=2, lwd=2)
lines(xscale, paggovertimecumrel.test, lty=1, lwd=2)
points(xscale, paggovertimecumrel.test, pch=1)
title(xlab="cumulative impressions through time (millions)",
      ylab="change in cumulative conversion rate \n (% of conversion rate in initial period)")
dev.off()

pdf( file=paste(path,"fieldrelative_inc.pdf",sep="") )
paggovertimeincrel.control <- 100*(paggovertime.control-paggovertime.control[1])/paggovertime.control[1]
paggovertimeincrel.test <- 100*(paggovertime.test-paggovertime.test[1])/paggovertime.test[1]
par(mar=c(5,5,4,2)+0.1)  #c(bottom, left, top, right), The default is c(5, 4, 4, 2) + 0.1.
ylimcur <- range(paggovertimeincrel.test,paggovertimeincrel.control)
xscale <- cumsum(maggovertime.combo)/1e6
plot( xscale, paggovertimeincrel.control, type="l",col="white",
      ylim=ylimcur, frame.plot=F,axes=F,ylab="",xlab="",main="")
axis(1,xscale, at=seq(0,900,by=100), labels=seq(0,900,by=100) )
axis(2)
lines(xscale, paggovertimeincrel.control, lty=2, lwd=2)
lines(xscale, paggovertimeincrel.test, lty=1, lwd=2)
points(xscale, paggovertimeincrel.test, pch=1)
title(xlab="cumulative impressions through time (millions)",
      ylab="change in incremental conversion rate \n (% of conversion rate in initial period)")
dev.off()

#=====================
#=== begin to run policies

d <- d.combo.public
Mhobs.cur <- Mhobs.combo

nworlds <- 100
hinitial <- 1
K <- 4
J <- nrow(d)/K
nparams <- 12
#nparams <- 12 + nrow(d) #heterogeneous model

nworlds <- 5

# Establishing truth for data-generating process
linpred.true <- log(mutruevec/(1-mutruevec))
mutruemat <- matrix( mutruevec , nrow=nrow(d), ncol=hend)
mutruevec <- mutruemat[,1]

## --- PERFECT INFORMATION POLICY , best (oracle policy)
mutrueJbyK <- matrix(mutruevec, ncol=K, byrow=T)
winnertruename <- apply(mutrueJbyK, 1, which.max)
winnertrueindmat <- matrix(0,J,K)
winnertrueindmat[ cbind(1:J , apply(mutrueJbyK, 1, which.max)) ] <- 1
winnertrueindvec <- c(t(winnertrueindmat))
mutrueJbyKrange <- t( apply(mutrueJbyK, 1, range) )
mutrueJbyKsorted <- t( apply(mutrueJbyK, 1, sort) )
mutrueJbyKperchng <- mutrueJbyKsorted/mutrueJbyKsorted[,1]

ytotaldist.best <- rep(0,nworlds)
for (i in 1:nworlds){
  ytotaldist.best[i] <- sum( rbinom(J*hend, Mhobs.cur[winnertrueindvec==1,], mutruevec[winnertrueindvec==1]) )
}
ptotaldist.best <- ytotaldist.best/sum(Mhobs.cur[winnertrueindvec==1,])

##### --- BALANCED POLICY ------

wmatbalanced <- matrix(1/K, numobs, hend)
ytotaldist.balanced <- rep(0,nworlds)
ptotaldist.balanced <- rep(0,nworlds)
for (i in 1:nworlds){
  ytotaldist.balanced[i] <- sum( rbinom(numobs*hend, round(Mhobs.cur*wmatbalanced), mutruemat) )
  ptotaldist.balanced[i] <- ytotaldist.balanced[i]/sum( round(Mhobs.cur*wmatbalanced) )
}

reslist.balanced = list(NULL);
reslist.balanced$ytotaldist <- ytotaldist.balanced
reslist.balanced$ptotaldist <- ptotaldist.balanced
reslist.balanced$wstardist <- array(wmatbalanced, dim=c(numobs,hend,nworlds) )

###### ---- EXPLORE THAN EXPLOIT POLICIES ------

### Use policy.exp.then.exp functions
source(file=paste(path,"Greedy_variant_policies_201505.r",sep=""))

MODEL=F
nsamp <- 100
n.iters <- 100
niters <- 100
n.worlds <- 50 #n.worlds <- 5
nworlds <- 50 #nworlds <- 5

res.ETE.g04.t1 <- policy.exp.then.exp(hinit=1,policy.greedy.pooled4,Mhobs.cur)
res.ETE.g04.t2 <- policy.exp.then.exp(hinit=2,policy.greedy.pooled4,Mhobs.cur)
res.ETE.g04.t3 <- policy.exp.then.exp(hinit=3,policy.greedy.pooled4,Mhobs.cur)
res.ETE.g04.t4 <- policy.exp.then.exp(hinit=4,policy.greedy.pooled4,Mhobs.cur)
res.ETE.g04.t5 <- policy.exp.then.exp(hinit=5,policy.greedy.pooled4,Mhobs.cur)
res.ETE.g04.t6 <- policy.exp.then.exp(hinit=6,policy.greedy.pooled4,Mhobs.cur)

res.ETE.g12.t1 <- policy.exp.then.exp(hinit=1,policy.greedy.pooled12,Mhobs.cur)
res.ETE.g12.t2 <- policy.exp.then.exp(hinit=2,policy.greedy.pooled12,Mhobs.cur)
res.ETE.g12.t3 <- policy.exp.then.exp(hinit=3,policy.greedy.pooled12,Mhobs.cur)
res.ETE.g12.t4 <- policy.exp.then.exp(hinit=4,policy.greedy.pooled12,Mhobs.cur)
res.ETE.g12.t5 <- policy.exp.then.exp(hinit=5,policy.greedy.pooled12,Mhobs.cur)
res.ETE.g12.t6 <- policy.exp.then.exp(hinit=6,policy.greedy.pooled12,Mhobs.cur)

res.ETE.unpooled.t1 <- policy.exp.then.exp(hinit=1,policy.greedy.unpooled,Mhobs.cur)
res.ETE.unpooled.t2 <- policy.exp.then.exp(hinit=2,policy.greedy.unpooled,Mhobs.cur)
res.ETE.unpooled.t3 <- policy.exp.then.exp(hinit=3,policy.greedy.unpooled,Mhobs.cur)
res.ETE.unpooled.t4 <- policy.exp.then.exp(hinit=4,policy.greedy.unpooled,Mhobs.cur)
res.ETE.unpooled.t5 <- policy.exp.then.exp(hinit=5,policy.greedy.unpooled,Mhobs.cur)
res.ETE.unpooled.t6 <- policy.exp.then.exp(hinit=6,policy.greedy.unpooled,Mhobs.cur)

ptotaldist.ETE.g04.t1 <- res.ETE.g04.t1$ptotal
ptotaldist.ETE.g04.t2 <- res.ETE.g04.t2$ptotal
ptotaldist.ETE.g04.t3 <- res.ETE.g04.t3$ptotal
ptotaldist.ETE.g04.t4 <- res.ETE.g04.t4$ptotal
ptotaldist.ETE.g04.t5 <- res.ETE.g04.t5$ptotal
ptotaldist.ETE.g04.t6 <- res.ETE.g04.t6$ptotal

ptotaldist.ETE.g12.t1 <- res.ETE.g12.t1$ptotal
ptotaldist.ETE.g12.t2 <- res.ETE.g12.t2$ptotal
ptotaldist.ETE.g12.t3 <- res.ETE.g12.t3$ptotal
ptotaldist.ETE.g12.t4 <- res.ETE.g12.t4$ptotal
ptotaldist.ETE.g12.t5 <- res.ETE.g12.t5$ptotal
ptotaldist.ETE.g12.t6 <- res.ETE.g12.t6$ptotal

ptotaldist.ETE.unpooled.t1 <- res.ETE.unpooled.t1$ptotal
ptotaldist.ETE.unpooled.t2 <- res.ETE.unpooled.t2$ptotal
ptotaldist.ETE.unpooled.t3 <- res.ETE.unpooled.t3$ptotal
ptotaldist.ETE.unpooled.t4 <- res.ETE.unpooled.t4$ptotal
ptotaldist.ETE.unpooled.t5 <- res.ETE.unpooled.t5$ptotal
ptotaldist.ETE.unpooled.t6 <- res.ETE.unpooled.t6$ptotal

res.ETE.totals <- data.frame(
  explore= rep(rep(1:6,each=nworlds),2),
  greedy= rep( c("g04","g12"),each=6*nworlds ),
  ptotal=c(
res.ETE.g04.t1$ptotal,
res.ETE.g04.t2$ptotal,
res.ETE.g04.t3$ptotal,
res.ETE.g04.t4$ptotal,
res.ETE.g04.t5$ptotal,
res.ETE.g04.t6$ptotal,
res.ETE.g12.t1$ptotal,
res.ETE.g12.t2$ptotal,
res.ETE.g12.t3$ptotal,
res.ETE.g12.t4$ptotal,
res.ETE.g12.t5$ptotal,
res.ETE.g12.t6$ptotal)
)

rbind(
c(summary(res.ETE.g04.t1$ptotal),sd(res.ETE.g04.t1$ptotal)),
c(summary(res.ETE.g04.t2$ptotal),sd(res.ETE.g04.t2$ptotal)),
c(summary(res.ETE.g04.t3$ptotal),sd(res.ETE.g04.t3$ptotal)),
c(summary(res.ETE.g04.t4$ptotal),sd(res.ETE.g04.t4$ptotal)),
c(summary(res.ETE.g04.t5$ptotal),sd(res.ETE.g04.t5$ptotal)),
c(summary(res.ETE.g04.t6$ptotal),sd(res.ETE.g04.t6$ptotal)),
c(summary(res.ETE.g12.t1$ptotal),sd(res.ETE.g12.t1$ptotal)),
c(summary(res.ETE.g12.t2$ptotal),sd(res.ETE.g12.t2$ptotal)),
c(summary(res.ETE.g12.t3$ptotal),sd(res.ETE.g12.t3$ptotal)),
c(summary(res.ETE.g12.t4$ptotal),sd(res.ETE.g12.t4$ptotal)),
c(summary(res.ETE.g12.t5$ptotal),sd(res.ETE.g12.t5$ptotal)),
c(summary(res.ETE.g12.t6$ptotal),sd(res.ETE.g12.t1$ptotal))
)

#densities
windows()
xlimcur <- range(res.ETE.totals$ptotal)
plot( density(res.ETE.g04.t1$ptotal), type="n",
     xlim=xlimcur, frame.plot=F,axes=F,ylab="",xlab="",main="")
axis(1)
lines(density(res.ETE.g04.t1$ptotal),col=1)
lines(density(res.ETE.g04.t2$ptotal),col=2)
lines(density(res.ETE.g04.t3$ptotal),col=3)
lines(density(res.ETE.g04.t4$ptotal),col=4)
lines(density(res.ETE.g04.t5$ptotal),col=5)
lines(density(res.ETE.g04.t6$ptotal),col=6)

lines(density(res.ETE.g12.t1$ptotal),col=1)
lines(density(res.ETE.g12.t2$ptotal),col=2)
lines(density(res.ETE.g12.t3$ptotal),col=3)
lines(density(res.ETE.g12.t4$ptotal),col=4)
lines(density(res.ETE.g12.t5$ptotal),col=5)
lines(density(res.ETE.g12.t6$ptotal),col=6)

lines(density(ptotaldist.balanced),col=1,lty=2)

#time series
res.ETE.overtime <- data.frame(
  explore= rep(rep(1:6,each=nworlds*hend),2),
  greedy = rep( c("g04","g12"),each=6*nworlds*hend ),
  period = rep(1:hend,6*2*nworlds),
rate = c(
res.ETE.g04.t1$pstarovertimecum,
res.ETE.g04.t2$pstarovertimecum,
res.ETE.g04.t3$pstarovertimecum,
res.ETE.g04.t4$pstarovertimecum,
res.ETE.g04.t5$pstarovertimecum,
res.ETE.g04.t6$pstarovertimecum,
res.ETE.g12.t1$pstarovertimecum,
res.ETE.g12.t2$pstarovertimecum,
res.ETE.g12.t3$pstarovertimecum,
res.ETE.g12.t4$pstarovertimecum,
res.ETE.g12.t5$pstarovertimecum,
res.ETE.g12.t6$pstarovertimecum)
)

windows()
xscale <- cumsum(maggovertime.combo)/1e6
ylimcur <-  1e6*quantile(res.ETE.overtime$rate,c(0,1))
plot(xscale, 1e6*apply(res.ETE.g04.t1$pstarovertimecum,2,mean), type="n",
      ylim=ylimcur, frame.plot=F,axes=F,ylab="",xlab="",main="")
axis(1,xscale, at=seq(0,900,by=100), labels=seq(0,900,by=100) )
axis(2)
lines(xscale, 1e6*apply(res.ETE.g04.t1$pstarovertimecum,2,mean), col=1)
lines(xscale, 1e6*apply(res.ETE.g04.t2$pstarovertimecum,2,mean), col=2)
lines(xscale, 1e6*apply(res.ETE.g04.t3$pstarovertimecum,2,mean), col=3)
lines(xscale, 1e6*apply(res.ETE.g04.t4$pstarovertimecum,2,mean), col=4)
lines(xscale, 1e6*apply(res.ETE.g04.t5$pstarovertimecum,2,mean), col=5)
lines(xscale, 1e6*apply(res.ETE.g04.t6$pstarovertimecum,2,mean), col=6)

lines(xscale, 1e6*apply(res.ETE.g12.t1$pstarovertimecum,2,mean), col=1,lty=2)
lines(xscale, 1e6*apply(res.ETE.g12.t2$pstarovertimecum,2,mean), col=2,lty=2)
lines(xscale, 1e6*apply(res.ETE.g12.t3$pstarovertimecum,2,mean), col=3,lty=2)
lines(xscale, 1e6*apply(res.ETE.g12.t4$pstarovertimecum,2,mean), col=4,lty=2)
lines(xscale, 1e6*apply(res.ETE.g12.t5$pstarovertimecum,2,mean), col=5,lty=2)
lines(xscale, 1e6*apply(res.ETE.g12.t6$pstarovertimecum,2,mean), col=6,lty=2)

lines(xscale, 1e6*apply(res.ETE.g12.t1$pstarovertimecum,2,quantile,.05), col=1,lty=2)
lines(xscale, 1e6*apply(res.ETE.g12.t2$pstarovertimecum,2,quantile,.05), col=2,lty=2)
lines(xscale, 1e6*apply(res.ETE.g12.t3$pstarovertimecum,2,quantile,.05), col=3,lty=2)
lines(xscale, 1e6*apply(res.ETE.g12.t4$pstarovertimecum,2,quantile,.05), col=4,lty=2)
lines(xscale, 1e6*apply(res.ETE.g12.t5$pstarovertimecum,2,quantile,.05), col=5,lty=2)
lines(xscale, 1e6*apply(res.ETE.g12.t6$pstarovertimecum,2,quantile,.05), col=6,lty=2)

lines(xscale, 1e6*apply(res.ETE.g12.t1$pstarovertimecum,2,quantile,.95), col=1,lty=2)
lines(xscale, 1e6*apply(res.ETE.g12.t2$pstarovertimecum,2,quantile,.95), col=2,lty=2)
lines(xscale, 1e6*apply(res.ETE.g12.t3$pstarovertimecum,2,quantile,.95), col=3,lty=2)
lines(xscale, 1e6*apply(res.ETE.g12.t4$pstarovertimecum,2,quantile,.95), col=4,lty=2)
lines(xscale, 1e6*apply(res.ETE.g12.t5$pstarovertimecum,2,quantile,.95), col=5,lty=2)
lines(xscale, 1e6*apply(res.ETE.g12.t6$pstarovertimecum,2,quantile,.95), col=6,lty=2)

lines(xscale, 1e6*pstarovertimecum.mean, lty=2, lwd=2)    #fully balanced design

title(xlab="impressions through time", ylab="cumulative conversion rate (per million)")

### ------ GREEDY VARIANTS AND THOMPSON SAMPLING POLICIES -----

source(file=paste(path,"policy2res.r",sep=""))
source(file=paste(path,"policy2res_fast.r",sep=""))

source(file=paste(path,"Greedy_variant_policies_201505.r",sep=""))
source(file=paste(path,"RPM_BB_20130221.r",sep=""))

# depends on mutruemat

MODEL=F  # MODEL = TRUE if the policy2res function calls a GLM-based policy

#small , fast
nsamp <- 100
n.iters <- 100
niters <- 100
n.worlds <- 50
nworlds <- 50

#regular
nsamp <- 1000
n.iters <- 1000
niters <- 1000
n.worlds <- 100
nworlds <- 100

# run!  from policies to results
reslist.greedy.pooled4 <- policy2res(policy.greedy.pooled4)
reslist.greedy.pooled12 <- policy2res(policy.greedy.pooled12)
reslist.greedy.unpooled <- policy2res(policy.greedy.unpooled)
reslist.softmax.unpooled <- policy2res(policy.softmax.unpooled)

reslist.gittins.pooled12 <- policy2res(policy.gittins.pooled12)
reslist.gittins.unpooled <- policy2res(policy.gittins.unpooled)

reslist.UCB1.unpooled <- policy2res(policy.UCB1.unpooled)
reslist.UCBtuned.unpooled <- policy2res(policy.UCBtuned.unpooled)

reslist.epsgreedy10.pooled12 <- policy2res(policy.epsgreedy.pooled12,eps=.10)
reslist.epsgreedy20.pooled12 <- policy2res(policy.epsgreedy.pooled12,eps=.20)
reslist.epsgreedy10.unpooled <- policy2res(policy.epsgreedy.unpooled,eps=.10)
reslist.epsgreedy20.unpooled <- policy2res(policy.epsgreedy.unpooled,eps=.20)

reslist.RPM.BB4 <- policy2res(policy.RPM.BB4)
reslist.RPM.BB12 <- policy2res(policy.RPM.BB12)
reslist.RPM.BB4.unpooled <- policy2res(policy.RPM.BB4.unpooled)

# save ptotaldist
ptotaldist.softmax.unpooled <- reslist.softmax.unpooled$ptotaldist
ptotaldist.greedy.unpooled <- reslist.greedy.unpooled$ptotaldist
ptotaldist.greedy.pooled12 <- reslist.greedy.pooled12$ptotaldist
ptotaldist.greedy.pooled4 <- reslist.greedy.pooled4$ptotaldist

ptotaldist.gittins.pooled12 <- reslist.gittins.pooled12$ptotaldist
ptotaldist.gittins.unpooled <- reslist.gittins.unpooled$ptotaldist

ptotaldist.UCB1.unpooled <- reslist.UCB1.unpooled$ptotaldist
ptotaldist.UCBtuned.unpooled <- reslist.UCBtuned.unpooled$ptotaldist

ptotaldist.epsgreedy10.unpooled <- reslist.epsgreedy10.unpooled$ptotaldist
ptotaldist.epsgreedy20.unpooled <- reslist.epsgreedy20.unpooled$ptotaldist
ptotaldist.epsgreedy10.pooled12 <- reslist.epsgreedy10.pooled12$ptotaldist
ptotaldist.epsgreedy20.pooled12 <- reslist.epsgreedy20.pooled12$ptotaldist

ptotaldist.RPM.BB4 <- reslist.RPM.BB4$ptotaldist
ptotaldist.RPM.BB12 <- reslist.RPM.BB12$ptotaldist
ptotaldist.RPM.BB4.unpooled <- reslist.RPM.BB4.unpooled$ptotaldist

source(file=paste(path,"RPM_Logit_Homog_20130218.r",sep=""))
source(file=paste(path,"RPM_Logit_LC2_flexmix_20130219.r",sep=""))
source(file=paste(path,"RPM_Logit_Heter_LMER_20130218.r",sep=""))

## TS-GLM
startruntime <- Sys.time()
#reslist.RPM.Logit.Homog <- policy2res( policy.RPM.Logit.Homog, MODEL=T, nparams=12)
reslist.RPM.Logit.Homog <- policy2res_fast( policy.RPM.Logit.Homog, MODEL=F, nparams=12, save_curworld=T)
endruntime <- Sys.time()
endruntime - startruntime

# TS-LCGLM
startruntime <- Sys.time()
reslist.RPM.Logit.LC2 <- policy2res_fast( policy.RPM.Logit.LC2, save_curworld=T, MODEL=F, K=12) #watchout for K=12 ?!
endruntime <- Sys.time()
endruntime - startruntime
ptotaldist.RPM.Logit.LC2 <- reslist.RPM.Logit.LC2$ptotaldist
##

# TS-HGLM
nworlds <- 50
startruntime <- Sys.time()
reslist.RPM.Logit.Heter <- policy2res_fast( policy.RPM.Logit.Heter, MODEL=F, save_curworld=T )
endruntime <- Sys.time()
endruntime - startruntime
# ptotaldist.RPM.Logit.Heter <- read.csv(file=paste(path,"pstartotal_append_TSHGLM_20150529.csv",sep="") ,header=F)[,-1]

# run once
restemphomog <- policy.RPM.Logit.Homog(m=mtstar[,hinitial],y=ytstar[,hinitial],nparams=12,MODEL=T)
restempheter <- policy.RPM.Logit.Heter(m=mtstar[,hinitial],y=ytstar[,hinitial])
restempLC2 <- policy.RPM.Logit.LC2(m=mtstar[,hinitial],y=ytstar[,hinitial],K=12) #watchout for K=12 ?!

# save ptotaldist
ptotaldist.RPM.Logit.Homog <- reslist.RPM.Logit.Homog$ptotaldist
ptotaldist.RPM.Logit.LC2 <- reslist.RPM.Logit.LC2$ptotaldist
ptotaldist.RPM.Logit.Heter <- reslist.RPM.Logit.Heter$ptotaldist

#----------------
summaryfn <- function(x)  c( mean=mean(x), sd=sd(x), quantile(x,c(.025,.975)) )
# TABLE summary of ptotal
ptotalsummaryfn <- function(x)  c( mean=mean(x), sd=sd(x), quantile(x,c(.025,.975)) )

ptotaldist.all <- rbind(
ptotaldist.balanced,
ptotaldist.ETE.g04.t1,
ptotaldist.ETE.g04.t2,
ptotaldist.ETE.g04.t3,
ptotaldist.ETE.g04.t4,
ptotaldist.ETE.g04.t5,
ptotaldist.ETE.g04.t6,
ptotaldist.ETE.g12.t1,
ptotaldist.ETE.g12.t2,
ptotaldist.ETE.g12.t3,
ptotaldist.ETE.g12.t4,
ptotaldist.ETE.g12.t5,
ptotaldist.ETE.g12.t6,
ptotaldist.ETE.unpooled.t1,
ptotaldist.ETE.unpooled.t2,
ptotaldist.ETE.unpooled.t3,
ptotaldist.ETE.unpooled.t4,
ptotaldist.ETE.unpooled.t5,
ptotaldist.ETE.unpooled.t6,
ptotaldist.epsgreedy10.pooled12,
ptotaldist.epsgreedy20.pooled12,
ptotaldist.epsgreedy10.unpooled,
ptotaldist.epsgreedy20.unpooled,
ptotaldist.greedy.pooled4,
ptotaldist.greedy.pooled12,
ptotaldist.softmax.unpooled,
ptotaldist.greedy.unpooled,
ptotaldist.gittins.pooled12,
ptotaldist.gittins.unpooled,
ptotaldist.UCB1.unpooled,
ptotaldist.UCBtuned.unpooled,
ptotaldist.RPM.BB4,
ptotaldist.RPM.BB12,
ptotaldist.RPM.BB4.unpooled,

ptotaldist.RPM.Logit.Homog,
# ptotaldist.RPM.Logit.LC2,
# ptotaldist.RPM.Logit.Heter,

ptotaldist.best)

ptotaldist.summary <- t( apply(ptotaldist.all,1,ptotalsummaryfn) )

##========= plots and figures for paper  ==========##

policynames <- c("ETE-g04-t1","ETE-g04-t2","ETE-g04-t3",
                 "ETE-g04-t4","ETE-g04-t5","ETE-g04-t6",
                 "ETE-g12-t1","ETE-g12-t2","ETE-g12-t3",
                 "ETE-g12-t4","ETE-g12-t5","ETE-g12-t6",
                 "balanced","greedy-pooled4","greedy-pooled12",
                 "softmax-unpooled","greedy-unpooled",
                 "epsgreedy10-pooled12","epsgreedy20-pooled12",
                 "epsgreedy10-unpooled","epsgreedy20-unpooled",
                 "RPM-binomial4","RPM-binomial12","RPM-binomial4-unpooled",
                 "RPM-logit-homog-MLE","RPM-logit-LC2-EM","RPM-logit-heter-REML",
                 "best")
rownames(ptotaldist.summary) <- policynames

1e6*ptotaldist.summary
#----------------

ptotaldf <- data.frame(
  policy = as.factor( rep(c("balanced","greedy-pooled4","greedy-pooled12",
                  "softmax-unpooled","greedy-unpooled",
                  "epsgreedy10-pooled12","epsgreedy20-pooled12",
                  "epsgreedy10-unpooled","epsgreedy20-unpooled",
                  "RPM-binomial4","RPM-binomial12","RPM-binomial4-unpooled",
                  "RPM-logit-homog-MLE","RPM-logit-LC2-EM","RPM-logit-heter-REML"
                  ),each=nworlds) ),
  rate = 1e6*c(ptotaldist.balanced,
            ptotaldist.greedy.pooled4,
            ptotaldist.greedy.pooled12,
            ptotaldist.softmax.unpooled,
            ptotaldist.greedy.unpooled,
            ptotaldist.epsgreedy10.pooled12,
            ptotaldist.epsgreedy20.pooled12,
            ptotaldist.epsgreedy10.unpooled,
            ptotaldist.epsgreedy20.unpooled,
            ptotaldist.RPM.BB4,
            ptotaldist.RPM.BB12,
            ptotaldist.RPM.BB4.unpooled,
            ptotaldist.RPM.Logit.Homog,
            ptotaldist.RPM.Logit.LC2,
            ptotaldist.RPM.Logit.Heter
          )
)

ptotaldf$RPM <- rep(F,nrow(ptotaldf))
ptotaldf$RPM[ptotaldf$policy %in%
             c("balanced","RPM-logit-homog-MLE","RPM-logit-LC2-EM","RPM-logit-heter-REML")
             ] <- T

####====== REVISION ON 2015-05-29 ============##
####====== REVISION ON 2013-11-27 ============##
####  - Separate tables and figures for TS only, Heuristics only, TestRollout Only

ptdf.policynames.TSonly <- c(
    "Balanced",
    "TS-BB-pooled",
    "TS-GLM-pooled",
    "TS-BB-unpooled",
    "TS-LCGLM",
    "TS-HGLM",
    "Perfect Info"
    )
nworlds <- 100
ptdf.TSonly <- data.frame(
  policy = factor( rep(ptdf.policynames.TSonly, each=nworlds),
    levels=ptdf.policynames.TSonly ),
  rate =1e6*c(
    ptotaldist.balanced,
    ptotaldist.RPM.BB12,
    ptotaldist.RPM.Logit.Homog,
    ptotaldist.RPM.BB4.unpooled,
    ptotaldist.RPM.Logit.LC2,
    ptotaldist.RPM.Logit.Heter,
    ptotaldist.best
    )
)
pdf( file=paste(path,"ptotal_boxplot_TSonly.pdf",sep="") )
gpt <- ggplot(ptdf.TSonly,aes(y=rate, x=policy))
gpt + geom_boxplot() +
  labs(y="\n conversion rate (per million)",x="policy") +
  theme_bw()+
  coord_flip()
dev.off()

#===========

#==========================================
ptdf.policynames.MABlit <- c(
    "Balanced",
    "Gittins-pooled",
    "Gittins-unpooled",
    "UCB1-unpooled",
    "UCBtuned-unpooled",
    "TS-BB-unpooled",
    "TS-HGLM",
    "Perfect Info"
    )
nworlds <- 100
ptdf.MABlit <- data.frame(
  policy = factor( rep(ptdf.policynames.MABlit, each=nworlds),
    levels=ptdf.policynames.MABlit ),
  rate =1e6*c(
    ptotaldist.balanced,
    ptotaldist.gittins.pooled12,
    ptotaldist.gittins.unpooled,
    ptotaldist.UCB1.unpooled,
    ptotaldist.UCBtuned.unpooled,
    ptotaldist.RPM.BB4.unpooled,
    ptotaldist.RPM.Logit.Heter,
    ptotaldist.best
    )
)
pdf( file=paste(path,"ptotal_boxplot_MABlit.pdf",sep="") )
gpt <- ggplot(ptdf.MABlit,aes(y=rate, x=policy))
gpt + geom_boxplot() +
  labs(y="\n conversion rate (per million)",x="policy") +
  theme_bw()+
  coord_flip()
dev.off()

#==========================================

ptdf.policynames.HeurOnly <- c(
    "Balanced",
    "epsgreedy10-pooled",
    "epsgreedy20-pooled",
    "epsgreedy10-unpooled",
    "epsgreedy20-unpooled",
    "greedy-pooled",
    "greedy-unpooled",
    "TS-HGLM",
    "Perfect Info"
    )
nworlds <- 100
ptdf.HeurOnly <- data.frame(
  policy = factor( rep(ptdf.policynames.HeurOnly, each=nworlds),
    levels=ptdf.policynames.HeurOnly ),
  rate =1e6*c(
    ptotaldist.balanced,
    ptotaldist.epsgreedy10.pooled12,
    ptotaldist.epsgreedy20.pooled12,
    ptotaldist.epsgreedy10.unpooled,
    ptotaldist.epsgreedy20.unpooled,
    ptotaldist.greedy.pooled12,
    ptotaldist.greedy.unpooled,
    ptotaldist.RPM.Logit.Heter,
    ptotaldist.best
    )
)
pdf( file=paste(path,"ptotal_boxplot_HeurOnly.pdf",sep="") )
gpt <- ggplot(ptdf.HeurOnly,aes(y=rate, x=policy))
gpt + geom_boxplot() +
  labs(y="\n conversion rate (per million)",x="policy") +
  theme_bw()+
  coord_flip()
dev.off()

#============================

ptdf.policynames.TRonly <- c(
    "Balanced",
    "testrollout-t1",
    "testrollout-t2",
    "testrollout-t3",
    "testrollout-t4",
    "testrollout-t5",
    "testrollout-t6",
    "testrollout-unpooled-t1",
    "testrollout-unpooled-t2",
    "testrollout-unpooled-t3",
    "testrollout-unpooled-t4",
    "testrollout-unpooled-t5",
    "testrollout-unpooled-t6",
    "Perfect Info"
    )
nworlds <- 100
ptdf.TRonly <- data.frame(
  policy = factor( rep(ptdf.policynames.TRonly, each=nworlds),
    levels=ptdf.policynames.TRonly ),
  rate =1e6*c(
    ptotaldist.balanced,
    ptotaldist.ETE.g12.t1,
    ptotaldist.ETE.g12.t2,
    ptotaldist.ETE.g12.t3,
    ptotaldist.ETE.g12.t4,
    ptotaldist.ETE.g12.t5,
    ptotaldist.ETE.g12.t6,
    ptotaldist.ETE.unpooled.t1,
    ptotaldist.ETE.unpooled.t2,
    ptotaldist.ETE.unpooled.t3,
    ptotaldist.ETE.unpooled.t4,
    ptotaldist.ETE.unpooled.t5,
    ptotaldist.ETE.unpooled.t6,
    ptotaldist.best
    )
)
pdf( file=paste(path,"ptotal_boxplot_TRonly.pdf",sep="") )
gpt <- ggplot(ptdf.TRonly,aes(y=rate, x=policy))
gpt + geom_boxplot() +
  labs(y="\n conversion rate (per million)",x="policy") +
  theme_bw()+
  coord_flip()
dev.off()

#=============================

ptdf.policynames.Unpooled <- c(
    "Balanced",
    "testrollout-unpooled-t2",
    "epsgreedy10-unpooled",
    "epsgreedy20-unpooled",
    "greedy-unpooled",
    "Gittins-unpooled",
    "UCBtuned-unpooled",
    "TS-BB-unpooled",
    "Perfect Info"
    )
nworlds <- 100
ptdf.Unpooled <- data.frame(
  policy = factor( rep(ptdf.policynames.Unpooled, each=nworlds),
    levels=ptdf.policynames.Unpooled ),
  rate =1e6*c(
    ptotaldist.balanced,
    ptotaldist.ETE.unpooled.t2,
    ptotaldist.epsgreedy10.unpooled,
    ptotaldist.epsgreedy20.unpooled,
    ptotaldist.greedy.unpooled,
    ptotaldist.gittins.unpooled,
    ptotaldist.UCBtuned.unpooled,
    ptotaldist.RPM.BB4.unpooled,
    ptotaldist.best
    )
)
pdf( file=paste(path,"ptotal_boxplot_Unpooled.pdf",sep="") )
gpt <- ggplot(ptdf.Unpooled,aes(y=rate, x=policy))
gpt + geom_boxplot() +
  labs(y="\n conversion rate (per million)",x="policy") +
  theme_bw()+
  coord_flip()
dev.off()

ptprobwinfn <- function(x) {
  pnames <- levels(x$policy)
  npolicies <- length(pnames)
  nworlds <- length(x$policy)/npolicies
  prwin <- matrix(NA,npolicies,npolicies)
  rownames(prwin) <- pnames
    for (i in 1:npolicies){
      for (j in 1:npolicies){
        if(i>j){
        idxpi <- which(x$policy==pnames[i])
        idxpj <- which(x$policy==pnames[j])
        prwin[i,j] <- sum(x$rate[idxpi] >= x$rate[idxpj])/nworlds
        }
      }
    }
  return(prwin)
}

ptprobwinfn(ptdf.TSonly)
ptprobwinfn(ptdf.MABlit)
ptprobwinfn(ptdf.Unpooled)
ptprobwinfn(ptdf.HeurOnly)
ptprobwinfn(ptdf.TRonly)

# ========= AFTER OPTIMIZING FOR CLICKS, LOOK AT CONVERSIONS ======

reslist_ctropt_cvrout <- function(reslist_ctropt){
  # function takes results optimized for CTR
  #    and looks at the implied CVR results
  # input
  #   reslist_ctropt (based on optimizing clicks)
  #   Mhobs.cur
  #   mutruemat (conversion rates)

  # grab wstardist from optimizing click through
  wstardist <- reslist_ctropt$wstardist
  numobs <- dim(wstardist)[1]
  hend <- dim(wstardist)[2]
  nworlds <- dim(wstardist)[3]

  ytotaldist <- rep(0,nworlds)
  ptotaldist <- rep(0,nworlds)
  for (i in 1:nworlds){
    wstar <- wstardist[,,i]
    ytotaldist[i] <- sum( rbinom(numobs*hend, round(Mhobs.cur*wstar), mutruemat) )
    ptotaldist[i] <- ytotaldist[i]/sum( round(Mhobs.cur*wstar) )
  }

  reslist_cvrout <- list(ptotaldist=ptotaldist)
  return(reslist_cvrout)
} #end function

# ex
ptotaldist.balanced.cvrout <- reslist_ctropt_cvrout(reslist.balanced)$ptotaldist
# ptotaldist.ETE.g12.t1.cvrout <- reslist_ctropt_cvrout(res.ETE.g12.t1)$ptotaldist
# ptotaldist.ETE.g12.t2.cvrout <- reslist_ctropt_cvrout(res.ETE.g12.t2)$ptotaldist
# ptotaldist.ETE.g12.t3.cvrout <- reslist_ctropt_cvrout(res.ETE.g12.t3)$ptotaldist
# ptotaldist.ETE.g12.t4.cvrout <- reslist_ctropt_cvrout(res.ETE.g12.t4)$ptotaldist
# ptotaldist.ETE.g12.t5.cvrout <- reslist_ctropt_cvrout(res.ETE.g12.t5)$ptotaldist
# ptotaldist.ETE.g12.t6.cvrout <- reslist_ctropt_cvrout(res.ETE.g12.t6)$ptotaldist
# ptotaldist.ETE.unpooled.t1.cvrout <- reslist_ctropt_cvrout(res.ETE.unpooled.t1)$ptotaldist
# ptotaldist.ETE.unpooled.t2.cvrout <- reslist_ctropt_cvrout(res.ETE.unpooled.t2)$ptotaldist
# ptotaldist.ETE.unpooled.t3.cvrout <- reslist_ctropt_cvrout(res.ETE.unpooled.t3)$ptotaldist
# ptotaldist.ETE.unpooled.t4.cvrout <- reslist_ctropt_cvrout(res.ETE.unpooled.t4)$ptotaldist
# ptotaldist.ETE.unpooled.t5.cvrout <- reslist_ctropt_cvrout(res.ETE.unpooled.t5)$ptotaldist
# ptotaldist.ETE.unpooled.t6.cvrout <- reslist_ctropt_cvrout(res.ETE.unpooled.t6)$ptotaldist
ptotaldist.epsgreedy10.pooled12.cvrout <- reslist_ctropt_cvrout(reslist.epsgreedy10.pooled12)$ptotaldist
ptotaldist.epsgreedy20.pooled12.cvrout <- reslist_ctropt_cvrout(reslist.epsgreedy20.pooled12)$ptotaldist
ptotaldist.epsgreedy10.unpooled.cvrout <- reslist_ctropt_cvrout(reslist.epsgreedy10.unpooled)$ptotaldist
ptotaldist.epsgreedy20.unpooled.cvrout <- reslist_ctropt_cvrout(reslist.epsgreedy20.unpooled)$ptotaldist
ptotaldist.greedy.pooled4.cvrout <- reslist_ctropt_cvrout(reslist.greedy.pooled4)$ptotaldist
ptotaldist.greedy.pooled12.cvrout <- reslist_ctropt_cvrout(reslist.greedy.pooled12)$ptotaldist
ptotaldist.softmax.unpooled.cvrout <- reslist_ctropt_cvrout(reslist.softmax.unpooled)$ptotaldist
ptotaldist.greedy.unpooled.cvrout <- reslist_ctropt_cvrout(reslist.greedy.unpooled)$ptotaldist
ptotaldist.gittins.pooled12.cvrout <- reslist_ctropt_cvrout(reslist.gittins.pooled12)$ptotaldist
ptotaldist.gittins.unpooled.cvrout <- reslist_ctropt_cvrout(reslist.gittins.unpooled)$ptotaldist
ptotaldist.UCB1.unpooled.cvrout <- reslist_ctropt_cvrout(reslist.UCB1.unpooled)$ptotaldist
ptotaldist.UCBtuned.unpooled.cvrout <- reslist_ctropt_cvrout(reslist.UCBtuned.unpooled)$ptotaldist
ptotaldist.RPM.BB4.cvrout <- reslist_ctropt_cvrout(reslist.RPM.BB4)$ptotaldist
ptotaldist.RPM.BB12.cvrout <- reslist_ctropt_cvrout(reslist.RPM.BB12)$ptotaldist
ptotaldist.RPM.BB4.unpooled.cvrout <- reslist_ctropt_cvrout(reslist.RPM.BB4.unpooled)$ptotaldist
ptotaldist.RPM.Logit.Homog.cvrout <- reslist_ctropt_cvrout(reslist.RPM.Logit.Homog)$ptotaldist
# ptotaldist.RPM.Logit.LC2,
# ptotaldist.RPM.Logit.Heter,

ptotaldist.cvrout <- rbind(
ptotaldist.balanced.cvrout,
# ptotaldist.ETE.g12.t1.cvrout,
# ptotaldist.ETE.g12.t2.cvrout,
# ptotaldist.ETE.g12.t3.cvrout,
# ptotaldist.ETE.g12.t4.cvrout,
# ptotaldist.ETE.g12.t5.cvrout,
# ptotaldist.ETE.g12.t6.cvrout,
# ptotaldist.ETE.unpooled.t1.cvrout,
# ptotaldist.ETE.unpooled.t2.cvrout,
# ptotaldist.ETE.unpooled.t3.cvrout,
# ptotaldist.ETE.unpooled.t4.cvrout,
# ptotaldist.ETE.unpooled.t5.cvrout,
# ptotaldist.ETE.unpooled.t6.cvrout,
ptotaldist.epsgreedy10.pooled12.cvrout,
ptotaldist.epsgreedy20.pooled12.cvrout,
ptotaldist.epsgreedy10.unpooled.cvrout,
ptotaldist.epsgreedy20.unpooled.cvrout,
ptotaldist.greedy.pooled4.cvrout,
ptotaldist.greedy.pooled12.cvrout,
ptotaldist.softmax.unpooled.cvrout,
ptotaldist.greedy.unpooled.cvrout,
ptotaldist.gittins.pooled12.cvrout,
ptotaldist.gittins.unpooled.cvrout,
ptotaldist.UCB1.unpooled.cvrout,
ptotaldist.UCBtuned.unpooled.cvrout,
ptotaldist.RPM.BB4.cvrout,
ptotaldist.RPM.BB12.cvrout,
ptotaldist.RPM.BB4.unpooled.cvrout,
ptotaldist.RPM.Logit.Homog.cvrout)
# ptotaldist.RPM.Logit.LC2.cvrout,
# ptotaldist.RPM.Logit.Heter.cvrout,

ptotaldist.cvrout.summary <- t( apply(ptotaldist.cvrout,1,ptotalsummaryfn) )

####

### end of code ###


md<-aov(MAD~condition*ifTW*sys,
+         data=avg_measures.1b2)
> summary(md)
                    Df   Sum Sq Mean Sq F value   Pr(>F)
condition            6   545494   90916   1.480    0.185
ifTW                 2   139644   69822   1.136    0.322
sys                  1     9239    9239   0.150    0.698
condition:ifTW      12   777131   64761   1.054    0.399
condition:sys        6   301527   50255   0.818    0.557
ifTW:sys             2  1372246  686123  11.168 2.08e-05 ***
condition:ifTW:sys  12   317249   26437   0.430    0.951
Residuals          307 18860918   61436
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> TukeyHSD(md,"ifTW:sys")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = MAD ~ condition * ifTW * sys, data = avg_measures.1b2)

$`ifTW:sys`
              diff        lwr       upr     p adj
1:1-0:1 -105.26173 -287.82748  77.30402 0.5636944
2:1-0:1 -226.45434 -432.32579 -20.58290 0.0216428
0:2-0:1 -209.56433 -395.27424 -23.85442 0.0167713
1:2-0:1 -144.90165 -324.90447  35.10118 0.1937212
2:2-0:1  -53.96257 -237.29550 129.37035 0.9589418
2:1-1:1 -121.19261 -271.28169  28.89646 0.1908875
0:2-1:1 -104.30260 -225.26478  16.65958 0.1354705
1:2-1:1  -39.63992 -151.64302  72.36319 0.9127477
2:2-1:1   51.29916  -65.98102 168.57933 0.8093737
0:2-2:1   16.89001 -137.00816 170.78818 0.9995860
1:2-2:1   81.55270  -65.40816 228.51355 0.6048526
2:2-2:1  172.49177   21.47046 323.51309 0.0147945
1:2-0:2   64.66268  -52.39547 181.72084 0.6096440
2:2-0:2  155.60176   33.48478 277.71873 0.0040772
2:2-1:2   90.93907  -22.31022 204.18837 0.1960297

 md<-aov(xpos_flips~condition*des,
+         data=avg_measures.1b2)
> summary(md)
               Df Sum Sq Mean Sq F value Pr(>F)
condition       6    7.0   1.164   0.754 0.6066
des             2   13.2   6.601   4.278 0.0146 *
condition:des   3    3.6   1.199   0.777 0.5076
Residuals     337  520.0   1.543
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> TukeyHSD(md,"des")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = xpos_flips ~ condition * des, data = avg_measures.1b2)

$des
             diff        lwr       upr     p adj
1-0    0.29016485 -0.1858717 0.7662014 0.3950214
2-0    0.07976862 -0.3879936 0.5475309 0.9714299
NaN-0  0.13740444 -0.5325840 0.8073929 0.9518763
2-1   -0.21039623 -0.6240313 0.2032389 0.5550854
NaN-1 -0.15276041 -0.7861458 0.4806250 0.9247621
NaN-2  0.05763582 -0.5695545 0.6848262 0.9952931
md<-aov(xpos_flips~condition*ifTW,
+         data=avg_measures.1b2)
> summary(md)
                Df Sum Sq Mean Sq F value  Pr(>F)
condition        6    7.0   1.164   0.775 0.59028
ifTW             2   15.5   7.728   5.145 0.00631 **
condition:ifTW  12   28.7   2.393   1.593 0.09187 .
Residuals      328  492.7   1.502
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> TukeyHSD(md,"ifTW")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = xpos_flips ~ condition * ifTW, data = avg_measures.1b2)

$ifTW
         diff         lwr       upr     p adj
1-0 0.3359125 -0.05358752 0.7254125 0.1065572
2-0 0.5776706  0.15296690 1.0023743 0.0042604
2-1 0.2417581 -0.12080831 0.6043246 0.2601680

md<-aov(xpos_flips~condition*sys*des,
+         data=avg_measures.1b2)
> summary(md)
                   Df Sum Sq Mean Sq F value Pr(>F)
condition           6    7.0   1.164   0.737 0.6205
sys                 1    0.2   0.155   0.098 0.7539
des                 2   13.1   6.549   4.146 0.0167 *
condition:sys       6    1.2   0.207   0.131 0.9924
condition:des       3    3.6   1.184   0.750 0.5232
sys:des             2    0.5   0.254   0.161 0.8517
condition:sys:des   3    4.9   1.626   1.029 0.3798
Residuals         325  513.4   1.580
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> TukeyHSD(md,"des")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = xpos_flips ~ condition * sys * des, data = avg_measures.1b2)

$des
             diff        lwr       upr     p adj
1-0    0.28869119 -0.1930413 0.7704237 0.4103524
2-0    0.07940827 -0.3939510 0.5527675 0.9727238
NaN-0  0.13672446 -0.5412807 0.8147296 0.9540658
2-1   -0.20928293 -0.6278673 0.2093015 0.5692374
NaN-1 -0.15196674 -0.7929308 0.4889973 0.9281440
NaN-2  0.05731619 -0.5773788 0.6920111 0.9955272

$`0`
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  0.000   1.690   2.143   2.365   3.000   9.000

$`1`
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  0.000   2.000   2.400   2.716   3.292   6.500

$`2`
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  0.000   1.757   2.388   2.505   3.000   6.000

$`NaN`
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  1.200   1.840   2.185   2.277   2.783   4.200

md<-aov(xpos_flips~condition*ifTW*des,
+         data=avg_measures.1b2)
> summary(md)
                    Df Sum Sq Mean Sq F value  Pr(>F)
condition            6    7.0   1.164   0.786 0.58149
ifTW                 2   15.5   7.728   5.220 0.00589 **
des                  2   13.5   6.737   4.550 0.01127 *
condition:ifTW      12   30.3   2.521   1.703 0.06505 .
condition:des        3    2.4   0.812   0.548 0.64963
ifTW:des             4    5.7   1.423   0.961 0.42895
condition:ifTW:des   6    6.1   1.020   0.689 0.65888
Residuals          313  463.4   1.481
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> TukeyHSD(md,"ifTW")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = xpos_flips ~ condition * ifTW * des, data = avg_measures.1b2)

$ifTW
         diff         lwr       upr     p adj
1-0 0.3359125 -0.05087533 0.7227003 0.1032751
2-0 0.5776706  0.15592422 0.9994170 0.0039629
2-1 0.2417581 -0.11828367 0.6017999 0.2552464

> TukeyHSD(md,"des")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = xpos_flips ~ condition * ifTW * des, data = avg_measures.1b2)

$des
             diff        lwr       upr     p adj
1-0    0.29819195 -0.1682664 0.7646503 0.3515765
2-0    0.06729521 -0.3910554 0.5256458 0.9813999
NaN-0  0.13535212 -0.5211557 0.7918600 0.9511049
2-1   -0.23089674 -0.6362092 0.1744157 0.4560575
NaN-1 -0.16283983 -0.7834810 0.4578014 0.9055143
NaN-2  0.06805691 -0.5465139 0.6826278 0.9918313

md<-aov(xpos_flips~condition*ifTW*sys,
+         data=avg_measures.1b2)
> summary(md)
                    Df Sum Sq Mean Sq F value  Pr(>F)
condition            6    7.0   1.164   0.768 0.59530
ifTW                 2   15.5   7.728   5.103 0.00660 **
sys                  1    0.0   0.018   0.012 0.91354
condition:ifTW      12   28.7   2.392   1.580 0.09628 .
condition:sys        6    3.6   0.605   0.399 0.87934
ifTW:sys             2   14.3   7.145   4.718 0.00959 **
condition:ifTW:sys  12    9.8   0.819   0.541 0.88742
Residuals          307  464.9   1.514
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> TukeyHSD(md,"ifTW")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = xpos_flips ~ condition * ifTW * sys, data = avg_measures.1b2)

$ifTW
         diff         lwr       upr     p adj
1-0 0.3359125 -0.05530934 0.7271343 0.1086046
2-0 0.5776706  0.15108946 1.0042518 0.0044746
2-1 0.2417581 -0.12241107 0.6059273 0.2631152

> TukeyHSD(md,"ifTW:sys")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = xpos_flips ~ condition * ifTW * sys, data = avg_measures.1b2)

$`ifTW:sys`
                 diff         lwr        upr     p adj
1:1-0:1 -0.4386087993 -1.34502422 0.46780662 0.7344479
2:1-0:1 -0.2777871291 -1.29991232 0.74433806 0.9708469
0:2-0:1 -0.8919406045 -1.81396636 0.03008515 0.0643955
1:2-0:1 -0.2770559831 -1.17074680 0.61663484 0.9489493
2:2-0:1 -0.0355591266 -0.94578346 0.87466520 0.9999975
2:1-1:1  0.1608216701 -0.58435124 0.90599458 0.9895988
0:2-1:1 -0.4533318053 -1.05389342 0.14722981 0.2574432
1:2-1:1  0.1615528162 -0.39452816 0.71763380 0.9611587
2:2-1:1  0.4030496727 -0.17923127 0.98533062 0.3532641
0:2-2:1 -0.6141534754 -1.37823805 0.14993110 0.1951286
1:2-2:1  0.0007311461 -0.72891057 0.73037286 1.0000000
2:2-2:1  0.2422280025 -0.50757336 0.99202936 0.9394864
1:2-0:2  0.6148846215  0.03370597 1.19606327 0.0311084
2:2-0:2  0.8563814779  0.25008644 1.46267651 0.0009095
2:2-1:2  0.2414968564 -0.32077129 0.80376500 0.8210018

md<-aov(idle_time~condition*sys,
+         data=avg_measures.1b2)
> summary(md)
               Df Sum Sq Mean Sq F value  Pr(>F)
condition       6   1188     198   0.444 0.84887
sys             1   4322    4322   9.695 0.00201 **
condition:sys   6   2620     437   0.979 0.43904
Residuals     335 149331     446
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> TukeyHSD(md,"sys")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = idle_time ~ condition * sys, data = avg_measures.1b2)

$sys
        diff     lwr     upr     p adj
2-1 7.323521 2.69494 11.9521 0.0020156

md<-aov(idle_time~condition*ifTW,
+         data=avg_measures.1b2)
> summary(md)
                Df Sum Sq Mean Sq F value Pr(>F)
condition        6   1188   198.0   0.437 0.8541
ifTW             2   3447  1723.3   3.801 0.0233 *
condition:ifTW  12   4116   343.0   0.757 0.6952
Residuals      328 148710   453.4
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> TukeyHSD(md,"ifTW")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = idle_time ~ condition * ifTW, data = avg_measures.1b2)

$ifTW
         diff        lwr        upr     p adj
1-0 -6.394662 -13.161724  0.3724001 0.0685275
2-0 -8.269054 -15.647736 -0.8903730 0.0236474
2-1 -1.874392  -8.173519  4.4247338 0.7633029

md<-aov(idle_time~condition*sys*des,
+         data=avg_measures.1b2)
> summary(md)
                   Df Sum Sq Mean Sq F value  Pr(>F)
condition           6   1188     198   0.446 0.84744
sys                 1   4322    4322   9.739 0.00197 **
des                 2   1272     636   1.433 0.24011
condition:sys       6   2400     400   0.901 0.49402
condition:des       3   1205     402   0.905 0.43866
sys:des             2   1031     516   1.162 0.31421
condition:sys:des   3   1822     607   1.368 0.25234
Residuals         325 144221     444
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> TukeyHSD(md,"sys")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = idle_time ~ condition * sys * des, data = avg_measures.1b2)

$sys
        diff      lwr      upr     p adj
2-1 7.323521 2.704866 11.94218 0.0019744

md<-aov(idle_time~condition*ifTW*des,
+         data=avg_measures.1b2)
> summary(md)
                    Df Sum Sq Mean Sq F value Pr(>F)
condition            6   1188   198.0   0.430 0.8587
ifTW                 2   3447  1723.3   3.742 0.0248 *
des                  2   1411   705.6   1.532 0.2177
condition:ifTW      12   4160   346.7   0.753 0.6990
condition:des        3   1083   361.0   0.784 0.5037
ifTW:des             4    885   221.2   0.480 0.7503
condition:ifTW:des   6   1136   189.3   0.411 0.8716
Residuals          313 144152   460.5
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> TukeyHSD(md,"ifTW")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = idle_time ~ condition * ifTW * des, data = avg_measures.1b2)

$ifTW
         diff        lwr        upr     p adj
1-0 -6.394662 -13.216475  0.4271511 0.0714670
2-0 -8.269054 -15.707435 -0.8306736 0.0250813
2-1 -1.874392  -8.224484  4.4756988 0.7665190

 md<-aov(idle_time~condition*ifTW*sys,
+         data=avg_measures.1b2)
> summary(md)
                    Df Sum Sq Mean Sq F value  Pr(>F)
condition            6   1188     198   0.452 0.84319
ifTW                 2   3447    1723   3.935 0.02053 *
sys                  1   3642    3642   8.316 0.00421 **
condition:ifTW      12   3877     323   0.738 0.71405
condition:sys        6   3088     515   1.175 0.31928
ifTW:sys             2   4176    2088   4.768 0.00914 **
condition:ifTW:sys  12   3604     300   0.686 0.76505
Residuals          307 134439     438
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> TukeyHSD(md,"ifTW")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = idle_time ~ condition * ifTW * sys, data = avg_measures.1b2)

$ifTW
         diff        lwr        upr     p adj
1-0 -6.394662 -13.047348  0.2580239 0.0625385
2-0 -8.269054 -15.523022 -1.0150868 0.0208458
2-1 -1.874392  -8.067051  4.3182666 0.7560805

> TukeyHSD(md,"sys")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = idle_time ~ condition * ifTW * sys, data = avg_measures.1b2)

$sys
        diff      lwr      upr     p adj
2-1 6.578166 1.989011 11.16732 0.0051061

> TukeyHSD(md,"ifTW:sys")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = idle_time ~ condition * ifTW * sys, data = avg_measures.1b2)

$`ifTW:sys`
               diff        lwr        upr     p adj
1:1-0:1   8.6658155  -6.747684 24.0793147 0.5910070
2:1-0:1   6.3664624 -11.014669 23.7475939 0.9002731
0:2-0:1  21.9003652   6.221414 37.5793163 0.0010862
1:2-0:1  12.0669796  -3.130139 27.2640983 0.2066292
2:2-0:1   9.6186461  -5.859623 25.0969152 0.4788332
2:1-1:1  -2.2993531 -14.970940 10.3722340 0.9953508
0:2-1:1  13.2345497   3.022062 23.4470369 0.0032534
1:2-1:1   3.4011641  -6.054935 12.8572628 0.9070627
2:2-1:1   0.9528305  -8.948796 10.8544569 0.9997826
0:2-2:1  15.5339028   2.540725 28.5270808 0.0089428
1:2-2:1   5.7005172  -6.706964 18.1079981 0.7751624
2:2-2:1   3.2521837  -9.498110 16.0024771 0.9779241
1:2-0:2  -9.8333856 -19.716268  0.0494964 0.0520144
2:2-0:2 -12.2817191 -22.591703 -1.9717357 0.0093211
2:2-1:2  -2.4483336 -12.009644  7.1129772 0.9775395

 md<-aov(idle_time~condition*ifTW*sys*des,
+         data=avg_measures.1b2)
> summary(md)
                        Df Sum Sq Mean Sq F value Pr(>F)
condition                6   1188     198   0.442 0.8506
ifTW                     2   3447    1723   3.844 0.0226 *
sys                      1   3642    3642   8.122 0.0047 **
des                      2   1256     628   1.401 0.2480
condition:ifTW          12   3929     327   0.730 0.7215
condition:sys            6   2917     486   1.084 0.3719
ifTW:sys                 2   3930    1965   4.382 0.0134 *
condition:des            3   1121     374   0.834 0.4764
ifTW:des                 4    822     206   0.458 0.7662
sys:des                  2   1400     700   1.561 0.2118
condition:ifTW:sys      12   3419     285   0.635 0.8113
condition:ifTW:des       6    994     166   0.369 0.8980
condition:sys:des        3   2395     798   1.781 0.1510
ifTW:sys:des             3    504     168   0.375 0.7711
condition:ifTW:sys:des   5   1399     280   0.624 0.6815
Residuals              279 125097     448
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> TukeyHSD(md,"ifTW")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = idle_time ~ condition * ifTW * sys * des, data = avg_measures.1b2)

$ifTW
         diff        lwr        upr     p adj
1-0 -6.394662 -13.129644  0.3403203 0.0667977
2-0 -8.269054 -15.612757 -0.9253523 0.0228708
2-1 -1.874392  -8.143657  4.3948722 0.7610385

> TukeyHSD(md,"sys")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = idle_time ~ condition * ifTW * sys * des, data = avg_measures.1b2)

$sys
        diff     lwr      upr     p adj
2-1 6.578166 1.93267 11.22366 0.0056772

> TukeyHSD(md,"ifTW:sys")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = idle_time ~ condition * ifTW * sys * des, data = avg_measures.1b2)

$`ifTW:sys`
              diff        lwr        upr     p adj
1:1-0:1   8.221861  -7.384597 23.8283184 0.6569806
2:1-0:1   5.961033 -11.637690 23.5597556 0.9264678
0:2-0:1  21.424120   5.548887 37.2993531 0.0018503
1:2-0:1  11.765901  -3.621468 27.1532694 0.2437071
2:2-0:1   9.268401  -6.403638 24.9404394 0.5348563
2:1-1:1  -2.260828 -15.091048 10.5693927 0.9959345
0:2-1:1  13.202260   2.861924 23.5425950 0.0039950
1:2-1:1   3.544040  -6.030437 13.1185181 0.8958674
2:2-1:1   1.046540  -8.979043 11.0721230 0.9996746
0:2-2:1  15.463087   2.307250 28.6189243 0.0108817
1:2-2:1   5.804868  -6.757940 18.3676757 0.7704213
2:2-2:1   3.307368  -9.602544 16.2172797 0.9774116
1:2-0:2  -9.658219 -19.664823  0.3483847 0.0654469
2:2-0:2 -12.155719 -22.594772 -1.7166673 0.0120616
2:2-1:2  -2.497500 -12.178507  7.1835068 0.9767069

md<-aov(idle_time~condition*eht_0,
+         data=avg_measures.1b2)
> summary(md)
                 Df Sum Sq Mean Sq F value Pr(>F)
condition         6   1188   198.0   0.433 0.8567
eht_0             2   3172  1586.2   3.468 0.0323 *
condition:eht_0  12   3094   257.8   0.564 0.8707
Residuals       328 150006   457.3
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> TukeyHSD(md,"eht_0")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = idle_time ~ condition * eht_0, data = avg_measures.1b2)

$eht_0
        diff       lwr        upr     p adj
1-0 -5.24357 -12.95302  2.4658839 0.2465030
2-0 -9.29814 -17.69273 -0.9035542 0.0257361
2-1 -4.05457 -10.19613  2.0869934 0.2671106

md<-aov(idle_time~condition*eht_0*sys,
+         data=avg_measures.1b2)
> summary(md)
                     Df Sum Sq Mean Sq F value Pr(>F)
condition             6   1188     198   0.448 0.8465
eht_0                 2   3172    1586   3.585 0.0289 *
sys                   1   4217    4217   9.531 0.0022 **
condition:eht_0      12   3073     256   0.579 0.8590
condition:sys         6   2701     450   1.017 0.4140
eht_0:sys             2   3389    1694   3.830 0.0228 *
condition:eht_0:sys  12   3883     324   0.731 0.7204
Residuals           307 135837     442
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> TukeyHSD(md,"sys")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = idle_time ~ condition * eht_0 * sys, data = avg_measures.1b2)

$sys
       diff      lwr      upr     p adj
2-1 7.22303 2.610084 11.83598 0.0022492

> TukeyHSD(md,"eht_0")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = idle_time ~ condition * eht_0 * sys, data = avg_measures.1b2)

$eht_0
        diff       lwr       upr     p adj
1-0 -5.24357 -12.82902  2.341874 0.2353780
2-0 -9.29814 -17.55770 -1.038584 0.0228888
2-1 -4.05457 -10.09734  1.988204 0.2557199

> TukeyHSD(md,"eht_0:sys")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = idle_time ~ condition * eht_0 * sys, data = avg_measures.1b2)

$`eht_0:sys`
               diff         lwr         upr     p adj
1:1-0:1 -15.1295022 -30.3000128  0.04100842 0.0510789
2:1-0:1 -21.2067556 -37.3944366 -5.01907455 0.0028032
0:2-0:1  -6.9243059 -23.6668946  9.81828281 0.8433384
1:2-0:1  -6.6768368 -20.8953478  7.54167415 0.7585935
2:2-0:1  -8.6142783 -23.8145059  6.58594924 0.5824518
2:1-1:1  -6.0772534 -18.1443588  5.98985191 0.6998387
0:2-1:1   8.2051963  -4.5966899 21.00708247 0.4428399
1:2-1:1   8.4526654  -0.8069279 17.71225863 0.0960283
2:2-1:1   6.5152238  -4.1908928 17.22134040 0.5029795
0:2-2:1  14.2824497   0.2901334 28.27476602 0.0423500
1:2-2:1  14.5299188   3.6838737 25.37596384 0.0020418
2:2-2:1  12.5924772   0.4880335 24.69692098 0.0360691
1:2-0:2   0.2474691 -11.4105733 11.90551146 0.9999999
2:2-0:2  -1.6899725 -14.5270600 11.14711510 0.9989965
2:2-1:2  -1.9374415 -11.2456419  7.37075885 0.9911927

md<-aov(idle_time~condition*noKD,
+         data=avg_measures.1b2)
> summary(md)
                Df Sum Sq Mean Sq F value   Pr(>F)
condition        6   1188     198   0.447    0.847
noKD             1   7369    7369  16.647 5.63e-05 ***
condition:noKD   6    605     101   0.228    0.968
Residuals      335 148298     443
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> TukeyHSD(md,"noKD")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = idle_time ~ condition * noKD, data = avg_measures.1b2)

$noKD
         diff       lwr       upr    p adj
1-0 -9.374459 -13.89448 -4.854436 5.64e-05

> tapply(avg_measures.1b2$idle_time, avg_measures.1b2$noKD, summary)
$`0`
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  220.0   308.4   319.7   316.8   326.7   349.8

$`1`
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  218.0   299.7   313.7   307.5   325.0   353.0

md<-aov(idle_time~condition*noDPP,
+         data=avg_measures.1b2)
> summary(md)
                 Df Sum Sq Mean Sq F value  Pr(>F)
condition         6   1188     198   0.438 0.85318
noDPP             1   4553    4553  10.072 0.00164 **
condition:noDPP   6    286      48   0.106 0.99576
Residuals       335 151433     452
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> TukeyHSD(md,"noDPP")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = idle_time ~ condition * noDPP, data = avg_measures.1b2)

$noDPP
         diff       lwr       upr     p adj
1-0 -7.241218 -11.73015 -2.752291 0.0016477

> tapply(avg_measures.1b2$idle_time, avg_measures.1b2$noDPP, summary)
$`0`
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  220.0   308.2   318.8   316.4   326.0   349.8

$`1`
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  218.0   300.1   315.8   309.2   325.9   353.0

md<-aov(xpos_flips~condition*noKD,
+         data=avg_measures.1b2)
> summary(md)
                Df Sum Sq Mean Sq F value   Pr(>F)
condition        6    7.0   1.164   0.757 0.604264
noKD             1   20.1  20.070  13.056 0.000349 ***
condition:noKD   6    1.8   0.302   0.196 0.977701
Residuals      335  515.0   1.537
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> TukeyHSD(md,"noKD")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = xpos_flips ~ condition * noKD, data = avg_measures.1b2)

$noKD
         diff       lwr       upr     p adj
1-0 0.4892237 0.2228692 0.7555781 0.0003491

> tapply(avg_measures.1b2$xpos_flips, avg_measures.1b2$noKD, summary)
$`0`
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  0.000   1.667   2.167   2.327   2.913   9.000

$`1`
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  0.000   2.000   2.500   2.815   3.579   7.000
md<-aov(xpos_flips~condition*noDPP,
+         data=avg_measures.1b2)
> summary(md)
                 Df Sum Sq Mean Sq F value  Pr(>F)
condition         6    7.0   1.164   0.763 0.59980
noDPP             1   16.4  16.446  10.779 0.00114 **
condition:noDPP   6    9.2   1.541   1.010 0.41857
Residuals       335  511.1   1.526
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> TukeyHSD(md,"noDPP")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = xpos_flips ~ condition * noDPP, data = avg_measures.1b2)

$noDPP
         diff      lwr       upr     p adj
1-0 0.4352118 0.174413 0.6960107 0.0011371

> tapply(avg_measures.1b2$xpos_flips, avg_measures.1b2$noDPP, summary)
$`0`
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  0.000   1.667   2.200   2.322   2.915   9.000

$`1`
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  0.000   2.000   2.500   2.755   3.500   7.000

md<-aov(MAD~condition*noTMD,
+         data=avg_measures.1b2)
> summary(md)
                 Df   Sum Sq Mean Sq F value Pr(>F)
condition         6   545494   90916   1.431 0.2018
noTMD             1   348233  348233   5.482 0.0198 *
condition:noTMD   6   151395   25233   0.397 0.8807
Residuals       335 21278327   63517
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> TukeyHSD(md,"noTMD")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = MAD ~ condition * noTMD, data = avg_measures.1b2)

$noTMD
         diff       lwr       upr     p adj
1-0 -67.16786 -123.6254 -10.71029 0.0198582

> tapply(avg_measures.1b2$MAD, avg_measures.1b2$noTMD, summary)
$`0`
     Min.   1st Qu.    Median      Mean   3rd Qu.      Max.
-537.3025    0.0502  130.7227  162.8187  301.1538  984.6729

$`1`
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
-521.76  -52.82   53.67   94.91  211.94 1007.23

> tapply(avg_measures.1b2$MAD, avg_measures.1b2$noTMD, mean)
        0         1
162.81866  94.90568
> tapply(avg_measures.1b2$MAD, avg_measures.1b2$noTMD, sd)
       0        1
260.9275 231.4039
md<-aov(MAD~condition*noKD,
+         data=avg_measures.1b2)
> summary(md)
                Df   Sum Sq Mean Sq F value Pr(>F)
condition        6   545494   90916   1.496 0.1787
noKD             1   711448  711448  11.706 0.0007 ***
condition:noKD   6   706984  117831   1.939 0.0740 .
Residuals      335 20359523   60775
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> TukeyHSD(md,"noKD")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = MAD ~ condition * noKD, data = avg_measures.1b2)

$noKD
        diff     lwr      upr     p adj
1-0 92.10979 39.1487 145.0709 0.0007008

> tapply(avg_measures.1b2$MAD, avg_measures.1b2$noKD, summary)
$`0`
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
-537.30  -31.23   91.93  103.10  237.14  926.96

$`1`
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
-419.04   20.63  146.33  196.18  356.20 1007.23
 tapply(avg_measures.1b2$MAD, avg_measures.1b2$noKD, mean)
       0        1
103.1020 196.1814
> tapply(avg_measures.1b2$MAD, avg_measures.1b2$noKD, sd)
       0        1
237.5888 266.2800

md<-aov(MAD~condition*noDPP,
+         data=avg_measures.1b2)
> summary(md)
                 Df   Sum Sq Mean Sq F value   Pr(>F)
condition         6   545494   90916   1.501 0.176932
noDPP             1   778731  778731  12.858 0.000386 ***
condition:noDPP   6   710408  118401   1.955 0.071568 .
Residuals       335 20288816   60564
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> TukeyHSD(md,"noDPP")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = MAD ~ condition * noDPP, data = avg_measures.1b2)

$noDPP
        diff     lwr      upr     p adj
1-0 94.70214 42.7432 146.6611 0.0003868

> tapply(avg_measures.1b2$MAD, avg_measures.1b2$noDPP, summary)
$`0`
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
-537.30  -59.62   90.60   95.91  232.46  926.96

$`1`
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
-419.04   16.99  133.91  191.84  341.24 1007.23

> tapply(avg_measures.1b2$MAD, avg_measures.1b2$noDPP, mean)
        0         1
 95.90984 191.84309
> tapply(avg_measures.1b2$MAD, avg_measures.1b2$noDPP, sd)
       0        1
239.1074 260.0829

md<-aov(idle_time~condition*noKD*des,
+         data=avg_measures.1b2)
> summary(md)
                    Df Sum Sq Mean Sq F value   Pr(>F)
condition            6   1188     198   0.443    0.850
noKD                 1   7369    7369  16.496 6.11e-05 ***
des                  2   1469     735   1.644    0.195
condition:noKD       6    609     101   0.227    0.968
condition:des        3   1291     430   0.963    0.410
noKD:des             2    260     130   0.291    0.748
condition:noKD:des   3     92      31   0.068    0.977
Residuals          325 145183     447
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> TukeyHSD(md,"noKD")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = idle_time ~ condition * noKD * des, data = avg_measures.1b2)

$noKD
         diff       lwr       upr    p adj
1-0 -9.374459 -13.91556 -4.833362 6.12e-05

> tapply(avg_measures.1b2$idle_time, avg_measures.1b2$noKD, summary)
$`0`
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  220.0   308.4   319.7   316.8   326.7   349.8

$`1`
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  218.0   299.7   313.7   307.5   325.0   353.0

> tapply(avg_measures.1b2$idle_time, avg_measures.1b2$noKD, mean)
      0       1
316.828 307.459
> tapply(avg_measures.1b2$idle_time, avg_measures.1b2$noKD, sd)
       0        1
16.49174 25.93991
md<-aov(xpos_flips~condition*noTGG*des,
+         data=avg_measures.1b2)
> summary(md)
                     Df Sum Sq Mean Sq F value Pr(>F)
condition             6    7.0   1.164   0.754 0.6069
noTGG                 1    0.9   0.878   0.569 0.4513
des                   2   13.4   6.695   4.336 0.0139 *
condition:noTGG       6    8.2   1.366   0.885 0.5062
condition:des         3    3.7   1.222   0.791 0.4995
noTGG:des             2    6.9   3.426   2.219 0.1104
condition:noTGG:des   3    2.0   0.681   0.441 0.7237
Residuals           325  501.8   1.544
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> TukeyHSD(md,"des")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = xpos_flips ~ condition * noTGG * des, data = avg_measures.1b2)

$des
             diff        lwr       upr     p adj
1-0    0.29178330 -0.1844795 0.7680461 0.3902758
2-0    0.08103590 -0.3869486 0.5490204 0.9701386
NaN-0  0.13849875 -0.5318081 0.8088056 0.9508363
2-1   -0.21074740 -0.6245790 0.2030843 0.5539579
NaN-1 -0.15328455 -0.7869709 0.4804018 0.9241160
NaN-2  0.05746284 -0.5700255 0.6849512 0.9953384

md<-aov(xpos_flips~condition*noKD*des,
+         data=avg_measures.1b2)
> summary(md)
                    Df Sum Sq Mean Sq F value   Pr(>F)
condition            6    7.0   1.164   0.771 0.593236
noKD                 1   20.1  20.070  13.297 0.000309 ***
des                  2   12.9   6.451   4.274 0.014715 *
condition:noKD       6    1.8   0.293   0.194 0.978318
condition:des        3    3.9   1.313   0.870 0.456987
noKD:des             2    3.5   1.756   1.164 0.313668
condition:noKD:des   3    4.1   1.376   0.911 0.435665
Residuals          325  490.5   1.509
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> TukeyHSD(md,"des")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = xpos_flips ~ condition * noKD * des, data = avg_measures.1b2)

$des
             diff        lwr       upr     p adj
1-0    0.28321271 -0.1876653 0.7540907 0.4070513
2-0    0.08676833 -0.3759250 0.5494617 0.9625545
NaN-0  0.13766541 -0.5250627 0.8003936 0.9500994
2-1   -0.19644438 -0.6055971 0.2127084 0.6019414
NaN-1 -0.14554729 -0.7720690 0.4809744 0.9320317
NaN-2  0.05089709 -0.5694967 0.6712909 0.9966343

> TukeyHSD(md,"noKD")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = xpos_flips ~ condition * noKD * des, data = avg_measures.1b2)

$noKD
         diff       lwr       upr     p adj
1-0 0.4892237 0.2252648 0.7531826 0.0003098

> tapply(avg_measures.1b2$xpos_flips, avg_measures.1b2$noKD, summary)
$`0`
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  0.000   1.667   2.167   2.327   2.913   9.000

$`1`
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  0.000   2.000   2.500   2.815   3.579   7.000

> tapply(avg_measures.1b2$xpos_flips, avg_measures.1b2$noKD, sd)
       0        1
1.162561 1.321613

md<-aov(xpos_flips~condition*noDPP*des,
+         data=avg_measures.1b2)
> summary(md)
                     Df Sum Sq Mean Sq F value   Pr(>F)
condition             6    7.0   1.164   0.782 0.584515
noDPP                 1   16.4  16.446  11.053 0.000986 ***
des                   2   13.1   6.570   4.416 0.012822 *
condition:noDPP       6    9.1   1.511   1.015 0.415091
condition:des         3    3.8   1.271   0.854 0.465299
noDPP:des             2    5.1   2.572   1.729 0.179157
condition:noDPP:des   3    5.7   1.887   1.268 0.285357
Residuals           325  483.6   1.488
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> TukeyHSD(md,"des")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = xpos_flips ~ condition * noDPP * des, data = avg_measures.1b2)

$des
             diff        lwr       upr     p adj
1-0    0.28610193 -0.1814247 0.7536285 0.3913216
2-0    0.08697069 -0.3724295 0.5463709 0.9615392
NaN-0  0.13879756 -0.5192137 0.7968089 0.9479230
2-1   -0.19913123 -0.6053719 0.2071094 0.5853817
NaN-1 -0.14730437 -0.7693669 0.4747582 0.9283841
NaN-2  0.05182687 -0.5641514 0.6678051 0.9963727

> TukeyHSD(md,"noDPP")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = xpos_flips ~ condition * noDPP * des, data = avg_measures.1b2)

$noDPP
         diff       lwr       upr     p adj
1-0 0.4352118 0.1776431 0.6927805 0.0009883

> tapply(avg_measures.1b2$xpos_flips, avg_measures.1b2$noDPP, summary)
$`0`
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  0.000   1.667   2.200   2.322   2.915   9.000

$`1`
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  0.000   2.000   2.500   2.755   3.500   7.000

> tapply(avg_measures.1b2$xpos_flips, avg_measures.1b2$noDPP, sd)
       0        1
1.115116 1.356505
md<-aov(MAD~condition*noTMD*des,
+         data=avg_measures.1b2)
> summary(md)
                     Df   Sum Sq Mean Sq F value Pr(>F)
condition             6   545494   90916   1.419 0.2064
noTMD                 1   348233  348233   5.437 0.0203 *
des                   2   277060  138530   2.163 0.1167
condition:noTMD       6   150047   25008   0.390 0.8850
condition:des         3    10408    3469   0.054 0.9834
noTMD:des             2    39093   19546   0.305 0.7372
condition:noTMD:des   3   135417   45139   0.705 0.5498
Residuals           325 20817698   64054
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> TukeyHSD(md,"noTMD")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = MAD ~ condition * noTMD * des, data = avg_measures.1b2)

$noTMD
         diff       lwr       upr     p adj
1-0 -67.16786 -123.8699 -10.46579 0.0203965

> tapply(avg_measures.1b2$MAD, avg_measures.1b2$noTMD, summary)
$`0`
     Min.   1st Qu.    Median      Mean   3rd Qu.      Max.
-537.3025    0.0502  130.7227  162.8187  301.1538  984.6729

$`1`
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
-521.76  -52.82   53.67   94.91  211.94 1007.23

> tapply(avg_measures.1b2$MAD, avg_measures.1b2$noTMD, sd)
       0        1
260.9275 231.4039

md<-aov(MAD~condition*noKD*sys,
+         data=avg_measures.1b2)
> summary(md)
                    Df   Sum Sq Mean Sq F value   Pr(>F)
condition            6   545494   90916   1.495 0.179197
noKD                 1   711448  711448  11.698 0.000706 ***
sys                  1    17014   17014   0.280 0.597218
condition:noKD       6   704009  117335   1.929 0.075620 .
condition:sys        6   270452   45075   0.741 0.616824
noKD:sys             1   336973  336973   5.541 0.019180 *
condition:noKD:sys   6   216162   36027   0.592 0.736399
Residuals          321 19521897   60816
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> TukeyHSD(md,"noKD")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = MAD ~ condition * noKD * sys, data = avg_measures.1b2)

$noKD
        diff      lwr      upr     p adj
1-0 92.10979 39.12238 145.0972 0.0007072

> tapply(avg_measures.1b2$MAD, avg_measures.1b2$noKD, summary)
$`0`
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
-537.30  -31.23   91.93  103.10  237.14  926.96

$`1`
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
-419.04   20.63  146.33  196.18  356.20 1007.23

> tapply(avg_measures.1b2$MAD, avg_measures.1b2$noKD, sd)
       0        1
237.5888 266.2800

md<-aov(MAD~condition*noDPP*sys,
+         data=avg_measures.1b2)
> summary(md)
                     Df   Sum Sq Mean Sq F value   Pr(>F)
condition             6   545494   90916   1.483 0.183430
noDPP                 1   778731  778731  12.699 0.000421 ***
sys                   1     9656    9656   0.157 0.691769
condition:noDPP       6   709335  118222   1.928 0.075848 .
condition:sys         6   273741   45624   0.744 0.614581
noDPP:sys             1    93088   93088   1.518 0.218821
condition:noDPP:sys   6   228940   38157   0.622 0.712499
Residuals           321 19684463   61322
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> TukeyHSD(md,"noDPP")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = MAD ~ condition * noDPP * sys, data = avg_measures.1b2)

$noDPP
        diff     lwr      upr     p adj
1-0 94.70214 42.4105 146.9938 0.0004223

> tapply(avg_measures.1b2$MAD, avg_measures.1b2$noDPP, summary)
$`0`
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
-537.30  -59.62   90.60   95.91  232.46  926.96

$`1`
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
-419.04   16.99  133.91  191.84  341.24 1007.23

> tapply(avg_measures.1b2$MAD, avg_measures.1b2$noDPP, sd)
       0        1
239.1074 260.0829

md<-aov(MAD~condition*eht_0*sys,
+         data=avg_measures.1b2)
> summary(md)
                     Df   Sum Sq Mean Sq F value Pr(>F)
condition             6   545494   90916   1.425 0.2046
eht_0                 2    19943    9972   0.156 0.8554
sys                   1    14033   14033   0.220 0.6394
condition:eht_0      12   808885   67407   1.056 0.3970
condition:sys         6   281055   46842   0.734 0.6225
eht_0:sys             2   592747  296374   4.645 0.0103 *
condition:eht_0:sys  12   472085   39340   0.617 0.8280
Residuals           307 19589207   63808
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> TukeyHSD(md,"eht_0:sys")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = MAD ~ condition * eht_0 * sys, data = avg_measures.1b2)

$`eht_0:sys`
               diff        lwr       upr     p adj
1:1-0:1 -115.158836 -297.33840  67.02073 0.4590121
2:1-0:1 -146.298181 -340.69274  48.09638 0.2605992
0:2-0:1 -191.858362 -392.91669   9.19997 0.0710899
1:2-0:1 -122.887533 -293.63473  47.85966 0.3090977
2:2-0:1  -75.544712 -258.08114 106.99172 0.8429500
2:1-1:1  -31.139345 -176.05075 113.77206 0.9898031
0:2-1:1  -76.699526 -230.43476  77.03571 0.7082350
1:2-1:1   -7.728697 -118.92526 103.46787 0.9999564
2:2-1:1   39.614124  -88.95345 168.18169 0.9502358
0:2-2:1  -45.560181 -213.59105 122.47069 0.9711406
1:2-2:1   23.410648 -106.83729 153.65859 0.9955543
2:2-2:1   70.753469  -74.60633 216.11326 0.7295309
1:2-0:2   68.970829  -71.02822 208.96988 0.7192561
2:2-0:2  116.313650  -37.84432 270.47161 0.2579116
2:2-1:2   47.342821  -64.43746 159.12310 0.8295492


#' @import ggplot2
#' @method plot frescalo
#' @export

plot.frescalo <- function(x, y = NULL, ...){

  #options(device = "windows") #this a is fix for windows for Rstudio's 1 device rule

  if('lm_stats' %in% names(x)) lm_stats<-x$lm_stats
  stats<-x$stat

  #dev.new()

  # Setup outer margin area
  par(oma = c(1,1,1,1))

  # Subdivide device into 4 figures
  layout(matrix(c(1,2,3,4), ncol = 2, nrow = 2, byrow = TRUE))

  # Setup margins
  par(mar = c(2,0,0,0))

  # No. Species
  map_data_R(gridrefs = stats$Location, attribute = stats$No_spp, breaks = cat_breaks(stats$No_spp, n_cat = 10, whole_breaks = TRUE), show.axis = FALSE, show.grid = FALSE, legend_pos = "topleft", leg_cex = 0.7, sq_border = NA, bg.col = NULL,shape_data = UK, xlab='',ylab='')
  mtext("(a) No. Species", adj = 0.05, font = 1, cex = 0.7)

  # Rescaled No. Species
  map_data_R(gridrefs = stats$Location, attribute = stats$Spnum_out, breaks = cat_breaks(stats$Spnum_out, n_cat = 10, whole_breaks = TRUE), show.axis = FALSE, show.grid = FALSE, legend_pos = "topleft",leg_cex = 0.7,  sq_border = NA, bg.col = NULL,shape_data = UK, xlab='',ylab='')
  mtext("(b) Rescaled No. Species", adj = 0.05, font = 1, cex = 0.7)

  # Alpha (Derivied Breaks)

  map_data_R(gridrefs = stats$Location, attribute = stats$Alpha, breaks = cat_breaks(stats$Alpha, n_cat = 10, whole_breaks = FALSE, rnd_digits = 2), show.axis = FALSE, show.grid = FALSE, legend_pos = "topleft", leg_cex = 0.7,  sq_border = NA, bg.col = NULL,shape_data = UK, xlab='',ylab='')
  mtext("(c) Alpha (Derived Breaks)", adj = 0.05, font = 1, cex = 0.7)

  par(mar=c(4,4,0,0))

  # histogram of trends
  if('lm_stats' %in% names(x)){
    density_line<-density(lm_stats$b)
    hist(lm_stats$b,main='',xlab='Trend',ylab='Frequency',ylim=c(0,max(density_line$y)),cex.axis=0.7,prob=TRUE)
    abline(v=0,col='blue')
    lines(density_line,col='red')
    mtext("(d) Histogram of species trends", adj = 0.05, font = 1, cex= 0.7)
  }
}


# Mini-Projeto 2

# Limpeza e Transformação de Dados com Power Query e Linguagem R

# Definindo a pasta de trabalho
# Substitua o caminho abaixo pela pasta no seu computador
setwd("C:/Users/dmpm/Documents/PowerBI/Cap12")
getwd()

# Instala os pacotes
install.packages("dplyr")
install.packages("data.table")
install.packages("ggplot2")

# Carrega os pacotes
library(dplyr)
library(data.table)
library(ggplot2)

# Carrega os dados
# Fonte de dados: https://archive.ics.uci.edu/ml/datasets/iris
dados_iris <- iris
View(dados_iris)

# Tarefa 1 - Sumarizar os dados com as médias de cada coluna de um dataset.
library(dplyr)
medias_iris <- summarize(group_by(dados_iris, Species),
                         media_sepal_length = mean(Sepal.Length),
                         media_sepal_width = mean(Sepal.Width),
                         media_petal_length = mean(Petal.Length),
                         media_petal_width = mean(Petal.Width))

View(medias_iris)

# Tarefa 2 - Extrair o valor inteiro de uma das colunas decimais.
library(data.table)
dados_iris_id <- data.table(dados_iris)
View(dados_iris_id)
dados_iris_id$Sepal.Length <- as.integer(dados_iris_id$Sepal.Length)
View(dados_iris_id)

# Para executar no Power Query
library(data.table)
dados_iris_id <- data.table(dados_iris)
dados_iris_id$Sepal.Length <- as.integer(dados_iris_id$Sepal.Length)

# Tarefa 3 – Construir um gráfico mostrando a relação de duas variáveis numéricas para as 3 categorias de uma variável categórica.
library(ggplot2)
ggplot(data = dados_iris, aes(x = Petal.Width, y = Petal.Length)) +
  geom_point(aes(color = Species), size = 3) +
  ggtitle("Largura e Comprimento das Pétalas") +
  labs(x = "Largura da Pétala",
       y = "Comprimento da Pétala") +
  theme_bw() +
  theme(title = element_text(size = 15, color = "turquoise4"))

# Cores disponíveis na Linguagem R
colors()


library(reticulate)
library(Seurat)

sc <- import("scanpy.api", convert=FALSE)

ad <- sc$datasets$pbmc68k_reduced()
ad$X <- ad$raw$X$toarray()

ad$var_names_make_unique()

sr <- Convert(ad, to="seurat")

sc$pp$normalize_per_cell(ad, counts_per_cell_after=1e4)
sc$pp$log1p(ad)

sr <- NormalizeData(object=sr, normalization.method="LogNormalize", scale.factor=1e4)

sc$pp$highly_variable_genes(ad, flavor='seurat', min_mean=0.0125, max_mean=3, min_disp=0.5)

ad_hvg <- py_to_r(ad$var["highly_variable"])
ad_hvg <- names(ad_hvg[ad_hvg])

sr <- FindVariableGenes(object=sr, mean.function=ExpMean, dispersion.function=LogVMR, x.low.cutoff=0.0125, x.high.cutoff=3, y.cutoff=0.5)

sr_hvg <- sr@var.genes

print(all(sr_hvg==ad_hvg))

hvg_info <- as.data.frame(sr@hvg.info)
hvg_info <- cbind(hvg_info, rownames(hvg_info) %in% sr_hvg)
colnames(hvg_info) <- c("means", "dispersions", "dispersions_norm", "highly_variable")
hvg_info <- hvg_info[rownames(sr@data),]

write.table(hvg_info, "seurat_hvg.csv")


#' ShipPowerModel: A package for estimating the propulsive power of commercial
#' marine vessels
#'
#' The ShipPowerModel package by the U.S. Environmental Protection Agency
#' provides functions for estimating the propulsive power of commercial marine
#' vessels (CMV). It also provides functions for estimating auxiliary and boiler
#' engine loads. This package can be used in conjunction with vessel
#' characteristics and emission factors (see the \code{ShipEF} package) to
#' estimate CMV air emissions.
#'
#' @section Primary ShipPowerModel functions:
#' The four functions below represent different methods for estimating vessel
#' power: \itemize{
#' \item \code{\link{calcPropPwr}} - Propeller law, which was used as the basis
#'  for load factor estimates in the U.S. EPA's 2014 National Emissions Inventory
#' \item \code{\link{calcAdmPwr}} - Admiralty formula, which was used as the
#'  basis for load factor estimates in the Third IMO Greenhouse Gas Study
#' \item \code{\link{calcHMPwr}} - Implementation of the Holtrop & Mennen model
#'  as used in the U.S. EPA's 2017 National Emissions Inventory
#' \item \code{\link{calcKristPwr}} - Implementation of Kristensen's SHIP-DESMO
#'  vessel power model
#' }
#'
#' In addition, \code{\link{calcAuxBoilerLoad}} is another primary function,
#' which assigns auxiliary and boiler engine loads based on ship type, ship
#' subtype, and operating mode.
#'
#' @section Vectorization:
#' Every function in this package has been optimized to work on vectors of data.
#' This is because the input datasets required to calculate instantaneous ship
#' power are very large. For example, the resolution of the ship activity data
#' may be as fine as every 5 minutes--or even more. Multiply this across the
#' hundreds or thousands of vessels that may be included in the analysis, and
#' the data become to large to handle via individual function calls per row of
#' data.
#'
#' Therefore, it is recommended that users store their data in \code{data.table}
#' objects and pass columns from the data.table as arguments to the functions in
#' this package. Other forms of vectorization work, as well, such as using
#' \code{data.frame}. The function documentation specifies which arguments can
#' be vectorized, and the vignettes introduced below provide examples on how to
#' do simple vectorization using \code{data.frame}.
#'
#' @section Vignettes:
#' An overview of each model can be found in the following vignette:
#' \code{vignette("OverviewOfPowerModels", package="ShipPowerModel")}
#'
#' Each model also has its own vignette: \itemize{
#' \item \code{vignette("Propeller.Law.Example", package="ShipPowerModel")}
#' \item \code{vignette("Admiralty.Formula.Example", package="ShipPowerModel")}
#' \item \code{vignette("HoltropMennen.Example", package="ShipPowerModel")}
#' \item \code{vignette("Kristensen.Example", package="ShipPowerModel")}
#' }
#'
#' @docType package
#' @name ShipPowerModel
NULL