context("wlda")

test_that("wlda: misspecified arguments", {
	data(iris)
	# wrong variable names
	expect_error(wlda(formula = Species ~ V1, data = iris))
	# wrong class
	expect_error(wlda(formula = iris, data = iris))
	# target variable also in x
	#expect_error(wlda(grouping = iris$Species, x = iris))          ## funktioniert, sollte aber nicht
	# missing x
	expect_error(wlda(grouping = iris$Species))
	## wrong method argument
	# missing quotes
	expect_error(wlda(Species ~ ., data = iris, method = ML))
	# method as vector
	expect_error(wlda(Species ~ ., data = iris, method = c("ML","unbiased")))
})

test_that("wlda throws a warning if grouping variable is numeric", {
	data(iris)
	expect_warning(wlda(formula = Petal.Width ~ ., data = iris))
	expect_warning(wlda(grouping = iris[,1], x = iris[,-1]))
	expect_warning(wlda(grouping = iris$Petal.Width, x = iris[,-5]))
})

test_that("wlda works if only one predictor variable is given", {
	data(iris)
	fit <- wlda(Species ~ Petal.Width, data = iris, subset = 6:60)
	expect_equal(ncol(fit$means), 1)
	expect_equal(dim(fit$cov), rep(1, 2))
})

test_that("wlda: training data from only one class", {
	data(iris)
	expect_that(wlda(Species ~ Petal.Width, data = iris, subset = 1:50), throws_error("training data from only one group given"))
	expect_that(wlda(Species ~ Petal.Width, data = iris, subset = 1), throws_error("training data from only one group given"))
})

test_that("wlda: weighting works correctly", {
	data(iris)
	## check if weighted solution with all weights = 1 equals unweighted solution
	fit1 <- wlda(Species ~ ., data = iris)
	fit2 <- wlda(Species ~ ., data = iris, weights = rep(1,150))
	expect_equal(fit1[-9],fit2[-9])
	## returned weights
	a <- rep(1,150)
	names(a) <- 1:150
	expect_equal(fit1$weights, a)
	expect_equal(fit2$weights, a)
	## weights and subsetting
	# formula, data
	fit <- wlda(Species ~ ., data = iris, subset = 11:60)
	a <- rep(1,50)
	names(a) <- 11:60
	expect_equal(fit$weights, a)
	# formula, data, weights
	fit <- wlda(Species ~ ., data = iris, weights = rep(1:3, 50), subset = 11:60)
	b <- rep(1:3,50)[11:60]
	names(b) <- 11:60
	expect_equal(fit$weights, b)
	# x, grouping
	fit <- wlda(x = iris[,-5], grouping = iris$Species, subset = 11:60)
	expect_equal(fit$weights, a)
	# x, grouping, weights
	fit <- wlda(x = iris[,-5], grouping = iris$Species, weights = rep(1:3, 50), subset = 11:60)
	expect_equal(fit$weights, b)
	## wrong specification of weights argument
	# weights in a matrix
	weight <- matrix(seq(1:150),nrow=50)
	expect_error(wlda(Species ~ ., data = iris, weights = weight))
	# weights < 0
	expect_error(wlda(Species ~ ., data = iris, weights = rep(-5, 150)))
	# weights true/false
	expect_error(wlda(Species ~ ., data = iris, weights = TRUE))
})

test_that("wlda: subsetting works", {
	data(iris)
	# formula, data
	fit1 <- wlda(Species ~ ., data = iris, subset = 1:80)
	fit2 <- wlda(Species ~ ., data = iris[1:80,])
	expect_equal(fit1[-9],fit2[-9])
	# formula, data, weights
	fit1 <- wlda(Species ~ ., data = iris, weights = rep(1:3, each = 50), subset = 1:80)
	fit2 <- wlda(Species ~ ., data = iris[1:80,], weights = rep(1:3, each = 50)[1:80])
	expect_equal(fit1[-9],fit2[-9])
	expect_equal(length(fit1$weights), 80)
	# x, grouping
	fit1 <- wlda(grouping = iris$Species, x = iris[,-5], subset = 1:80)
	fit2 <- wlda(grouping = iris$Species[1:80], x = iris[1:80,-5])
	expect_equal(fit1[c(1:8)],fit2[c(1:8)])
	# x, grouping, weights
	fit1 <- wlda(grouping = iris$Species, x = iris[,-5], weights = rep(1:3, each = 50), subset = 1:80)
	fit2 <- wlda(grouping = iris$Species[1:80], x = iris[1:80,-5], weights = rep(1:3, each = 50)[1:80])
	expect_equal(fit1[c(1:8)],fit2[c(1:8)])
	expect_equal(length(fit1$weights), 80)
	# wrong specification of subset argument
	expect_error(wlda(Species ~ ., data = iris, subset = iris[1:10,]))
	expect_error(wlda(Species ~ ., data = iris, subset = FALSE))
	expect_error(wlda(Species ~ ., data = iris, subset = 0))
	expect_error(wlda(Species ~ ., data = iris, subset = -10:50))
})

test_that("wlda: NA handling works correctly", {
	### NA in x
	data(iris)
	irisna <- iris
	irisna[1:10, c(1,3)] <- NA
	## formula, data
	# na.fail
	expect_error(wlda(Species ~ ., data = irisna, subset = 6:60, na.action = na.fail))
	# check if na.omit works correctly
	fit1 <- wlda(Species ~ ., data = irisna, subset = 6:60, na.action = na.omit)
	fit2 <- wlda(Species ~ ., data = irisna, subset = 11:60)
	expect_equal(fit1[-c(9, 12)], fit2[-9])
	expect_equal(length(fit1$weights), 50)
	## formula, data, weights
	# na.fail
	expect_error(wlda(Species ~ ., data = irisna, subset = 6:60, weights = rep(1:3, 50), na.action = na.fail))
	# check if na.omit works correctly
	fit1 <- wlda(Species ~ ., data = irisna, subset = 6:60, weights = rep(1:3, 50), na.action = na.omit)
	fit2 <- wlda(Species ~ ., data = irisna, subset = 11:60, weights = rep(1:3, 50))
	expect_equal(fit1[-c(9, 12)], fit2[-9])
	expect_equal(length(fit1$weights), 50)
	## x, grouping
	# na.fail
	expect_error(wlda(grouping = irisna$Species, x = irisna[,-5], subset = 6:60, na.action = na.fail))
	# check if na.omit works correctly
	fit1 <- wlda(grouping = irisna$Species, x = irisna[,-5], subset = 6:60, na.action = na.omit)
	fit2 <- wlda(grouping = irisna$Species, x = irisna[,-5], subset = 11:60)
	expect_equal(fit1[1:8], fit2[1:8])
	expect_equal(length(fit1$weights), 50)
	## x, grouping, weights
	# na.fail
	expect_error(wlda(grouping = irisna$Species, x = irisna[,-5], subset = 6:60, weights = rep(1:3, 50), na.action = na.fail))
	# check if na.omit works correctly
	fit1 <- wlda(grouping = irisna$Species, x = irisna[,-5], subset = 6:60, weights = rep(1:3, 50), na.action = na.omit)
	fit2 <- wlda(grouping = irisna$Species, x = irisna[,-5], subset = 11:60, weights = rep(1:3, 50))
	expect_equal(fit1[1:8], fit2[1:8])
	expect_equal(length(fit1$weights), 50)

	### NA in grouping
	irisna <- iris
	irisna$Species[1:10] <- NA
	## formula, data
	# na.fail
	expect_error(wlda(Species ~ ., data = irisna, subset = 6:60, na.action = na.fail))
	# check if na.omit works correctly
	fit1 <- wlda(Species ~ ., data = irisna, subset = 6:60, na.action = na.omit)
	fit2 <- wlda(Species ~ ., data = irisna, subset = 11:60)
	expect_equal(fit1[-c(9, 12)], fit2[-9])
	expect_equal(length(fit1$weights), 50)
	## formula, data, weights
	# na.fail
	expect_error(wlda(Species ~ ., data = irisna, subset = 6:60, weights = rep(1:3, 50), na.action = na.fail))
	# check if na.omit works correctly
	fit1 <- wlda(Species ~ ., data = irisna, subset = 6:60, weights = rep(1:3, 50), na.action = na.omit)
	fit2 <- wlda(Species ~ ., data = irisna, subset = 11:60, weights = rep(1:3, 50))
	expect_equal(fit1[-c(9, 12)], fit2[-9])
	expect_equal(length(fit1$weights), 50)
	## x, grouping
	# na.fail
	expect_error(wlda(grouping = irisna$Species, x = irisna[,-5], subset = 6:60, na.action = na.fail))
	# check if na.omit works correctly
	fit1 <- wlda(grouping = irisna$Species, x = irisna[,-5], subset = 6:60, na.action = na.omit)
	fit2 <- wlda(grouping = irisna$Species, x = irisna[,-5], subset = 11:60)
	expect_equal(fit1[1:8], fit2[1:8])
	expect_equal(length(fit1$weights), 50)
	## x, grouping, weights
	# na.fail
	expect_error(wlda(grouping = irisna$Species, x = irisna[,-5], subset = 6:60, weights = rep(1:3, 50), na.action = na.fail))
	# check if na.omit works correctly
	fit1 <- wlda(grouping = irisna$Species, x = irisna[,-5], subset = 6:60, weights = rep(1:3, 50), na.action = na.omit)
	fit2 <- wlda(grouping = irisna$Species, x = irisna[,-5], subset = 11:60, weights = rep(1:3, 50))
	expect_equal(fit1[1:8], fit2[1:8])
	expect_equal(length(fit1$weights), 50)

	### NA in weights
	weights <- rep(1:3,50)
	weights[1:10] <- NA
	## formula, data, weights
	# na.fail
	expect_error(wlda(Species ~ ., data = iris, subset = 6:60, weights = weights, na.action = na.fail))
	# check if na.omit works correctly
	fit1 <- wlda(Species ~ ., data = iris, subset = 6:60, weights = weights, na.action = na.omit)
	fit2 <- wlda(Species ~ ., data = iris, subset = 11:60, weights = weights)
	expect_equal(fit1[-c(9, 12)], fit2[-9])
	expect_equal(length(fit1$weights), 50)
	## x, grouping, weights
	# na.fail
	expect_error(wlda(grouping = iris$Species, x = iris[,-5], subset = 6:60, weights = weights, na.action = na.fail))
	# check if na.omit works correctly
	fit1 <- wlda(grouping = iris$Species, x = iris[,-5], subset = 6:60, weights = weights, na.action = na.omit)
	fit2 <- wlda(grouping = iris$Species, x = iris[,-5], subset = 11:60, weights = weights)
	expect_equal(fit1[1:8], fit2[1:8])
	expect_equal(length(fit1$weights), 50)

	### NA in subset
	subset <- 6:60
	subset[1:5] <- NA
	## formula, data
	# na.fail
	expect_error(wlda(Species ~ ., data = iris, subset = subset, na.action = na.fail))
	# check if na.omit works correctly
	fit1 <- wlda(Species ~ ., data = iris, subset = subset, na.action = na.omit)
	fit2 <- wlda(Species ~ ., data = iris, subset = 11:60)
	expect_equal(fit1[-c(9, 12)], fit2[-9])
	expect_equal(length(fit1$weights), 50)
	## formula, data, weights
	# na.fail
	expect_error(wlda(Species ~ ., data = iris, subset = subset, weights = rep(1:3, 50), na.action = na.fail))
	# check if na.omit works correctly
	fit1 <- wlda(Species ~ ., data = iris, subset = subset, weights = rep(1:3, 50), na.action = na.omit)
	fit2 <- wlda(Species ~ ., data = iris, subset = 11:60, weights = rep(1:3, 50))
	expect_equal(fit1[-c(9, 12)], fit2[-9])
	expect_equal(length(fit1$weights), 50)
	## x, grouping
	# na.fail
	expect_error(wlda(grouping = iris$Species, x = iris[,-5], subset = subset, na.action = na.fail))
	# check if na.omit works correctly
	fit1 <- wlda(grouping = iris$Species, x = iris[,-5], subset = subset, na.action = na.omit)
	fit2 <- wlda(grouping = iris$Species, x = iris[,-5], subset = 11:60)
	expect_equal(fit1[1:8], fit2[1:8])
	expect_equal(length(fit1$weights), 50)
	## x, grouping, weights
	# na.fail
	expect_error(wlda(grouping = iris$Species, x = iris[,-5], subset = subset, weights = rep(1:3, 50), na.action = na.fail))
	# check if na.omit works correctly
	fit1 <- wlda(grouping = iris$Species, x = iris[,-5], subset = subset, weights = rep(1:3, 50), na.action = na.omit)
	fit2 <- wlda(grouping = iris$Species, x = iris[,-5], subset = 11:60, weights = rep(1:3, 50))
	expect_equal(fit1[1:8], fit2[1:8])
	expect_equal(length(fit1$weights), 50)
})

#=================================================================================================================
context("predict.wlda")

test_that("predict.wlda works correctly with formula and data.frame interface and with missing newdata", {
	data(iris)
	ran <- sample(1:150,100)
	## formula, data
	fit <- wlda(formula = Species ~ ., data = iris, subset = ran)
  	pred <- predict(fit)
  	expect_equal(rownames(pred$posterior), rownames(iris)[ran])
	## formula, data, newdata
	fit <- wlda(formula = Species ~ ., data = iris, subset = ran)
  	predict(fit, newdata = iris[-ran,])
	## grouping, x
	fit <- wlda(x = iris[,-5], grouping = iris$Species, subset = ran)
  	pred <- predict(fit)
  	expect_equal(rownames(pred$posterior), rownames(iris)[ran])
	## grouping, x, newdata
	fit <- wlda(x = iris[,-5], grouping = iris$Species, subset = ran)
  	predict(fit, newdata = iris[-ran,-5])
})

