R/ModelTester.R

Defines functions ModelTester

Documented in ModelTester

#' ModelTest
#' 
#' This function is used to test models historically.
#' 
#' @param mod_type Can be (a) "TREE" for CART model or (b) "GLM" for Logistic model
#' @param model The actual fitted model (must correspond to mod_type)
#' @param test The unseen dataset to run predictions on
#' 
#' 
#' @export

ModelTester <- function(mod_type,model,test){
  out <- list()
  
  if(mod_type=="GLM"){
    
    z <- qnorm(.99)
    preds <- stats::predict(model,test,type='response',se.fit=TRUE)
    
    lower <- preds$fit - z*preds$se.fit
    upper <- preds$fit + z*preds$se.fit
    
    dum <- rep("0",length(preds$fit))
    dum[preds$fit>.5]="1"
    
    table <- data.frame(Date=test$Date,Team=test$Team,Lo=lower,Avg=preds$fit,Hi=upper,PrWL=dum,TrWL=test$strWL)
    
    matrix <- table(dum,test$strWL)
    
    out[[1]] <- table
    out[[2]] <- matrix
    
  } else if(mod_type=="TREE"){
    
    preds <- stats::predict(model,test,type='prob')
    predsC <- stats::predict(model,test,type='class')
    
    preds <- data.frame(preds)
    colnames(preds) <- c("PrL","PrW")
    
    table <- data.frame(Date=test$Date,Team=test$Team,PrL=preds$PrL,PrW=preds$PrW,PrWL=predsC,TrWL=test$strWL)
    
    matrix <- table(predsC,test$strWL)
    
    out[[1]] <- table
    out[[2]] <- matrix
  }
  return(out)  
  
}
dennist2/MerlinV1 documentation built on Dec. 11, 2019, 8:41 p.m.