#######################################################################
#sumFunc.r
sumFunc <- function(x){
  n <- length(x)
  x <- as.integer(x)
  sum <- 0
  for(i in 1:n){
    if(!x[i]%%3)
      sum <- sum + x[i]
    else sum <- sum + 3*x[i]
    if(sum > 300){
      print(paste("Your sum of", sum, "exceeds 300"))
      break
    }
  }
  sum
}
#######################################################################
#meanComp.r
meanComp <- function(A){
  B <- vector(length = ncol(A)*(ncol(A) - 1)/2)
  count <- 1
  for(i in 1:(ncol(A) - 1)){
    for(j in (i + 1):ncol(A)){
      n1 <- nrow(A) - sum(is.na(A[, i]))
      n2 <- nrow(A) - sum(is.na(A[, j]))
      m1 <- mean(A[, i],na.rm = TRUE)
      s1 <- var(A[, i], na.rm = TRUE)
      m2 <- mean(A[, j], na.rm = TRUE)
      s2 <- var(A[, j], na.rm = TRUE)
      n <- n1 + n2 - 2
      sdFactor <- sqrt(n1^{-1} + n2^{-1})
      sPooled <- sqrt(((n1 - 1)*s1 +  (n2 - 1)*s2)/n)
      mDiff <- m1 - m2 
      B[count] <- mDiff/(sPooled*sdFactor)
      count <- count+1
    }
  }
  B
}
#######################################################################
#meanCompVec.r
meanCompVec <- function(A){            

  indexList <- list()

  #fill indexList with all the index pairs
  count <- 0
  for(i in 1:(ncol(A) - 1)){
    for(j in (i + 1):ncol(A))
      indexList[[count + j - i]] <- c(i, j)
    count <- count + ncol(A) - i
  }

  #compute the statistics for the columns
  meanVec <- apply(A, MARGIN = 2, FUN = mean, na.rm = TRUE)
  varVec <- apply(A, MARGIN = 2, FUN = var, na.rm = TRUE)
  nVec <- apply(A, MARGIN = 2, FUN =
        function(e) length(e) - sum(is.na(e)))

  #now compute the t statistics
  sapply(indexList, FUN = function(e){
    n1 <- nVec[e[1]]
    n2 <- nVec[e[2]]
    mDiff <- meanVec[e[1]] - meanVec[e[2]]
    n <- n1 + n2 - 2
    sdFactor <- sqrt(n1^{-1} + n2^{-1})
    sPooled <- sqrt(((n1 - 1)*(varVec[e[1]]) +
        (n2 - 1)*(varVec[e[2]]))/n)
    mDiff/(sPooled*sdFactor)
  }
         )
}
#######################################################################
#meanCompOuter.r
meanCompOuter<- function(A){            

  #compute the statistics for the columns
  meanVec <- apply(A, MARGIN = 2, FUN = mean, na.rm = TRUE)
  varVec <- apply(A, MARGIN = 2, FUN = var, na.rm = TRUE)
  nVec <- apply(A, MARGIN = 2, FUN =
        function(e) length(e) - sum(is.na(e)))
  
  #now compute the t statistics
  outer(1:ncol(A), 1:ncol(A), FUN = function(i, j){
    n1 <- nVec[i]
    n2 <- nVec[j]
    mDiff <- meanVec[i] - meanVec[j]
    n <- n1 + n2 - 2
    sdFactor <- sqrt(n1^{-1} + n2^{-1})
    sPooled <- sqrt(((n1 - 1)*(varVec[i]) +
        (n2 - 1)*(varVec[j]))/n)
    mDiff/(sPooled*sdFactor)
  })[upper.tri(matrix(, ncol(A), ncol(A)))] 
}
#######################################################################
#guess5.r
guess5 <- function(n, fOut, drawDate = "1952-1-17"){
  if(weekdays(as.Date(drawDate)) == "Monday")
    drawDate <- as.Date(drawDate) +
        c(0, cumsum(rep(c(3, 4), ceiling((n - 1)/2),
        length.out = n - 1)))
  else
    drawDate <- as.Date(drawDate) +
        c(0, cumsum(rep(c(4, 3), ceiling((n - 1)/2),
        length.out = n - 1)))

  Machine <- sample(c("A", "B"), n, replace = TRUE)
  Set <- sample(1:10, n, replace = TRUE)
  Day <- substr(weekdays(drawDate), start = 1, stop = 1)
  info <- cbind(as.character(drawDate), Machine, Set, Day)
  A <- replicate(n*10, sample.int(40, 5))
  for(i in 1:n) {
    cat(info[i, ], "\n", file = fOut, append = TRUE)
    write(A[, (10*i - 9):(10*i)], file = fOut, ncolumns = 5, append = TRUE)
  }
}

drawData <- as.integer(unlist(strsplit(readLines("guess5.txt")[-seq(1, 1090, 11)],
split = " ")))
#######################################################################
#hyperChi.r
hyperChi <-function (O, K, k){
  chistat <- (K - 1)*chisq.test(O)[[1]]/(K - k)
  names(chistat) <- NULL
  list(chistat, 1 - pchisq(chistat, K - 1))
}
#######################################################################
rngFM <- function(N, seed){
  .C("rngFM", as.integer(N), as.integer(seed))
}