test_that("predict.wlda: retrieving training data works", {
	data(iris)
	## no subset
	# formula, data
	fit <- wlda(formula = Species ~ ., data = iris)
  	pred1 <- predict(fit)
  	pred2 <- predict(fit, newdata = iris)
  	expect_equal(pred1, pred2)
	# y, x
	fit <- wlda(x = iris[,-5], grouping = iris$Species)
  	pred1 <- predict(fit)
  	pred2 <- predict(fit, newdata = iris[,-5])
  	expect_equal(pred1, pred2)
	## subset
	ran <- sample(1:150,100)
	# formula, data
	fit <- wlda(formula = Species ~ ., data = iris, subset = ran)
  	pred1 <- predict(fit)
  	pred2 <- predict(fit, newdata = iris[ran,])
  	expect_equal(pred1, pred2)
	# y, x
	fit <- wlda(x = iris[,-5], grouping = iris$Species, subset = ran)
  	pred1 <- predict(fit)
  	pred2 <- predict(fit, newdata = iris[ran,-5])
  	expect_equal(pred1, pred2)
})

test_that("predict.wlda works with missing classes in the training data", {
	data(iris)
	ran <- sample(1:150,100)
	expect_warning(fit <- wlda(Species ~ ., data = iris, subset = 1:100))
	pred <- predict(fit, newdata = iris[-ran,])
	expect_equal(nlevels(pred$class), 3)
	expect_equal(ncol(pred$posterior), 2)
	# a <- rep(0,50)
	# names(a) <- rownames(pred$posterior)
	# expect_equal(pred$posterior[,3], a)
})

test_that("predict.wlda works with one single predictor variable", {
	data(iris)
	ran <- sample(1:150,100)
	fit <- wlda(Species ~ Petal.Width, data = iris, subset = ran)
	expect_equal(ncol(fit$means), 1)
	expect_equal(dim(fit$cov), rep(1, 2))
	predict(fit, newdata = iris[-ran,])
})

test_that("predict.wlda works with one single test observation", {
	data(iris)
	ran <- sample(1:150,100)
	fit <- wlda(Species ~ Petal.Width, data = iris, subset = ran)
  	pred <- predict(fit, newdata = iris[5,])
	expect_equal(length(pred$class), 1)
	expect_equal(dim(pred$posterior), c(1, 3))
	a <- factor("setosa", levels = c("setosa", "versicolor", "virginica"))
	names(a) = "5"
	expect_equal(pred$class, a)
	pred <- predict(fit, newdata = iris[58,])
	expect_equal(length(pred$class), 1)
	expect_equal(dim(pred$posterior), c(1, 3))
	a <- factor("versicolor", levels = c("setosa", "versicolor", "virginica"))
	names(a) = "58"
	expect_equal(pred$class, a)
})

test_that("predict.wlda works with one single predictor variable and one single test observation", {
	data(iris)
	ran <- sample(1:150,100)
	fit <- wlda(Species ~ Petal.Width, data = iris, subset = ran)
	expect_equal(ncol(fit$means), 1)
	expect_equal(dim(fit$cov), rep(1, 2))
	pred <- predict(fit, newdata = iris[5,])
	expect_equal(length(pred$class), 1)
	expect_equal(dim(pred$posterior), c(1, 3))
})

test_that("predict.wlda: NA handling in newdata works", {
	data(iris)
	ran <- sample(1:150,100)
	irisna <- iris
	irisna[1:17,c(1,3)] <- NA
	fit <- wlda(Species ~ ., data = iris, subset = ran)
	expect_warning(pred <- predict(fit, newdata = irisna))
	expect_equal(all(is.na(pred$class[1:17])), TRUE)
	expect_equal(all(is.na(pred$posterior[1:17,])), TRUE)
})

test_that("predict.wlda: misspecified arguments", {
	data(iris)
	ran <- sample(1:150,100)
	fit <- wlda(Species ~ Petal.Width, data = iris, subset = ran)
    # errors in newdata
    expect_error(predict(fit, newdata = TRUE))
    expect_error(predict(fit, newdata = -50:50))
    # errors in prior
    expect_error(predict(fit, prior = rep(2,length(levels(iris$Species))), newdata = iris[-ran,]))
    expect_error(predict(fit, prior = TRUE, newdata = iris[-ran,]))
    expect_error(predict(fit, prior = 0.6, newdata = iris[-ran,]))
})


library(shiny)
library(crosstalk)
library(d3scatter)
library(detourr)
library(dplyr)

ui <- fluidPage(
  fluidRow(
    column(4, selectInput("species", "Species",
      levels(iris$Species),
      multiple = TRUE
    )),
    column(4, numericInput("seed", "Random Number Seed", "123")),
    column(4, sliderInput("max_bases", "Max. Bases", 3, 50, 10))
  ),
  fluidRow(
    column(4, d3scatterOutput("d3scatter", height = "400px")),

    # output function used matches display method (show_scatter) and tour
    # dimension (3d)
    column(8, displayScatter3dOutput("detour", height = "800px"))
  )
)

server <- function(input, output, session) {
  # updating data set based on user selection. This is different to linked
  # filtering in `demo/crosstalk.rmd` because a new tour is generated each time
  # the selection is changed.
  user_iris <- reactive({
    iris[is.null(input$species) | iris$Species %in% input$species, ]
  })

  shared_iris <- SharedData$new(user_iris)

  output$d3scatter <- renderD3scatter({
    d3scatter(
      shared_iris,
      ~Petal.Length, ~Petal.Width, ~Species
    )
  })

  output$detour <- shinyRenderDisplayScatter3d({
    set.seed(input$seed)

    detour(shared_iris, tour_aes(projection = -Species, colour = Species)) |>
      tour_path(grand_tour(3), max_bases = input$max_bases) |>
      show_scatter()
  })
}

shinyApp(ui, server)


#!/usr/bin/env Rscript
library(data.table)
library("psych")
library("softImpute")
merged<-read.delim("index/uniqueIndexed.SampleID.tsv",check.names=F)

merged<-merged[,-1]
f <- sapply(merged, is.factor)
flevels=sapply(merged[,which(f)],levels)
for(fac in which(f)){merged[,fac]=as.numeric(merged[,fac])}
mm<-as.matrix(merged)
fit1=softImpute(mm,rank=29,lambda=29,type="als",maxit=1000)
fmm=complete(mm,fit1)
a<-apply(fmm,2,var)
fmm<-fmm[,a!=0]
x=as.data.frame(fmm)
for(i in 1:ncol(flevels)){
	x[,colnames(flevels)[i]]=flevels[x[,colnames(flevels)[i]],i]
}
write.table(x,"index/uniqueIndexed.m.SampleID.tsv",sep="\t",row.names=F)
b<-as.data.frame(t(matrix(unlist(strsplit(colnames(fmm),"._.",fixed=T)),nrow=2)))
rownames(b)=colnames(fmm)
colnames(b)=c("category","names")
write.table(b,"index/uniqueIndexed.SampleID.name",sep="\t")

cats=levels(b$category)

dat <- data.frame(i=NULL,j=NULL,type=NULL,value=NULL,pvalue=NULL)
for(i in 1:(length(cats))){
	for(j in (i):length(cats)){
		print(j)
		ii=which(b$category==cats[i])
		jj=which(b$category==cats[j])
		cc=corr.test(fmm[,ii,drop=F],fmm[,jj,drop=F],method="spearman",adjust="fdr")
		re=-log10(cc$p)
		ind=which(re>=1 ,arr.ind=T)
		if(nrow(ind)>0){
			d=data.frame(i=ii[ind[,1]],j=jj[ind[,2]],type="cor",value=cc$r[ind],pvalue=re[ind])
			dat <- rbindlist(list(dat,d))
		}

	}
}
dat=dat[dat$i!=dat$j,]
dat$pvalue[dat$pvalue==Inf]=max(dat$pvalue[dat$pvalue!=Inf])+1
write.table(dat,"index/uniqueIndexed.SampleID.link",sep="\t",row.names=F)


#Read in file

#accept arguments
args<-commandArgs(TRUE)

#install package
require(RColorBrewer)
require(maps)
require(spam)
require(fields)

C <- as.matrix(read.table(args[1]))

e <- eigen(C)

e.var.per <- round(e$values/sum(e$values)*100, 2)

e_df <- as.data.frame(e$vectors, row.names = NULL)

write.table(e_df, file = paste0(c(args[1]),c(".tab")), sep = "\t", row.names = TRUE, col.names = NA)

#quit
q()


# Time "prediction" of a dummy set
# Used as control to find the overhead of raster::predict
# Also the RMSE values

library(raster)
source("utils/set-temp-path.r")
source("utils/load-data.r")
source("utils/accuracy-statistics.r")

TempDir = "../../userdata/temp/"
TrainingRasters = LoadTrainingRasters()
AllData = LoadClassificationData()
ValData = LoadValidationData()

# A 4-fold CV for static prediction is always the same
DummyData = ValData@data
DummyData[] = 100.0/9.0

AST = AccuracyStatTable(DummyData, ValData@data)
write.csv(AST, "../data/stat-dummy.csv")
write.csv(CalcErrors(DummyData, ValData@data), "../data/errors-stat-dummy.csv")

# For the pure-nonpure case
DummyData = ValData@data[!AllData@data$pure,]
DummyData[] = 100.0/9.0

AST = AccuracyStatTable(DummyData, ValData@data[!AllData@data$pure,])
write.csv(AST, "../data/stat-dummy-pure.csv")

dummypredict = function(model, blockvals, ...)
{
    return(matrix(100/9, nrow = nrow(blockvals), ncol = 9))
}

system.time(predicted <- predict(TrainingRasters, lm("1~1"), fun=dummypredict, index=1:9,
        filename=paste0(TempDir, "dummy.grd"), progress="text", overwrite=TRUE))


library(kernlab)
library(caret)
library(keras)
library(shiny)
# install_keras()

server <- shinyServer(function(input, output, section) {

  first <- reactive({
    switch(input$first,
           "Blue" = 1,
           "Red" = -1,
           "No" = 0)
  })

  dragon <- reactive({
    switch(input$dragon,
           "Blue" = 1,
           "Red" = -1,
           "No" = 0)
  })

  herald <- reactive({
    switch(input$herald,
           "Blue" = 1,
           "Red" = -1,
           "No" = 0)
  })
  # compose data frame
  new <- reactive({
    data.frame(
      Name = c("blueKills",
               "blueDeaths",
               "blueGoldDiff",
               "blueExperienceDiff",
               "WardsPlacedDiff",
               "WardsDestroyedDiff",
               "FirstBloodDiff",
               "blueAssistsDiff",
               "DragonsDiff",
               "HeraldsDiff",
               "AvgLevelDiff",
               "TotalJungleMinionsKilledDiff",
               "CSPerMinDiff"),

      Value = as.numeric(c(input$kill,
                           input$death,
                           input$gold,
                           input$exp,
                           input$ward_place,
                           input$ward_des,
                           first(),
                           input$assist,
                           dragon(),
                           herald(),
                           input$level,
                           input$jg,
                           input$cs)),

      stringsAsFactors=FALSE)
  })

  # ann.model <- load_model_tf("models/ann_model")
  load("knn_model.RData")
  # load("models/lgm_model.RData")
  load("svm_model.RData")
  load("res_model.RData")

  output$result <- renderPrint({
    test <- as.data.frame(predict(res.pca, newdata = data.frame("blueKills"=input$kill,
                                                                "blueDeaths"=input$death,
                                                                "blueGoldDiff"=input$gold,
                                                                "blueExperienceDiff"=input$exp,
                                                                "WardsPlacedDiff"=input$ward_place,
                                                                "WardsDestroyedDiff"=input$ward_des,
                                                                "FirstBloodDiff"=first(),
                                                                "blueAssistsDiff"=input$assist,
                                                                "DragonsDiff"=dragon(),
                                                                "HeraldsDiff"=herald(),
                                                                "AvgLevelDiff"=input$level,
                                                                "TotalJungleMinionsKilledDiff"=input$jg,
                                                                "CSPerMinDiff"=input$cs
    )))

    knn.predict <- predict(knn.model, newdata = test)
    # glm.predict = predict(lgm.model, newdata = test)
    svm.predict <- predict(svm.model, test)

    # ann.predict <- predict_classes(object = ann.model, x = as.matrix(test[,1:13])) %>%
    # as.vector()

    models <- c(knn.predict,svm.predict)
    uniqv <- unique(models)
    vote <- uniqv[which.max(tabulate(match(models, uniqv)))]

    if(vote == 2) { winner = "Blue team is the winner based on the first ten minutes data (accuracy is about 70 percent)" }
    if(vote == 1) { winner = "Red team is the winner based on the first ten minutes data (accuracy is about 70 percent)" }

    return(winner)
  })
  # show the result
  output$table <- renderDataTable({
    new()
  })

})


require(lattice)
cellIDset = c(56, 70, 30, 20)
##cellIDset = NULL
edgesOld <- read.csv(file=paste("cell_edges", 0, ".txt", sep=''), header=FALSE)
ellipsesOld <- read.csv(file=paste("ellipses", 0, ".txt", sep=''), header=FALSE)
r0 <- 1.53

ratioAll <- NULL

##for (file_index in seq(170, 172) )
for (file_index in seq(0,10100,by=1) )
{
    cat(file_index, "\n")
    nodes <- read.csv(file=paste("cell_nodes", file_index, ".txt", sep=''), header=FALSE)
    edges <- read.csv(file=paste("cell_edges", file_index, ".txt", sep=''), header=FALSE)
    ##force <- read.csv(file=paste("force", file_index, ".txt", sep=''), header=FALSE)
    ##force2 <- read.csv(file=paste("force2", file_index, ".txt", sep=''), header=FALSE)
    ellipses <- read.csv(file=paste("ellipses", file_index, ".txt", sep=''), header=FALSE)
    ##sumforce <- read.csv(file=paste("sum_force", file_index, ".txt", sep=''), header=FALSE)
    ##eigenshape <- read.csv(file=paste("eigen", file_index, ".txt", sep=''), header=FALSE)
    ##subset <- force[,1] %in% cellIDset
    ##subset2 <- sumforce[,1] %in% cellIDset
    ##eigensubset <- eigenshape[,1]==1
    ##eigensubset <- eigenshape[,1]>0

    type <- ellipses[,12]
    ratio <- ellipses[,8]/ellipses[,9]
    ratioAll <- c(ratioAll, sum(ratio>r0)/length(ratio))

    ## png(paste("ar", file_index, ".png", sep=''), height=600, width=800)
    ## ##pdf(paste("ar", file_index, ".pdf", sep=''), height=7, width=7)
    ## par(mar=c(5,6,5,5))
    ## ## hist(ellipses[type<10,6]/ellipses[type<10,7], breaks=15, freq=FALSE,
    ## ##      xlim=c(1,2.6),
    ## ##      xlab="aspect ratio", ylab="frequency", cex.lab=2, cex.axis=1.5,
    ## ##      ##main=paste("mean", mean(ratio), "var", var(ratio)))
    ## ##      main="")
    ## ar <- ellipses[type<10,6]/ellipses[type<10,7]
    ## ##ar <- ar + rnorm(length(ar), 0, 0.1)
    ## F <- ecdf(ar)
    ## plot(F, xlim=c(1,2.6))
    ## abline(v=1.53, col='red', lwd=4, lty=2)
    ## abline(h=sum(ar<1.53)/length(ar), col='blue', lwd=4, lty=2)
    ## ##cat(file="meanar.txt", mean(ellipses[,6]/ellipses[,7]), '\n', append=T)
    ## dev.off()

    padded_figure_index <- sprintf("%05d", file_index)

    png(paste("plot", padded_figure_index, ".png", sep=''),
        height=600, width=1200)
    par(mfrow=c(1,2))
    ##pdf(paste("plot", file_index, ".pdf", sep=''),
    ##    height=6, width=8)
    plot(0, 0, type='n', xlim=c(-30, 30), ylim=c(-30, 30), asp=1, axes=FALSE,
         ##main=paste("p1 =", format(round(p0, 2), nsmall = 2))
         ##main="", cex.lab=1.5, cex.axis=1.5, xlab="longitudinal direction", ylab="circumferential direction")
         main="", cex.lab=1.5, cex.axis=1.5, xlab="", ylab="")
    ##points(nodes[,2], nodes[,3])
    ##segments(edgesOld[, 4], edgesOld[, 5], edgesOld[, 6], edgesOld[, 7], col='gray')
    segments(edges[, 4], edges[, 5], edges[, 6], edges[, 7], col='black')

    ## arrows(force[subset,2], force[subset,3], force[subset,4], force[subset,5],
    ##        length=0.1, col='blue')
    ## arrows(force2[subset,2], force2[subset,3], force2[subset,4], force2[subset,5],
    ##        length=0.1, col='red')
    ## arrows(sumforce[subset2,2], sumforce[subset2,3], sumforce[subset2,4], sumforce[subset2,5], length=0.1, col='red')

    ##eigensubset <- eigenshape[,1]==1
    ##eigensubset <- eigenshape[,1]>0
    ##points(ellipses[ratio>r0, 2:3], col='gold', pch=16, cex=2)
    ##points(ellipses[ratio<=r0, 2:3], col='blue', pch=16, cex=2)
    points(ellipses[(type<10) & (ratio>r0), 2:3], col='darkgray', pch=1, cex=1)
    points(ellipses[(type<10) & (ratio<=r0), 2:3], col='black', pch=16, cex=0.75)
    points(ellipses[type>=10, 2:3], col='red', pch=16, cex=0.75)
    ## arrows(eigenshape[eigensubset,2],
    ##        eigenshape[eigensubset,3],
    ##        eigenshape[eigensubset,6],
    ##        eigenshape[eigensubset,7], length=0.1, col='blue')
    ## arrows(eigenshape[eigensubset,2],
    ##        eigenshape[eigensubset,3],
    ##        eigenshape[eigensubset,8],
    ##        eigenshape[eigensubset,9], length=0.1, col='red')
    arrows(ellipses[ratio>r0,2],
           ellipses[ratio>r0,3],
           ellipses[ratio>r0,10],
           ellipses[ratio>r0,11], length=0.1, col='gold')
    arrows(ellipses[ratio<=r0,2],
           ellipses[ratio<=r0,3],
           ellipses[ratio<=r0,10],
           ellipses[ratio<=r0,11], length=0.1, col='blue')
    ##abline(h = c(Hmin, Hmax))
    ##abline(v = c(Lmin, Lmax))
    par(mar=c(14, 6, 12, 8))
    plot(ratioAll, col='black', xlim=c(0, 2000), ylim=c(0,1),
         xlab="time", ylab="percentage of elongated cells", cex.axis=1.5, cex.lab=1.5, type='l', lwd=2)
    abline(h=c(0.2, 0.4, 0.6, 0.8), lty=4, col='gray')
    points(file_index+1, ratioAll[file_index+1], col='red', cex=1.5, pch=16)
    dev.off()
}


# Functions used in tf2.r!
cat('Loaded functions!\n')

# note! right now this only works for one factor...
get_motif<-function(factor){
    pfm <- read.table('data/CTCF.NGCGCCMYCTAGYGGTN.pwm',skip=2)
    # including some pseudo-counts... maybe unnecessary?
    pseudo_pfm <- matrix(rep(1,prod(dim(pfm))),dim(pfm))
    adj_pfm <- pfm+pseudo_pfm
    # note taking a transpose!
    pwm <- t(adj_pfm/rowSums(adj_pfm))
        return(pwm)
}

is_bound<-function(probs){
    success <- runif(length(probs))<probs
    # a single success is sufficient for binding!
    bound_yn <- ifelse((sum(success)==0),0,1)
    return(bound_yn)
}

include_pwm<-function(hmm,pwm,N_STATES){
    # assume background and generic states have equal probs
    set.emission.params.qhmm(hmm, c(1,N_STATES), rep(0.25,4), slot=1, fixed=rep(T,4))
    # the pwm is just a matrix, whose columns refer to positions and rows refer to ACGT
    for (state in 2:(N_STATES-1)){
        set.emission.params.qhmm(hmm, state, pwm[,(state-1)], slot=1, fixed=rep(T,4))
    }
    return(hmm)
}

# coincidence matrix is one row per transcription factor, first column is 'F's CDF given no phi', second is 'F's CDF with phi' approximately speaking...
get_interactions<-function(factor,binding_status){
    # binding of factor of interest
    reference<-binding_status[,factor]
    # all the others
    rest_mat<-binding_status[,!colnames(binding_status)==factor]
    # if they're both bound, we see 2!
    comp_mat<-rest_mat+reference
    # if other isn't bound, but ref is, we see -1!
    cont_mat<-rest_mat-reference
    # how often do we see both, given we saw 'other'?
    if_yes<-colSums(comp_mat==2)/colSums(rest_mat==1)
    # how often do we see ref and not other, given no other?
    if_no<-colSums(cont_mat==-1)/colSums(rest_mat==0)
    # turn this into a matrix, and pass it back
    coincidence_mat<-matrix(c(if_no,if_yes),nrow=(N_FACTORS-1),ncol=2)
    # remove the NaNs... they should be zeroes anyway!
    coincidence_mat[is.nan(coincidence_mat)]<-0
    return(coincidence_mat)
}

get_C_int<-function(peak,peak_length,factor,factor_size,binding_status,coincidence){
    # get the peak-specific bound status, excluding current factor
    peak_bound_status<-as.numeric(binding_status[peak,!colnames(binding_status)==factor])
    # translate this into a matrix of indices to query the coincidence matrix - the query will produce a vector of the relevant elements of the coincidence matrix depending on whether or not the factor is actully bound in this peak!
    relevant_indices<-matrix(c(seq(N_FACTORS-1),peak_bound_status+1),(N_FACTORS-1),2)
    # some normalisation, sum over aforementioned vector...
    C_int <- (1.0/((peak_length-factor_size)*(N_FACTORS-1)))*sum(coincidence[relevant_indices])
    return(C_int)
}

visualise_binding<-function(binding_status){
    print(binding_status)
}

build_hmm<-function(N_STATES,N_FEATURES,pwm){
    # no covariates!
    data_shape <- list(rep(1,N_FEATURES+1),NULL)

    # construct the valid transitions! this is a little tricky because the number of states depends on the transcription factor
    #valid_transitions <- matrix(rep(0,N_STATES*N_STATES),nrow=N_STATES,ncol=N_STATES,byrow=T)
    valid_transitions <- matrix(c(rep(1,N_STATES),rep(0,N_STATES*(N_STATES-1))),nrow=N_STATES,ncol=N_STATES,byrow=F)
    valid_transitions[1,] <- c(1,2,rep(0,(N_STATES-3)),3)
    #valid_transitions[2:(N_STATES-2),3:(N_STATES-1)] <- diag(N_STATES-3)
    valid_transitions[2:(N_STATES-2),3:(N_STATES-1)] <- 2*diag(N_STATES-3)
    valid_transitions[(N_STATES-1),] <- c(1,rep(0,(N_STATES-1)))
    valid_transitions[N_STATES,] <- c(1, rep(0,(N_STATES-2)), 2)
    # everything is discrete here!
    transition_functions <- rep("discrete",N_STATES)
    # noo, a for loop!
    emission_functions <- vector(mode="list",length=N_STATES)
    for (state in 1:N_STATES){
        emission_functions[[state]]<-rep("discrete",(N_FEATURES+1))
    }
    # create the HMM
    hmm <- new.qhmm(data_shape,valid_transitions,transition_functions,emission_functions,support.missing=TRUE)

    # set the fixed parameters
    #set.transition.params.qhmm(hmm,2:(N_STATES-1),1,fixed=T)
    set.transition.params.qhmm(hmm,2:(N_STATES-2),c(0.01,0.99),fixed=c(TRUE,TRUE))
    set.transition.params.qhmm(hmm,N_STATES-1,1,fixed=T)
    # include the pwm info!
    hmm <- include_pwm(hmm,pwm,N_STATES)
    return(hmm)
}

initialise_hmm<-function(hmm,N_STATES,N_FEATURES,transition_params,emission_params){
    # initialise it! ... always start in the background state!
    set.initial.probs.qhmm(hmm,c(1,rep(0,(N_STATES-1))))

    set.transition.params.qhmm(hmm,1,transition_params$B)
    set.transition.params.qhmm(hmm,N_STATES,transition_params$G)

    # emissions (DNAse)
    for (state in 1:N_STATES){
        for (feature in 1:N_FEATURES){
            theta <- emission_params[state,feature]
            if(theta==1|theta==0){
                print("we're about to get an emission probability of zero!")
                }
           set.emission.params.qhmm(hmm, state, c(1-theta,theta),slot=(feature+1))
            }
        }
    return(hmm)
}

get_emission_prob<-function(hmm,peak_data,peak_length,state,N_FEATURES){
    probs<-rep(1,(peak_length-1))
    pre_probs<-rep(1,(peak_length-1))
    for (slot_num in 1:(N_FEATURES+1)){
        emission_probs<-get.emission.params.qhmm(hmm,state,slot=slot_num)
        # this line might look a bit weird, but the value in the peak_data is actually the label found in the hmm
        # also note: we're actually only interested in the probs from 2... end... see the mathematical form!
        states<-peak_data[slot_num,2:peak_length]
        # missing data is uninformative
        pre_probs[states==0]<-1
        pre_probs[!states==0]<-emission_probs[states[!states==0]]
        probs<-probs*pre_probs
#        probs<-probs*emission_probs[peak_data[slot_num,2:peak_length]]
    }
    return(probs)
}

get_new_transition_params <- function(peak,peak_length,factor,factor_size,binding_status,coincidence,xis,TAU){
    # get the interaction modifier
    C_int <- get_C_int(peak,peak_length,factor,factor_size,binding_status,coincidence)

    norm_B <- xis$"xi_BB"+xis$"xi_BG"
    norm_G <- xis$"xi_GB"+xis$"xi_GG"

    a_BB <- (1-TAU-C_int)*xis$"xi_BB"/norm_B
    a_BF <- TAU + C_int
    a_BG <- (1-TAU-C_int)*xis$"xi_BG"/norm_B
    a_GB <- xis$"xi_GB"/norm_G
    a_GG <- xis$"xi_GG"/norm_G

    return(list(B=c(a_BB,a_BF,a_BG),G=c(a_GB,a_GG)))
}

# this just displays the likelihoods
visualise_probs <- function(gamma,peak,factor,N_STATES){
    rb<-rainbow(N_STATES*3,s=0.9,v=0.9)
    plot(gamma[1,],type="p",col="black",pch=20,cex=0.8,ylab="Posterior",xlab="Location in peak",ylim=c(0,1))
    abline(h=0.05,col="snow3",lty=3)
    for(s in seq(2,N_STATES-1)){
        lines(gamma[s,],type="p",col=ifelse(gamma[s,]>0.05,rb[s+N_STATES],"snow2"),pch=20)
    }
    lines(gamma[N_STATES,],type="p",col="sienna1",pch=20,cex=0.8)
    browser()
}

get_theta <- function(gamma,data,N_STATES,N_FEATURES){
    theta_d <- rowSums(gamma)
    theta_n <- matrix(NA,nrow=N_STATES,ncol=N_FEATURES)
    # theta_numer is more complicated... for each location in the peak we have a matrix: the rows correspond to STATES and the columns correspond to the DNase emissions!
    for (feature in 1:N_FEATURES){
        # remember, ignoring the first emission (DNA sequence)
        X <- data[(feature+1),]-1
        for (state in 1:N_STATES){
            theta_n[state,feature]<-sum(gamma[state,]*X)
            if (is.na(theta_n[state,feature])){
                print(gamma[state,])
                print(X)
                browser()
                }
            if (theta_n[state,feature]==theta_d[state]){
                # assume this is underflow... this is an ad hoc solution, i'm sorry :(
                theta_n[state,feature]<-0.95*theta_n[state,feature]+(0.05)*(1-theta_n[state,feature])
            }
        }
    }
# this is the 'smart' way of doing it... (without those middle 2 for loops)
# theta_numer <- theta_numer + gamma[,loc]%o%(peak_data[2:(N_FEATURES+1),loc]-1)
    return(list("theta_numer"=theta_n,"theta_denom"=theta_d))
}

get_xi <- function(hmm,alpha_prime,beta_prime,loglik,peak_data,N_STATES,N_FEATURES,peak_length){
    # get emission probabilities (this is the likelihood of the data given diff states, for each loc)
    emissions_B <- get_emission_prob(hmm,peak_data,peak_length,1,N_FEATURES)
    emissions_G <- get_emission_prob(hmm,peak_data,peak_length,N_STATES,N_FEATURES)
    # get transition probabilities
    a_BB <- get.transition.params.qhmm(hmm,1)[1]
    # remember, B can only transition to B,F,G
    a_BG <- get.transition.params.qhmm(hmm,1)[3]
    # G can only transition to B,G
    a_GB <- get.transition.params.qhmm(hmm,N_STATES)[1]
    a_GG <- get.transition.params.qhmm(hmm,N_STATES)[2]

    # the B state is the first row! G is final...
    xi_BB <- sum(exp(alpha_prime[1,1:(peak_length-1)]+beta_prime[1,2:peak_length]+log(emissions_B)+log(a_BB)-loglik))
    xi_BG <- sum(exp(alpha_prime[1,1:(peak_length-1)]+beta_prime[N_STATES,2:peak_length]+log(emissions_G)+log(a_BG)-loglik))
    xi_GB <- sum(exp(alpha_prime[N_STATES,1:(peak_length-1)]+beta_prime[1,2:peak_length]+log(emissions_B)+log(a_GB)-loglik))
    xi_GG <- sum(exp(alpha_prime[N_STATES,1:(peak_length-1)]+beta_prime[N_STATES,2:peak_length]+log(emissions_G)+log(a_GG)-loglik))

    return(list("xi_BB"=xi_BB,"xi_BG"=xi_BG,"xi_GB"=xi_GB,"xi_GG"=xi_GG))
}

get_confusion_matrix <- function(pred,known,FACTORS){
    cm <- vector("list")
    for (factor in FACTORS){
        pos<-which(known[,factor]==1)
        neg<-which(known[,factor]==0)
        TP<-sum(pred[pos,factor]==1)
        FP<-sum(pred[neg,factor]==1)
        TN<-sum(pred[neg,factor]==0)
        FN<-sum(pred[pos,factor]==0)
        cm[[factor]]<-list("TP"=TP,"FP"=FP,"TN"=TN,"FN"=FN,"sens"=100*TP/(TP+FN),"spec"=100*TN/(FP+TN))
        }
    return(cm)
}

eval_peak <- function(peak,factor_hmm,data,trans_params,emiss_params,N_STATES,N_FEATURES,factor,factor_size,binding_status,coincidence,TAU){
    if (peak%%10000==0){
        print(peak)
    }

    peak_data <- data[[peak]]
    missing_data <- (peak_data==0)*1
    peak_length <- ncol(peak_data)

    # initialise parameters
    if (is.null(trans_params[[peak]])){
#        cat("No transition parameters saved for ",factor," - making some up!\n")
        trans_params[[peak]] <- list(B=c(0.4,0.2,0.4),G=c(0.5,0.5))
    }

    # initialise hmm
    if (sum(unlist(trans_params[[peak]])==0)>0){
        print("PROBLEM AHOY!")
        print(trans_params[[peak]])
    }

    peak_hmm <- initialise_hmm(factor_hmm,N_STATES,N_FEATURES,trans_params[[peak]],emiss_params)

    # get new parameters
    # rows are states, cols are locations
    alpha_prime <- forward.qhmm(peak_hmm,peak_data,missing=missing_data)
    beta_prime <- backward.qhmm(peak_hmm,peak_data,missing=missing_data)
    loglik <- attr(alpha_prime,"loglik")

    # emissions
    gamma <- exp(alpha_prime + beta_prime - loglik)
    theta <- get_theta(gamma,peak_data,N_STATES,N_FEATURES)

    # transitions
    xis <- get_xi(peak_hmm,alpha_prime,beta_prime,loglik,peak_data,N_STATES,N_FEATURES,peak_length)
    new_trans_params <- get_new_transition_params(peak,peak_length,factor,factor_size,binding_status,coincidence,xis,TAU)
    # check if bound (the result of this only makes sense after EM has converged)
    visualise_probs(gamma,peak,factor,N_STATES)
    posteriors <- gamma[2,]
    bound_yn <- is_bound(posteriors)

    # values to return
    return_vals <- list("theta"=theta,"new_trans_params"=new_trans_params,"loglik"=loglik,"bound"=bound_yn)
}


'

In the file "first_stage_estimation.r", we regress the expropriation risk
in the country on log mortality.

The file requires to be called with a model specification as the argument,
a corresponding json-file must exist in PATH_IN_MODEL_SPECS. That file
needs to define a dictionary with keys:

    * INSTD - the dependent variable (in the first stage)
    * INSTS - the instrument
    * KEEP_CONDITION - any sampling restrictions
    * DUMMIES - additional dummy variables to be used as controls

The r-file loops over various specifications with geographic controls /
restrictions as defined in PATH_IN_MODEL_SPECS/geography.json. Finally,
it stores a dataframe with estimation results.

'

rm(list=ls())
options(digits=3)

args = commandArgs(trailingOnly=TRUE)

source(args[1])

# Load required libraries
library(foreign)
library(car)
library(zoo)
library(rjson)
library(sandwich)
library(lmtest)
library(aod)

# Load model and geographic specification.
model <- fromJSON(file=args[2])
geography <- fromJSON(file=args[3])

# Initilize output dataframe for results.
results = data.frame(matrix(nrow = 7, ncol = 7))
row.names(results) <- c(
    "Log mortality($\\beta$)",
    "~~ \\{homoscedastic standard errors\\}",
    "~~ (heteroscedastic standard errors)",
    "~~ (heteroscedastic-clustered SE)",
    "p-value of log mortality",
    "p-value of indicators",
    "p-value of controls"
)

# Loop over geographical specifications.
for (i in 1:7) {

    # Load data
    data <- read.csv(file=args[4], header=TRUE)

    # Implement model-specific restrictions.
    if (model$KEEP_CONDITION != "") {
        data <- subset(data, eval(parse(text = model$KEEP_CONDITION)))
    }

    # Implement geographical constraints.
    GEO_COND <- paste("GEO_KEEP_CONDITION_", i, sep="")
    if (geography[[GEO_COND]] != "") {
        data <- subset(data, eval(parse(text = geography[[GEO_COND]])))
    }
    GEO_CONTROLS <- paste("GEO_CONTROLS_", i, sep="")

    # Set up variables for regression.
    y <- model$INSTD
    x <- model$INSTS
    dummies <- model$DUMMIES
    geo_controls <- geography[[GEO_CONTROLS]]

    # Set up regression formula.
    reg_formula <- as.formula(paste(y, " ~ ", x, dummies, geo_controls, sep=""))
    reg <- lm(reg_formula, data)

    # Store regression output that is the same across models.
    results[1, i] = reg$coef[[2]]
    results[2, i] = sqrt(diag(vcov(reg))[2])
    results[3, i] = summaryw(reg)[[1]][2,2]
    results[4, i] = clx(fm = reg, dfcw = 1, cluster = data[ ,x])[[1]][2,2]

    # p-value of log mortality, based on the appropriate standard errors.
    if (model$MODEL_NAME == "baseline" | model$MODEL_NAME == "addindic") {
        results[5, i] = clx(fm = reg, dfcw = 1, cluster = data[ ,x])[[1]][2,4]
    } else {
        results[5, i] = summaryw(reg)[[1]][2,4]
    }

    # p-value of indicators, based on the appropriate standard errors
    if (model$MODEL_NAME == "baseline" | model$MODEL_NAME == "addindic")  {
        sigma = clx(fm = reg, dfcw = 1, cluster = data[ ,x])[[2]]
        if (model$MODEL_NAME == "addindic") {
            results[6, i] = wald.test(
                b = reg$coef,
                Sigma = sigma,
                Terms = 3:4,
                df = reg$df
            )[[6]][[2]][4]
        }
    } else {
        sigma = summaryw(reg)[[2]]
        if (model$MODEL_NAME == "rmconj_addindic" | model$MODEL_NAME == "newdata") {
            results[6, i] = wald.test(
                b = reg$coef,
                Sigma = sigma,
                Terms = 3:4,
                df = reg$df
            )[[6]][[2]][4]
        }
    }

    # p-value of geographic controls, based on length of model and appropriate standard errors
    if (i != 1 & i != 3) {
        if (dummies == "") {
            terms = 3:length(reg$coef)
        } else {
            terms = 5:length(reg$coef)
        }
        results[7, i] = wald.test(
            b = reg$coef,
            Sigma = sigma,
            Terms = terms,
            df = reg$df
        )[[6]][[2]][4]
    }
}

# Save data to disk.
write.csv(results, file=args[5], col.names=TRUE)


#Usage:
#Rscript cell_manual_differential.r gene_list gene_class cell_list cell_class
library("monocle")
library("reshape")
library("ggplot2")
library("pheatmap")
args <- commandArgs(trailingOnly=TRUE)
if(length(args)!=4)
{
	print("Usage: Rscript cell_manual_differential.r gene_list gene_class cell_list cell_class")
}
HSMM <- readRDS("post_quality_control_data.rds")
HSMM <- HSMM[, strtoi(unlist(strsplit(args[3],",")))]
gene_list <-  unlist(strsplit(args[1],","))
gene_class <- unlist(strsplit(args[2],","))
cell_class <- unlist(strsplit(args[4],","))
saveRDS(gene_list,'gene_manual_list.rds')
saveRDS(gene_class,'gene_manual_class.rds')
newHSMM <- HSMM[gene_list,]
expression_matrix <- exprs(newHSMM)
colnames(expression_matrix) <- cell_class
sorted_matrix <- expression_matrix[order(gene_class),order(colnames(expression_matrix))]
Cell_Type <- colnames(sorted_matrix)
colnames(sorted_matrix) <- 1:length(colnames(sorted_matrix))
annotation_col_data <- data.frame(Cell_Type)
annotation_row_data <- data.frame(gene_class[order(gene_class)])
rownames(annotation_row_data) <- rownames(sorted_matrix)
names(annotation_row_data) <- "Gene Class"
names(annotation_col_data) <- "Cell Type"
log_matrix <- log(sorted_matrix+1)
png('cell_manual_heatmap.png',width = 15, height = 15, units= 'in',res=600)
pheatmap(log_matrix, annotation_col=annotation_col_data,annotation_row=annotation_row_data,cluster_cols = FALSE,cluster_rows = FALSE,show_colnames = FALSE)
dev.off()


library(plyr)
library(ggplot2)
library(RColorBrewer)
library(grid)
library(scales)
library(changepoint)

setwd(dirname(sys.frame(1)$ofile))
theme_update(plot.margin = unit(c(0,0,0,0), "cm"))

source("common.r")

datafiles = getdatafiles(c("data"), "*.csv")

if (!exists("indata")) {
  indata = readdata(datafiles)
  badposes = laply(indata, function(x) {nrow(x)})
  badfiles = datafiles$fn[badposes == 0]

  #check that everything has the same start time - it doesn't.
  #starts = laply(indata, function(x) { as.numeric(x$t[1]) })
}

for (idxtest in c(seq(2,50000,1000))) {
  toplot = indata[[idxtest]]
  if (FALSE) {
    #investigate the selected data a bit
    ggplot(toplot, aes(x=t, y=v)) + geom_line()
    bydx = data.frame(
      x=seq(1,nrow(toplot),1),
      y=toplot$v
    )
    ggplot(bydx, aes(x, y)) + geom_line()
  }

  cpts = findcps(toplot)

  ggplot(toplot, aes(x=t, y=v)) + geom_line() +
    geom_vline(xintercept=as.numeric(cpts$t), linetype=4)
  ggsave(paste("figs/", idxtest,
             gsub("%20", " ", gsub("/", "-", datafiles$fn[idxtest], fixed=TRUE)),
             "png", sep="."), width=5, height=4)
}


> md<-aov(MAD~condition,
+         data=avg_measures.0b2)
> summary(md)
             Df   Sum Sq Mean Sq F value  Pr(>F)
condition     5  1070366  214073   3.762 0.00266 **
Residuals   248 14110495   56897
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> TukeyHSD(md,"condition")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = MAD ~ condition, data = avg_measures.0b2)

$condition
                       diff         lwr       upr     p adj
antiDPP-antiCH   -77.848063 -212.863242  57.16712 0.5622124
antiKMT-antiCH     7.194692 -129.150975 143.54036 0.9999888
antiUS-antiCH   -147.609140 -284.655534 -10.56275 0.0265033
proCH-antiCH      30.979439 -134.111921 196.07080 0.9945072
proUs-antiCH      23.929734 -141.161626 189.02109 0.9983877
antiKMT-antiDPP   85.042755  -50.652341 220.73785 0.4676144
antiUS-antiDPP   -69.761076 -206.160243  66.63809 0.6843819
proCH-antiDPP    108.827502  -55.726973 273.38198 0.4049024
proUs-antiDPP    101.777797  -62.776678 266.33227 0.4828948
antiUS-antiKMT  -154.803831 -292.520116 -17.08755 0.0175330
proCH-antiKMT     23.784747 -141.863126 189.43262 0.9984593
proUs-antiKMT     16.735042 -148.912831 182.38291 0.9997212
proCH-antiUS     178.588578   12.363456 344.81370 0.0271230
proUs-antiUS     171.538873    5.313751 337.76400 0.0386804
proUs-proCH       -7.049705 -197.060846 182.96144 0.9999981

> tapply(avg_measures.0b2$MAD, avg_measures.0b2$condition, summary)
$antiCH
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
-341.46 -104.12    0.00   97.47  261.98  895.90

$antiDPP
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max.
-469.277 -107.187   -8.377   19.623  174.136  453.900

$antiKMT
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
-681.42  -89.57   21.86  104.67  301.69 1187.74

$antiUS
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
-495.45 -187.05  -45.90  -50.14   64.46  584.79

$proCH
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
-245.65   26.74  129.88  128.45  223.87  446.43

$proUs
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
-271.61  -24.41   98.62  121.40  252.72  648.51
md<-aov(MAD~condition*ifTW*des,
+         data=avg_measures.0b2)
> summary(md)
                    Df   Sum Sq Mean Sq F value  Pr(>F)
condition            5   994949  198990   3.170 0.00895 **
ifTW                 2    28367   14183   0.226 0.79796
des                  1     2354    2354   0.038 0.84663
condition:ifTW      10   448088   44809   0.714 0.71085
condition:des        3   206214   68738   1.095 0.35239
ifTW:des             2   485260  242630   3.865 0.02259 *
condition:ifTW:des   6   164331   27389   0.436 0.85397
Residuals          194 12177258   62769
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
30 observations deleted due to missingness
> TukeyHSD(md,"ifTW:des")
  Tukey multiple comparisons of means
    95% family-wise confidence level

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

$`ifTW:des`
               diff        lwr       upr     p adj
1:0-0:0   29.101809 -270.65868 328.86230 0.9999978
2:0-0:0   18.650004 -306.77607 344.07608 1.0000000
0:1-0:0  -26.976353 -334.04321 280.09050 0.9999990
1:1-0:0  -14.693025 -291.25201 261.86596 1.0000000
2:1-0:0   96.243689 -196.22227 388.70965 0.9822689
0:2-0:0   26.574969 -280.49189 333.64183 0.9999991
1:2-0:0   70.665533 -204.68206 346.01313 0.9966195
2:2-0:0  -53.539023 -343.08900 236.01095 0.9996834
2:0-1:0  -10.451805 -279.16354 258.25993 1.0000000
0:1-1:0  -56.078163 -302.23649 190.08017 0.9985431
1:1-1:0  -43.794834 -250.64923 163.05956 0.9991418
2:1-1:0   67.141879 -160.54256 294.82632 0.9913011
0:2-1:0   -2.526840 -248.68517 243.63149 1.0000000
1:2-1:0   41.563723 -163.66826 246.79570 0.9993796
2:2-1:0  -82.640832 -306.56728 141.28562 0.9641195
0:1-2:0  -45.626357 -322.46512 231.21240 0.9998664
1:1-2:0  -33.343029 -275.90130 209.21525 0.9999665
2:1-2:0   77.593685 -182.95570 338.14307 0.9907145
0:2-2:0    7.924965 -268.91380 284.76373 1.0000000
1:2-2:0   52.015529 -189.16064 293.19169 0.9990182
2:2-2:0  -72.189027 -329.46093 185.08288 0.9937651
1:1-0:1   12.283329 -205.02392 229.59058 1.0000000
2:1-0:1  123.220042 -114.00117 360.44126 0.7874420
0:2-0:1   53.551323 -201.45382 308.55647 0.9991912
1:2-0:1   97.641886 -118.12157 313.40534 0.8890761
2:2-0:1  -26.562670 -260.17936 207.05402 0.9999923
2:1-1:1  110.936713  -85.19774 307.07116 0.6989259
0:2-1:1   41.267994 -176.03926 258.57524 0.9996139
1:2-1:1   85.358557  -84.19581 254.91293 0.8149487
2:2-1:1  -38.845998 -230.60515 152.91316 0.9993783
0:2-2:1  -69.668719 -306.88994 167.55250 0.9915343
1:2-2:1  -25.578156 -220.00076 168.84445 0.9999761
2:2-2:1 -149.782712 -363.84587  64.28045 0.4130380
1:2-0:2   44.090563 -171.67289 259.85402 0.9993374
2:2-0:2  -80.113992 -313.73068 153.50269 0.9770376
2:2-1:2 -124.204556 -314.21245  65.80334 0.5101730

md<-aov(MAD~condition*ifTW*sys*des,
+         data=avg_measures.0b2)
> summary(md)
                        Df   Sum Sq Mean Sq F value  Pr(>F)
condition                5   994949  198990   3.245 0.00793 **
ifTW                     2    28367   14183   0.231 0.79374
sys                      1      449     449   0.007 0.93187
des                      1     2366    2366   0.039 0.84449
condition:ifTW          10   448019   44802   0.731 0.69491
condition:sys            5   140539   28108   0.458 0.80678
ifTW:sys                 1   145074  145074   2.366 0.12583
condition:des            3   210207   70069   1.143 0.33336
ifTW:des                 2   495345  247673   4.039 0.01929 *
sys:des                  1   134470  134470   2.193 0.14045
condition:ifTW:sys       5   323742   64748   1.056 0.38663
condition:ifTW:des       6   171438   28573   0.466 0.83285
condition:sys:des        3   335568  111856   1.824 0.14452
ifTW:sys:des             1    80092   80092   1.306 0.25466
condition:ifTW:sys:des   3   326778  108926   1.776 0.15344
Residuals              174 10669417   61318
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
30 observations deleted due to missingness
> TukeyHSD(md,"ifTW:des")
  Tukey multiple comparisons of means
    95% family-wise confidence level

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

$`ifTW:des`
               diff        lwr       upr     p adj
1:0-0:0   29.101677 -267.56098 325.76434 0.9999975
2:0-0:0   18.649371 -303.41363 340.71237 1.0000000
0:1-0:0  -27.342726 -331.23624 276.55079 0.9999987
1:1-0:0  -15.056603 -288.75753 258.64432 1.0000000
2:1-0:0   97.066853 -192.37666 386.51036 0.9797904
0:2-0:0   26.940695 -276.95282 330.83421 0.9999989
1:2-0:0   71.012022 -201.49003 343.51407 0.9961896
2:2-0:0  -54.303695 -340.86135 232.25396 0.9996141
2:0-1:0  -10.452306 -276.38708 255.48247 1.0000000
0:1-1:0  -56.444402 -300.05884 187.17004 0.9983320
1:1-1:0  -44.158279 -248.87496 160.55840 0.9990026
2:1-1:0   67.965176 -157.36629 293.29664 0.9897729
0:2-1:0   -2.160982 -245.77542 241.45346 1.0000000
1:2-1:0   41.910345 -161.20069 245.02138 0.9992778
2:2-1:0  -83.405372 -305.01768 138.20694 0.9592883
0:1-2:0  -45.992097 -319.96991 227.98571 0.9998441
1:1-2:0  -33.705973 -273.75756 206.34562 0.9999599
2:1-2:0   78.417482 -179.43929 336.27425 0.9892060
0:2-2:0    8.291324 -265.68648 282.26913 1.0000000
1:2-2:0   52.362651 -186.32111 291.04641 0.9988730
2:2-2:0  -72.953066 -327.56623 181.66009 0.9927277
1:1-0:1   12.286123 -202.77540 227.34764 1.0000000
2:1-0:1  124.409578 -110.36011 359.17926 0.7670426
0:2-0:1   54.283421 -198.08641 306.65325 0.9990229
1:2-0:1   98.354748 -115.17893 311.88842 0.8777957
2:2-0:1  -26.960969 -258.16338 204.24144 0.9999905
2:1-1:1  112.123455  -81.98407 306.23098 0.6725629
0:2-1:1   41.997297 -173.06422 257.05882 0.9995181
1:2-1:1   86.068625  -81.73351 253.87076 0.7975517
2:2-1:1  -39.247092 -229.02454 150.53035 0.9992659
0:2-2:1  -70.126158 -304.89584 164.64353 0.9904079
1:2-2:1  -26.054830 -218.46820 166.35854 0.9999697
2:2-2:1 -151.370548 -363.22150  60.48041 0.3820171
1:2-0:2   44.071327 -169.46235 257.60500 0.9992766
2:2-0:2  -81.244390 -312.44680 149.95802 0.9730112
2:2-1:2 -125.315717 -313.36000  62.72856 0.4812479

md<-aov(xpos_flips~condition*sys,
+         data=avg_measures.0b2)
> summary(md)
               Df Sum Sq Mean Sq F value   Pr(>F)
condition       5    8.4   1.670   1.015 0.409389
sys             1   22.5  22.479  13.663 0.000273 ***
condition:sys   5    4.0   0.809   0.492 0.782380
Residuals     232  381.7   1.645
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
10 observations deleted due to missingness
> TukeyHSD(md,"sys")
  Tukey multiple comparisons of means
    95% family-wise confidence level

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

$sys
         diff       lwr       upr     p adj
2-1 0.6486356 0.3028721 0.9943992 0.0002732

md<-aov(xpos_flips~condition*sys*des,
+         data=avg_measures.0b2)
> summary(md)
                   Df Sum Sq Mean Sq F value  Pr(>F)
condition           5    8.4   1.670   0.998 0.41998
sys                 1   22.5  22.479  13.427 0.00031 ***
des                 1    1.9   1.912   1.142 0.28632
condition:sys       5    4.1   0.811   0.485 0.78755
condition:des       3    2.2   0.720   0.430 0.73147
sys:des             1    2.4   2.374   1.418 0.23503
condition:sys:des   3    0.2   0.078   0.047 0.98662
Residuals         224  375.0   1.674
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
10 observations deleted due to missingness
> TukeyHSD(md,"sys")
  Tukey multiple comparisons of means
    95% family-wise confidence level

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

$sys
         diff       lwr       upr     p adj
2-1 0.6486356 0.2997858 0.9974854 0.0003099
md<-aov(xpos_flips~condition*eht_0,
+         data=avg_measures.0b2)
> summary(md)
                 Df Sum Sq Mean Sq F value Pr(>F)
condition         5    8.4   1.670   0.988 0.4257
eht_0             2   13.5   6.741   3.989 0.0198 *
condition:eht_0  10   12.8   1.278   0.756 0.6709
Residuals       226  382.0   1.690
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
10 observations deleted due to missingness
> TukeyHSD(md,"eht_0")
  Tukey multiple comparisons of means
    95% family-wise confidence level

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

$eht_0
          diff        lwr         upr     p adj
1-0 -0.4027046 -0.9017045  0.09629531 0.1398843
2-0 -0.8391733 -1.5502042 -0.12814245 0.0159796
2-1 -0.4364687 -1.0452318  0.17229431 0.2106455

md<-aov(xpos_flips~condition*eht_0*sys,
+         data=avg_measures.0b2)
> summary(md)
                     Df Sum Sq Mean Sq F value   Pr(>F)
condition             5    8.4   1.670   0.986 0.427211
eht_0                 2   13.5   6.741   3.980 0.020124 *
sys                   1   20.3  20.317  11.995 0.000648 ***
condition:eht_0      10   12.9   1.289   0.761 0.666421
condition:sys         5    2.5   0.506   0.299 0.913233
eht_0:sys             2    1.0   0.484   0.286 0.751817
condition:eht_0:sys  10    5.7   0.574   0.339 0.969543
Residuals           208  352.3   1.694
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
10 observations deleted due to missingness
> TukeyHSD(md,"eht_0")
  Tukey multiple comparisons of means
    95% family-wise confidence level

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

$eht_0
          diff       lwr         upr     p adj
1-0 -0.4027046 -0.902532  0.09712283 0.1406762
2-0 -0.8391733 -1.551383 -0.12696331 0.0162209
2-1 -0.4364687 -1.046241  0.17330385 0.2115415

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

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

$sys
         diff       lwr       upr    p adj
2-1 0.5881831 0.2371486 0.9392177 0.001125

md<-aov(idle_time~condition*sys,
+         data=avg_measures.0b2)
> summary(md)
               Df Sum Sq Mean Sq F value Pr(>F)
condition       5   2568     514   0.706 0.6198
sys             1   8812    8812  12.107 0.0006 ***
condition:sys   5   2313     463   0.636 0.6727
Residuals     232 168860     728
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
10 observations deleted due to missingness
> TukeyHSD(md,"sys")
  Tukey multiple comparisons of means
    95% family-wise confidence level

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

$sys
         diff       lwr       upr     p adj
2-1 -12.84265 -20.11506 -5.570246 0.0006003

md<-aov(idle_time~condition*sys*des,
+         data=avg_measures.0b2)
> summary(md)
                   Df Sum Sq Mean Sq F value   Pr(>F)
condition           5   2568     514   0.688 0.632661
sys                 1   8812    8812  11.813 0.000701 ***
des                 1    334     334   0.448 0.503872
condition:sys       5   2319     464   0.622 0.683293
condition:des       3    702     234   0.314 0.815339
sys:des             1    347     347   0.466 0.495697
condition:sys:des   3    375     125   0.167 0.918261
Residuals         224 167095     746
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
10 observations deleted due to missingness
> TukeyHSD(md,"sys")
  Tukey multiple comparisons of means
    95% family-wise confidence level

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

$sys
         diff       lwr       upr     p adj
2-1 -12.84265 -20.20639 -5.478915 0.0007015

md<-aov(idle_time~condition*eht_0*sys,
+         data=avg_measures.0b2)
> summary(md)
                     Df Sum Sq Mean Sq F value   Pr(>F)
condition             5   2568     514   0.823    0.535
eht_0                 2  25226   12613  20.212 9.52e-09 ***
sys                   1  11318   11318  18.136 3.11e-05 ***
condition:eht_0      10   6557     656   1.051    0.402
condition:sys         5   1354     271   0.434    0.825
eht_0:sys             2   1350     675   1.082    0.341
condition:eht_0:sys  10   4378     438   0.701    0.723
Residuals           208 129803     624
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
10 observations deleted due to missingness
> 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.0b2)

$sys
         diff       lwr       upr   p adj
2-1 -13.88229 -20.62031 -7.144273 6.9e-05

md<-aov(idle_time~condition*eht_0*sys,
+         data=avg_measures.0b2)
> summary(md)
                     Df Sum Sq Mean Sq F value   Pr(>F)
condition             5   2568     514   0.823    0.535
eht_0                 2  25226   12613  20.212 9.52e-09 ***
sys                   1  11318   11318  18.136 3.11e-05 ***
condition:eht_0      10   6557     656   1.051    0.402
condition:sys         5   1354     271   0.434    0.825
eht_0:sys             2   1350     675   1.082    0.341
condition:eht_0:sys  10   4378     438   0.701    0.723
Residuals           208 129803     624
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
10 observations deleted due to missingness
> 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.0b2)

$sys
         diff       lwr       upr   p adj
2-1 -13.88229 -20.62031 -7.144273 6.9e-05

> tapply(avg_measures.0b2$idle_time,avg_measures.0b2$sys,summary)
$`1`
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  220.8   297.9   312.3   312.7   321.5   359.0

$`2`
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  230.8   284.8   302.8   299.9   315.9   359.0

> tapply(avg_measures.0b2$idle_time,avg_measures.0b2$eht_0,summary)
$`0`
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  220.8   262.1   290.8   283.8   299.8   333.9

$`1`
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  241.6   296.8   307.5   308.8   322.1   359.0

$`2`
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  256.1   302.0   315.2   311.0   319.5   334.8

  md<-aov(idle_time~condition*noKD,
+         data=avg_measures.0b2)
> summary(md)
                Df Sum Sq Mean Sq F value   Pr(>F)
condition        5   2436     487   0.692 0.630313
noKD             1   9026    9026  12.813 0.000416 ***
condition:noKD   5   1603     321   0.455 0.809379
Residuals      242 170485     704
---
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.0b2)

$noKD
         diff      lwr       upr     p adj
1-0 -12.87538 -19.9621 -5.788664 0.0004169

md<-aov(xpos_flips~condition*noPP,
+         data=avg_measures.0b2)
> summary(md)
                Df Sum Sq Mean Sq F value Pr(>F)
condition        5    7.2   1.431   0.876 0.4975
noPP             1    6.4   6.357   3.894 0.0496 *
condition:noPP   5   10.8   2.157   1.321 0.2556
Residuals      242  395.1   1.633
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> TukeyHSD(md,"noPP")
  Tukey multiple comparisons of means
    95% family-wise confidence level

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

$noPP
        diff          lwr       upr     p adj
1-0 0.376724 0.0005103274 0.7529377 0.0496925

md<-aov(xpos_flips~condition*noKD,
+         data=avg_measures.0b2)
> summary(md)
                Df Sum Sq Mean Sq F value   Pr(>F)
condition        5    7.2   1.431   0.897 0.483483
noKD             1   23.8  23.778  14.913 0.000145 ***
condition:noKD   5    2.6   0.517   0.324 0.898297
Residuals      242  385.9   1.594
---
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.0b2)

$noKD
         diff       lwr       upr     p adj
1-0 0.6608427 0.3236967 0.9979887 0.0001449

md<-aov(MAD~condition*noPP,
+         data=avg_measures.0b2)
> summary(md)
                Df   Sum Sq Mean Sq F value  Pr(>F)
condition        5  1070366  214073   3.825 0.00236 **
noPP             1      952     952   0.017 0.89632
condition:noPP   5   566033  113207   2.023 0.07610 .
Residuals      242 13543510   55965
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> TukeyHSD(md,"condition")
  Tukey multiple comparisons of means
    95% family-wise confidence level

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

$condition
                       diff         lwr       upr     p adj
antiDPP-antiCH   -77.848063 -211.778640  56.08251 0.5531317
antiKMT-antiCH     7.194692 -128.055684 142.44507 0.9999883
antiUS-antiCH   -147.609140 -283.554615 -11.66366 0.0245927
proCH-antiCH      30.979439 -132.785711 194.74459 0.9942892
proUs-antiCH      23.929734 -139.835416 187.69488 0.9983219
antiKMT-antiDPP   85.042755  -49.562277 219.64779 0.4580632
antiUS-antiDPP   -69.761076 -205.064523  65.54237 0.6766640
proCH-antiDPP    108.827502  -54.405076 272.06008 0.3953409
proUs-antiDPP    101.777797  -61.454781 265.01038 0.4733836
antiUS-antiKMT  -154.803831 -291.413815 -18.19385 0.0161597
proCH-antiKMT     23.784747 -140.532445 188.10194 0.9983963
proUs-antiKMT     16.735042 -147.582150 181.05223 0.9997096
proCH-antiUS     178.588578   13.698774 343.47838 0.0251774
proUs-antiUS     171.538873    6.649069 336.42868 0.0361198
proUs-proCH       -7.049705 -195.534451 181.43504 0.9999980

md<-aov(idle_time~condition*noKD*des,
+         data=avg_measures.0b2)
> summary(md)
                    Df Sum Sq Mean Sq F value   Pr(>F)
condition            5   2436     487   0.678 0.640285
noKD                 1   9026    9026  12.568 0.000474 ***
des                  1    287     287   0.400 0.527642
condition:noKD       5   1624     325   0.452 0.811456
condition:des        3   1000     333   0.464 0.707683
noKD:des             1      1       1   0.001 0.973077
condition:noKD:des   3   1120     373   0.520 0.669043
Residuals          234 168056     718
---
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.0b2)

$noKD
         diff       lwr       upr     p adj
1-0 -12.87538 -20.03193 -5.718831 0.0004749

 md<-aov(xpos_flips~condition*noKD*des,
+         data=avg_measures.0b2)
> summary(md)
                    Df Sum Sq Mean Sq F value  Pr(>F)
condition            5    7.2   1.431   0.894 0.48595
noKD                 1   23.8  23.778  14.853 0.00015 ***
des                  1    2.1   2.103   1.314 0.25292
condition:noKD       5    2.6   0.515   0.321 0.89977
condition:des        3    2.3   0.777   0.485 0.69299
noKD:des             1    0.6   0.586   0.366 0.54567
condition:noKD:des   3    6.2   2.078   1.298 0.27588
Residuals          234  374.6   1.601
---
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 * des, data = avg_measures.0b2)

$noKD
         diff       lwr       upr     p adj
1-0 0.6608427 0.3229558 0.9987295 0.0001506

md<-aov(MAD~condition*noKD*des,
+         data=avg_measures.0b2)
> summary(md)
                    Df   Sum Sq Mean Sq F value  Pr(>F)
condition            5  1070366  214073   3.742 0.00281 **
noKD                 1     1460    1460   0.026 0.87320
des                  1        1       1   0.000 0.99636
condition:noKD       5   246059   49212   0.860 0.50854
condition:des        3   218991   72997   1.276 0.28328
noKD:des             1    41273   41273   0.721 0.39653
condition:noKD:des   3   216453   72151   1.261 0.28840
Residuals          234 13386258   57206
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> TukeyHSD(md,"condition")
  Tukey multiple comparisons of means
    95% family-wise confidence level

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

$condition
                       diff         lwr       upr     p adj
antiDPP-antiCH   -77.848063 -213.293031  57.59690 0.5652479
antiKMT-antiCH     7.194692 -129.584999 143.97438 0.9999889
antiUS-antiCH   -147.609140 -285.091789 -10.12649 0.0273124
proCH-antiCH      30.979439 -134.637450 196.59633 0.9945725
proUs-antiCH      23.929734 -141.687155 189.54662 0.9984073
antiKMT-antiDPP   85.042755  -51.084295 221.16981 0.4708719
antiUS-antiDPP   -69.761076 -206.594438  67.07229 0.6868997
proCH-antiDPP    108.827502  -56.250794 273.90580 0.4082108
proUs-antiDPP    101.777797  -63.300499 266.85609 0.4861278
antiUS-antiKMT  -154.803831 -292.958503 -16.64916 0.0181296
proCH-antiKMT     23.784747 -142.390427 189.95992 0.9984780
proUs-antiKMT     16.735042 -149.440132 182.91022 0.9997246
proCH-antiUS     178.588578   11.834318 345.34284 0.0279458
proUs-antiUS     171.538873    4.784613 338.29313 0.0397391
proUs-proCH       -7.049705 -197.665702 183.56629 0.9999981

md<-aov(idle_time~condition*noPP*sys,
+         data=avg_measures.0b2)
> summary(md)
                    Df Sum Sq Mean Sq F value   Pr(>F)
condition            5   2568     514   0.845 0.519128
noPP                 1     58      58   0.095 0.758159
sys                  1   8770    8770  14.430 0.000188 ***
condition:noPP       5   2954     591   0.972 0.435749
condition:sys        5   2297     459   0.756 0.582537
noPP:sys             1  31462   31462  51.768 9.71e-12 ***
condition:noPP:sys   5    737     147   0.243 0.943115
Residuals          220 133707     608
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
10 observations deleted due to missingness
> TukeyHSD(md,"sys")
  Tukey multiple comparisons of means
    95% family-wise confidence level

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

$sys
         diff       lwr       upr     p adj
2-1 -12.71539 -19.36275 -6.068023 0.0002099

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

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

$`noPP:sys`
              diff        lwr        upr     p adj
1:1-0:1 -53.255056 -74.849871 -31.660241 0.0000000
0:2-0:1 -70.965613 -93.648029 -48.283197 0.0000000
1:2-0:1 -55.882992 -76.844325 -34.921658 0.0000000
0:2-1:1 -17.710557 -30.603024  -4.818089 0.0025802
1:2-1:1  -2.627935 -12.172631   6.916761 0.8919620
1:2-0:2  15.082621   3.281893  26.883349 0.0059882

md<-aov(idle_time~condition*noTGG*sys,
+         data=avg_measures.0b2)
> summary(md)
                     Df Sum Sq Mean Sq F value   Pr(>F)
condition             5   2568     514   0.706 0.619308
noTGG                 1    236     236   0.324 0.569537
sys                   1   8619    8619  11.853 0.000689 ***
condition:noTGG       5    691     138   0.190 0.966185
condition:sys         5   2500     500   0.688 0.633206
noTGG:sys             1   6665    6665   9.166 0.002759 **
condition:noTGG:sys   5   1299     260   0.357 0.877184
Residuals           220 159975     727
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
10 observations deleted due to missingness
> TukeyHSD(md,"sys")
  Tukey multiple comparisons of means
    95% family-wise confidence level

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

$sys
         diff       lwr       upr     p adj
2-1 -12.64445 -19.91552 -5.373386 0.0007276

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

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

$`noTGG:sys`
              diff        lwr        upr     p adj
1:1-0:1 -14.980517 -31.274931   1.313898 0.0839444
0:2-0:1 -26.551151 -41.760751 -11.341551 0.0000596
1:2-0:1 -18.625233 -33.572500  -3.677965 0.0078427
0:2-1:1 -11.570635 -24.249509   1.108240 0.0874950
1:2-1:1  -3.644716 -16.007675   8.718243 0.8708997
1:2-0:2   7.925919  -2.967507  18.819345 0.2380893

md<-aov(idle_time~condition*noTMD*sys,
+         data=avg_measures.0b2)
> summary(md)
                     Df Sum Sq Mean Sq F value   Pr(>F)
condition             5   2568     514   0.719 0.610077
noTMD                 1   1930    1930   2.701 0.101721
sys                   1   8158    8158  11.415 0.000862 ***
condition:noTMD       5   2141     428   0.599 0.700597
condition:sys         5   2578     516   0.722 0.607899
noTMD:sys             1   6015    6015   8.417 0.004095 **
condition:noTMD:sys   5   1942     388   0.543 0.743206
Residuals           220 157222     715
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
10 observations deleted due to missingness
> TukeyHSD(md,"sys")
  Tukey multiple comparisons of means
    95% family-wise confidence level

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

$sys
         diff       lwr       upr     p adj
2-1 -12.30725 -19.51547 -5.099036 0.0009032

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

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

$`noTMD:sys`
              diff         lwr       upr     p adj
1:1-0:1  -9.646826 -25.2204690  5.926818 0.3787194
0:2-0:1 -22.536827 -35.6585323 -9.415123 0.0000814
1:2-0:1 -10.866223 -24.7671606  3.034716 0.1824387
0:2-1:1 -12.890002 -25.8941946  0.114191 0.0530193
1:2-1:1  -1.219397 -15.0094648 12.570671 0.9957619
1:2-0:2  11.670605   0.7249784 22.616232 0.0315866

md<-aov(idle_time~condition*noKD*sys,
+         data=avg_measures.0b2)
> summary(md)
                    Df Sum Sq Mean Sq F value  Pr(>F)
condition            5   2568     514   0.721 0.60814
noKD                 1   9521    9521  13.371 0.00032 ***
sys                  1   7082    7082   9.946 0.00184 **
condition:noKD       5   1656     331   0.465 0.80190
condition:sys        5   2200     440   0.618 0.68627
noKD:sys             1   1553    1553   2.180 0.14121
condition:noKD:sys   5   1319     264   0.371 0.86850
Residuals          220 156654     712
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
10 observations deleted due to missingness
> TukeyHSD(md,"noKD")
  Tukey multiple comparisons of means
    95% family-wise confidence level

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

$noKD
         diff       lwr       upr     p adj
1-0 -13.34755 -20.54275 -6.152353 0.0003205

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

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

$sys
         diff       lwr       upr     p adj
2-1 -11.45022 -18.64542 -4.255023 0.0019448

 md<-aov(idle_time~condition*noDPP*sys,
+         data=avg_measures.0b2)
> summary(md)
                     Df Sum Sq Mean Sq F value   Pr(>F)
condition             5   2568     514   0.730 0.601992
noDPP                 1   1572    1572   2.233 0.136484
sys                   1  10730   10730  15.241 0.000126 ***
condition:noDPP       5   4284     857   1.217 0.301964
condition:sys         5   2337     467   0.664 0.651267
noDPP:sys             1   5538    5538   7.867 0.005484 **
condition:noDPP:sys   5    649     130   0.184 0.968262
Residuals           220 154875     704
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
10 observations deleted due to missingness
> TukeyHSD(md,"sys")
  Tukey multiple comparisons of means
    95% family-wise confidence level

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

$sys
         diff       lwr       upr     p adj
2-1 -13.89951 -21.05374 -6.745282 0.0001678

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

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

$`noDPP:sys`
              diff        lwr        upr     p adj
1:1-0:1  21.966221   6.043082  37.889360 0.0024474
0:2-0:1  -5.139458 -18.010517   7.731600 0.7298522
1:2-0:1  -4.053509 -16.091842   7.984823 0.8194936
0:2-1:1 -27.105679 -42.160368 -12.050991 0.0000322
1:2-1:1 -26.019730 -40.368980 -11.670481 0.0000278
1:2-0:2   1.085949  -9.777693  11.949591 0.9939110

 md<-aov(idle_time~condition*noKMT*sys,
+         data=avg_measures.0b2)
> summary(md)
                     Df Sum Sq Mean Sq F value   Pr(>F)
condition             5   2568     514   0.731 0.601233
noKMT                 1    113     113   0.161 0.688318
sys                   1   9023    9023  12.835 0.000419 ***
condition:noKMT       5   1806     361   0.514 0.765709
condition:sys         5   2250     450   0.640 0.669335
noKMT:sys             1  11383   11383  16.193 7.87e-05 ***
condition:noKMT:sys   5    752     150   0.214 0.956312
Residuals           220 154658     703
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
10 observations deleted due to missingness
> TukeyHSD(md,"noKMT:sys")
  Tukey multiple comparisons of means
    95% family-wise confidence level

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

$`noKMT:sys`
              diff        lwr        upr     p adj
1:1-0:1  20.215505   4.194164  36.236845 0.0068758
0:2-0:1  13.862997  -6.395823  34.121816 0.2898735
1:2-0:1  -1.858371 -15.812827  12.096085 0.9858811
0:2-1:1  -6.352508 -24.850818  12.145802 0.8105767
1:2-1:1 -22.073876 -33.320856 -10.826896 0.0000048
1:2-0:2 -15.721368 -32.461468   1.018733 0.0742403
md<-aov(xpos_flips~condition*noPP*sys,
+         data=avg_measures.0b2)
> summary(md)
                    Df Sum Sq Mean Sq F value   Pr(>F)
condition            5    8.4   1.670   1.082  0.37130
noPP                 1    7.3   7.327   4.745  0.03044 *
sys                  1   26.1  26.122  16.919 5.51e-05 ***
condition:noPP       5    9.6   1.923   1.246  0.28874
condition:sys        5    5.3   1.054   0.682  0.63724
noPP:sys             1   15.4  15.397   9.972  0.00181 **
condition:noPP:sys   5    4.8   0.966   0.626  0.68030
Residuals          220  339.7   1.544
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
10 observations deleted due to missingness
> TukeyHSD(md,"sys")
  Tukey multiple comparisons of means
    95% family-wise confidence level

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

$sys
        diff       lwr      upr    p adj
2-1 0.693963 0.3589209 1.029005 6.25e-05

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

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

$noPP
         diff        lwr       upr     p adj
1-0 0.4356707 0.04129862 0.8300429 0.0305304

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

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

$`noPP:sys`
             diff          lwr       upr     p adj
1:1-0:1 1.7019652  0.613537922 2.7903925 0.0004138
0:2-0:1 1.9897113  0.846466449 3.1329561 0.0000632
1:2-0:1 2.1786829  1.122184524 3.2351813 0.0000014
0:2-1:1 0.2877461 -0.362063314 0.9375554 0.6612264
1:2-1:1 0.4767177 -0.004356473 0.9577919 0.0531162
1:2-0:2 0.1889717 -0.405811606 0.7837549 0.8437831

md<-aov(xpos_flips~condition*noKD*sys,
+         data=avg_measures.0b2)
> summary(md)
                    Df Sum Sq Mean Sq F value   Pr(>F)
condition            5    8.4   1.670   1.044 0.392674
noKD                 1   24.2  24.197  15.124 0.000133 ***
sys                  1   18.1  18.074  11.297 0.000915 ***
condition:noKD       5    2.1   0.429   0.268 0.930046
condition:sys        5    3.8   0.766   0.479 0.791876
noKD:sys             1    2.6   2.574   1.609 0.205993
condition:noKD:sys   5    5.4   1.086   0.679 0.639943
Residuals          220  352.0   1.600
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
10 observations deleted due to missingness
> TukeyHSD(md,"noKD")
  Tukey multiple comparisons of means
    95% family-wise confidence level

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

$noKD
         diff       lwr      upr     p adj
1-0 0.6728769 0.3318183 1.013936 0.0001337

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

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

$sys
         diff       lwr      upr     p adj
2-1 0.5784403 0.2373817 0.919499 0.0009756

summary(md)
                     Df Sum Sq Mean Sq F value   Pr(>F)
condition             5    8.4   1.670   1.000 0.418915
noKMT                 1    1.4   1.444   0.864 0.353623
sys                   1   21.1  21.081  12.615 0.000468 ***
condition:noKMT       5    2.7   0.539   0.322 0.899171
condition:sys         5    4.3   0.861   0.515 0.764559
noKMT:sys             1    9.5   9.470   5.667 0.018139 *
condition:noKMT:sys   5    1.6   0.320   0.191 0.965589
Residuals           220  367.6   1.671
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
10 observations deleted due to missingness
> TukeyHSD(md,"noKMT:sys")
  Tukey multiple comparisons of means
    95% family-wise confidence level

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

$`noKMT:sys`
              diff        lwr       upr     p adj
1:1-0:1 -0.5316761 -1.3128012 0.2494489 0.2945139
0:2-0:1 -0.1346266 -1.1223511 0.8530980 0.9848955
1:2-0:1  0.3702715 -0.3100820 1.0506250 0.4951262
0:2-1:1  0.3970495 -0.5048409 1.2989399 0.6653816
1:2-1:1  0.9019476  0.3535979 1.4502974 0.0001782
1:2-0:2  0.5048981 -0.3112703 1.3210665 0.3799100

md<-aov(MAD~condition*noKMT*sys,
+         data=avg_measures.0b2)
> summary(md)
                     Df   Sum Sq Mean Sq F value  Pr(>F)
condition             5  1080086  216017   3.630 0.00355 **
noKMT                 1       44      44   0.001 0.97822
sys                   1      834     834   0.014 0.90590
condition:noKMT       5    97236   19447   0.327 0.89648
condition:sys         5   116462   23292   0.391 0.85441
noKMT:sys             1   466154  466154   7.834 0.00558 **
condition:noKMT:sys   5   133465   26693   0.449 0.81406
Residuals           220 13091292   59506
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
10 observations deleted due to missingness
> TukeyHSD(md,"noKMT:sys")
  Tukey multiple comparisons of means
    95% family-wise confidence level

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

$`noKMT:sys`
              diff        lwr        upr     p adj
1:1-0:1 -107.40879 -254.81091  39.993323 0.2368665
0:2-0:1 -179.90001 -366.28846   6.488434 0.0628854
1:2-0:1  -57.37263 -185.75865  71.013403 0.6546562
0:2-1:1  -72.49122 -242.68235  97.699906 0.6882757
1:2-1:1   50.03617  -53.44011 153.512438 0.5945953
1:2-0:2  122.52739  -31.48758 276.542348 0.1697969


#EXORDIUM
library(tm)
library(textmineR)
library(bigmemory)
library(igraph)

#CREATING CUSTOMIZED STOPWORDS LIST
stopwords <- read.csv("stopwords.csv", header = FALSE)
stopwords <- as.character(stopwords$V1)
flibbertigibbet <- c(stopwords, stopwords())

#ACCESSING FILES
docs <- Corpus(DirSource("whatever"))

#PREPROCESSING
docs <- tm_map(docs, content_transformer(tolower))
removeSpecialChars <- function(x) gsub("[^a-zA-Z ]", "", x)
docs <- tm_map(docs, removeSpecialChars)
docs <- tm_map(docs, removeNumbers)
docs <- tm_map(docs, removePunctuation)
docs <- tm_map(docs, stripWhitespace)
docs <- tm_map(docs, removeWords, flibbertigibbet)

#SETTING AND INSPECTING MATRIX
dtm <- DocumentTermMatrix(docs)
dim(dtm)
dtm <- weightTf(dtm, normalize = TRUE)
#colnames(dtm) [100:110]
#inspect(dtm) [1:10, 101:110]
#top100terms <- findFreqTerms(dtm, 100)
dtmDense <- removeSparseTerms(dtm, 0.98)   #removes terms absent in whatever percentage of documents; max allowed sparsity percentage is 0.998
dim(dtmDense)
dtmFinal <- as.big.matrix(x = as.matrix(dtmDense))
dtmFinal <- as.matrix(dtmFinal)

#CONVERTING TO COÖCCURRENCE MATRIX
cooccurrences <- Dtm2Tcm(dtmFinal)
write.csv(as.matrix(cooccurrences), file = "AdjacencyMatrix.csv")

#BUILDING NETWORK
the_wordgraph <- graph_from_adjacency_matrix(cooccurrences, weighted = TRUE, mode = "lower")
net_clusters <- cluster_louvain(the_wordgraph, weights = NULL)
cluster_results <- cbind(V(the_wordgraph)$name, net_clusters$membership)
write.csv(cluster_results, file ="ClusterResults.csv")
fluke <- neighbors(the_wordgraph, "china", mode = c("out", "in", "all", "total"))
neighs <- cbind(V(the_wordgraph)$name, fluke$membership)
write.csv(neighs, file ="Neighbors.csv")

Topic modelling by network clustering, experimental.r


library("monocle")
library("reshape")
fpkm_matrix <- read.table('genes.fpkm_table', sep="\t", header = TRUE, row.names=1) # fpkm matrix
sample_sheet<- read.delim("samples.table", row.names=1) # sample sheet
gene_annotations<- read.delim("genes.attr_table", row.names=1) # gene annotations
fd<- new("AnnotatedDataFrame", data = gene_annotations)
pheno.data <- colnames(fpkm_matrix)
pheno.data <- unlist(lapply(pheno.data, function(x) strsplit(x, '_')[[1]][1]))
pheno.data.df <- data.frame(type=pheno.data)
rownames(pheno.data.df) <- colnames(fpkm_matrix)
row_names <- rownames(pheno.data.df)
pheno.data.df$name <- row_names
pheno.data.df$type <- NULL
pd <- new('AnnotatedDataFrame', data = pheno.data.df)
HSMM <- newCellDataSet(as.matrix(fpkm_matrix), phenoData = pd, featureData = fd)
pData(HSMM)$Size_Factor <- NULL
rownames(pData(HSMM)) <- NULL
pData(HSMM)$Total_mRNAs <- Matrix::colSums(exprs(HSMM))
write.table(pData(HSMM), "mRNA_table.txt", sep="\t",quote=FALSE)


library(jsonlite)
library(ggplot2)
library( ReporteRs )
library(fBasics)
library(reshape)

#args = commandArgs(trailingOnly=TRUE)
#setwd(args[1])
setwd("C:/Users/Johannes/Projects/elastic/results/output/C/2")

logDF <- fromJSON("logstash.json", flatten = TRUE)
logDF$timeEpoch <- as.numeric( logDF$`fields.@timestamp`)
logDF$time <- as.POSIXct(logDF$timeEpoch/1000, origin="1970-01-01", tz="Europe/Amsterdam")

workerStart <- logDF[grep("worker:start", logDF$"_source.message", ignore.case=T),]
workerStart$start <- 1
firstStart <- min(workerStart$timeEpoch)

metricsDF <- fromJSON("metrics.json", flatten = TRUE)
metricsDF <- metricsDF[grep("^node", metricsDF$`_source.beat.hostname`),]
metricsDF$timeEpoch <- as.numeric( metricsDF$`fields.@timestamp`)
metricsDF$timeFromStart <- (metricsDF$timeEpoch - firstStart)/1000
metricsDF$time <- as.POSIXct(metricsDF$timeEpoch/1000, origin="1970-01-01", tz="Europe/Amsterdam")

loadDF <- metricsDF[grep("load", metricsDF$"_source.metricset.name"),]
save(loadDF, file = "loadDF.Rdata")

load("loadDF.Rdata")
load <- data.frame(loadDF$`_source.beat.name`, loadDF$timeFromStart, loadDF$`_source.system.load.5`)
colnames(load) <- c("name", "time", "load5")
load = load[load$time > 0,]

load$cut <- cut(x = load$time, breaks = seq(0, 2400, by = 10), labels = FALSE)
# AVG the values!!
for(i in 0:240)
{
  bin = load[load$cut == i,]
  bin = bin[complete.cases(bin), ]
  #print(colAvgs(bin[, "load5"]))
  print(bin$load5)
}

p <- ggplot(loadDF, aes(loadDF$timeFromStart, loadDF$`_source.system.load.5`))
p <- p + geom_line(aes(colour = factor(loadDF$`_source.beat.hostname`)))
p <- p + xlab("Time (in seconds) since first Workflow started") + ylab("System load over 5 minutes")
p <- p + scale_x_continuous(limits=c(0, 2540), breaks=(breaks=seq(0,2400,200)))
p <- p + scale_colour_discrete(name="")
p <- p + theme(legend.position="top")

p


#' Matrix Multiplication
#'
#' Conceptually, this computes \code{t(x) \%*\% x} for a shaq \code{x}.
#'
#' @section Communication:
#' The operation consists of a local crossproduct, followed by an
#' \code{allreduce()} call, quadratic on the number of columns.
#'
#' For \code{crossprod()}, if the matrix distribution is poorly balanced
#' (specifically, if any rank has fewer rows than columns), then an inefficient
#' method is used. Similarly for \code{tcrossprod()} if the number of local rows
#' is greater than the number of local columns.
#'
#' @param x
#' A shaq.
#' @param y
#' A shaq or \code{NULL}.
#'
#' @return
#' A regular matrix.
#'
#' @examples
#' \dontrun{
#' library(kazaam)
#' x = ranshaq(runif, 10, 3)
#'
#' cp = crossprod(x)
#' comm.print(cp)
#'
#' finalize()
#' }
#'
#' @useDynLib kazaam R_mpicrossprod
#' @useDynLib kazaam R_float_mpicrossprod
#'
#' @name crossprod
#' @rdname crossprod
NULL

cp.internal = function(x, alpha)
{
  comm_ptr = pbdMPI::get.mpi.comm.ptr(.pbd_env$SPMD.CT$comm)
  data = DATA(x)
  alpha = as.double(alpha)

  if (is.float(data))
  {
    ret = .Call(R_float_mpicrossprod, DATA(data), alpha, comm_ptr)
    float32(ret)
  }
  else
  {
    if (!is.double(data))
      storage.mode(data) = "double"

    .Call(R_mpicrossprod, data, alpha, comm_ptr)
  }
}

cp.shaq = function(x, y = NULL)
{
  if (!is.null(y))
  {
    if (is.shaq(y))
    {
      cp.local = crossprod(DATA(x), DATA(y))
      MPI_Allreduce(cp.local)
    }
    else
      comm.stop("argument 'y' must be a shaq or NULL")
  }
  else
  {
    m = nrow(DATA(x))
    n = ncol(DATA(x))
    check = MPI_Allreduce(as.integer(m<n))
    if (check)
    {
      cp.local = crossprod(DATA(x))
      MPI_Allreduce(cp.local)
    }
    else
      cp.internal(x, 1.0)
  }
}

tcp.shaq = function(x, y = NULL)
{
  if (!is.null(y))
  {
    if (is.tshaq(y))
    {
      tcp.local = tcrossprod(DATA(x), DATA(y))
      MPI_Allreduce(tcp.local)
    }
    else
      comm.stop("argument 'y' must be a tshaq or NULL")
  }
  else
  {
    m = nrow(DATA(x))
    n = ncol(DATA(x))
    check = MPI_Allreduce(as.integer(m<n))
    if (check)
    {
      tcp.local = tcrossprod(DATA(x))
      MPI_Allreduce(tcp.local)
    }
    else
      cp.internal(x, 1.0)
  }
}

#' @rdname crossprod
#' @export
setMethod("crossprod", signature(x="shaq", y="ANY"), cp.shaq)

#' @rdname crossprod
#' @export
setMethod("tcrossprod", signature(x="tshaq", y="ANY"), tcp.shaq)


library(base)
library(caret)
library(e1071)
library(modules)
library(rstudioapi)
library(stats)
library(tidyverse)
library(utils)

base::setwd(base::dirname(rstudioapi::getActiveDocumentContext()$path))
start <- base::Sys.time()

base::set.seed(0xACDC)

train_test_prop <- 0.75

data <- modules::use('data')$get_data$get_data()

target_column <- 47
target_column_name <- base::colnames(data)[target_column]
formula <- stats::as.formula(base::paste(target_column_name, ' ~ .'))

data$url <- NULL

index <-
  base::sample(1:base::nrow(data), size = base::nrow(data) * train_test_prop)

train <- data[index, ]
test <- data[-index, ]

knn_model <- caret::train(
  formula,
  data = train,
  method = 'knn',
  trControl = caret::trainControl('cv', number = 10),
  preProcess = base::c('center', 'scale'),
  tuneLength = 20
)

base::plot(knn_model)
base::print(knn_model$bestTune)

predicted <- knn_model %>% stats::predict(test)

cm <-
  caret::confusionMatrix(predicted, base::as.factor(test$status))
base::print(cm$table)
base::print(cm$overall['Accuracy'])

base::print(base::Sys.time() - start)
base::rm(start, index)

predict_using_knn <- function(file = 'scripts/output.csv') {
  output <- utils::read.csv(
    file,
    header = TRUE,
    sep = ',',
    stringsAsFactors = FALSE,
    strip.white = TRUE
  )

  urls <- output$url
  output$url <- NULL

  results <- knn_model %>% stats::predict(output)

  base::print(base::data.frame(url = urls, status = results))
}


# define string formatting
`%--%` <- function(x, y)
  # from stack exchange:
  # https://stackoverflow.com/questions/46085274/is-there-a-string-formatting-operator-in-r-similar-to-pythons
{
  do.call(sprintf, c(list(x), y))
}

results <- data.frame(matrix(0, ncol = 8, nrow = 2))
colnames(results) <- c("BART-Int", "BART-Int-se", "GPBQ", "GPBQ-se", "MI", "MI-se", "dim", "n")
dims = c(1, 2, 3)
l = 1
for (num_data in c(20)) {
  for (dim in c(1)) {
    bart = c()
    gp = c()
    mi = c()
    for (num_cv in 1:20) {
      df <- read.csv("results/fisher_function/fisher_function/PaperDim%sUniform_%s_%s.csv"
                     %--% c(dim, num_data, num_cv))
      bart = c(bart, df$BARTMean)
      gp = c(gp, df$GPMean)
      mi = c(mi, df$MIMean)
    }
  }
  results[l, 1] = mean(abs(bart - df$actual))
  results[l, 2] = sd(abs(bart - df$actual)) / sqrt(20)
  results[l, 3] = mean(abs(gp - df$actual))
  results[l, 4] = sd(abs(gp - df$actual)) / sqrt(20)
  results[l, 5] = mean(abs(mi - df$actual))
  results[l, 6] = sd(abs(mi - df$actual)) / sqrt(20)
  results[l, 7] = dim
  results[l, 8] = num_data*dim
  l = l + 1
}
for (num_data in c(20)) {
  for (dim in c(2)) {
    bart = c()
    gp = c()
    mi = c()
    for (num_cv in 1:20) {
      df <- read.csv("results/fisher_function/fisher_function/PaperDim%sUniform_%s_%s.csv"
                     %--% c(dim, num_data, num_cv))
      bart = c(bart, df$BARTMean)
      gp = c(gp, df$GPMean)
      mi = c(mi, df$MIMean)
    }
  }
  results[l, 1] = mean(abs(bart - df$actual))
  results[l, 2] = sd(abs(bart - df$actual)) / sqrt(20)
  results[l, 3] = mean(abs(gp - df$actual))
  results[l, 4] = sd(abs(gp - df$actual)) / sqrt(20)
  results[l, 5] = mean(abs(mi - df$actual))
  results[l, 6] = sd(abs(mi - df$actual)) / sqrt(20)
  results[l, 7] = dim
  results[l, 8] = num_data*dim
  l = l + 1
}

for (num_data in c(20)) {
  for (dim in c(3)) {
    bart = c()
    gp = c()
    mi = c()
    for (num_cv in 1:20) {
      df <- read.csv("results/fisher_function/fisher_function/PaperDim%sUniform_%s_%s.csv"
                     %--% c(dim, num_data, num_cv))
      bart = c(bart, df$BARTMean)
      gp = c(gp, df$GPMean)
      mi = c(mi, df$MIMean)
    }
  }
  results[l, 1] = mean(abs(bart - df$actual))
  results[l, 2] = sd(abs(bart - df$actual)) / sqrt(20)
  results[l, 3] = mean(abs(gp - df$actual))
  results[l, 4] = sd(abs(gp - df$actual)) / sqrt(20)
  results[l, 5] = mean(abs(mi - df$actual))
  results[l, 6] = sd(abs(mi - df$actual)) / sqrt(20)
  results[l, 7] = dim
  results[l, 8] = num_data*dim
  l = l + 1
}

write.csv(results, "figures_code/non_stationarity.csv" %--% c(dim, num_data))


#' get_local_dim
#'
#' Given a global number of rows or columns, get the corresponding local number
#' of rows or columns. Pass the number of rows for a \code{shaq} and the number
#' of columns for a \code{tshaq}.
#'
#' @details
#' This assumes that the data is distributed across ranks in roughly equal
#' pieces. Modify the communicator by setting \code{.pbd_env$SPMD.CT$comm}.
#'
#' @param dim
#' Either the global number of rows or the global number of columns.
#'
#' @return
#' The number of rows/columns which is local to the calling rank.
#'
#' @export
get_local_dim = function(dim)
{
  dim = as.double(dim)

  size = comm.size()
  local = dim %/% size
  rem = dim - local*size
  if (comm.rank()+1 <= rem)
    local = local + 1

  local
}


  logbook.fisheries.stats.merge = function( Z ) {

    # merge snow crab fishery stats to incoming data frame Z
    # using plon/plat/yr discretization
    # data exists for 1996 and more recent

    nZ = nrow(Z)
    Z0 = Z[ which( Z$yr < 1996 ) ,]

    yrs = sort( unique( Z$yr ), decreasing=T )  # most recent will have data and is used to init var lists
    yrs = yrs[ which(yrs >= 1996) ]
    out = NULL
    for ( yr in yrs )  {

      X = Z[ which( Z$yr == yr ) ,]
      X$plon = aegis_floor(X$plon)
      X$plat = aegis_floor(X$plat)
      X$gridid = paste(
        X$plon%/%p$fisheries.grid.resolution * p$fisheries.grid.resolution,
        X$plat%/%p$fisheries.grid.resolution * p$fisheries.grid.resolution,
        sep="."
      )

      # fisheries data regridded
      fg = logbook.db( DS="logbook.gridded", p=p, yrs=yr )
      fg$gridid = as.character( fg$gridid )
      X = merge( X, fg, by="gridid", all.x=T, all.y=F, sort=F)
      X$gridid = NULL # no longer needed

      for (vl in setdiff(names(fg), c("plon", "plat","gridid") )) {
        X[!is.finite(X[,vl]),vl] = 0 # make sure NA's created by merge statement are set to 0
      }
      rm (fg); gc()

      out = rbind( out, X )
    }

    # fill years without appropriate data with NAs
    if (nrow(Z0) > 0 ) {
      out.names = names ( out )
      newvars = setdiff( out.names, names(Z0) )
      for (nv in newvars) Z0[,nv] = NA
      out = rbind(out, Z0[, names(out)] )
    }

    if (nrow(out) != nZ ) stop ("Merge error")

    return (out)

  }


asmat_BandMat <- function(x)
{
  dim <- getdim(x)
  k <- kdim(x)

  if (class(x) == "ZeroMat")
  {
    ret <- numeric(prod(dim))
    dim(ret) <- dim
    return(ret)
  }

  .Call(R_tomatrix_fromband, getData(x), dim[1L], dim[2L], k[1L], k[2L])
}

asmat_SymMat <- function(x)
{
  n <- getdim(x)[1L]
  triangle <- x@triangle

  .Call(R_tomatrix_fromsym, getData(x), n, triangle)
}

#' as.matrix
#'
#' @param x
#' A banded or symmetric matrix.
#'
#' @return
#' An R matrix.
#'
#' @keywords Casters
#' @name as.matrix
#' @rdname as.matrix
NULL

#' @rdname as.matrix
#' @export
setMethod("as.matrix", signature(x="BandMat"), asmat_BandMat)

#' @rdname as.matrix
#' @export
setMethod("as.matrix", signature(x="SymMat"), asmat_SymMat)


cc_colors <- c(rgb(240, 240, 0, maxColorValue=255),        # AJ
               rgb(128, 128, 128, maxColorValue=255),               #B6
               rgb(240, 128, 128, maxColorValue=255),              #129S
               rgb(16, 16, 240, maxColorValue=255),                   #NOD
               rgb(0, 160, 240, maxColorValue=255),                   #NZO
               rgb(0, 160, 0, maxColorValue=255),                        #CAST
               rgb(240, 0, 0, maxColorValue=255),                        #PWK
               rgb(144, 0, 224, maxColorValue=255))                   #WSB