R/ModelTest.R

Defines functions ModelTest

Documented in ModelTest

#' ModelTest
#' 
#' This function takes the (1) type of model ("TREE" for Tree,"GLM" for Logreg), (2) the pre-fitted model for use in prediction, and (3) the test set to run predictions on
#' 
#' @param type The type of statistical method being used. "TREE" for CART or "GLM" for Logistic Regression.
#' @param model The actual fit model to calculate predict() on
#' @param test The test set for predictions to be made on
#' 
#' @export

ModelTest <- function(type,model,test){
  out <- list()
  
  if(type=="TREE"){
    
    
    preds <- stats::predict(model,test,type='prob')
    predc <- stats::predict(model,test,type='class')
    
    preds <- data.frame(preds)
    colnames(preds) <- c("PrL","PrW")
    
    if(is.null(test$strWL)==TRUE){
      
      tab <- data.frame(Date=test$Date,Team=test$Team,PrL=preds$PrL,PrW=preds$PrW,PrWL=predc)
      tab[3:4] <- round(tab[3:4],digits = 3)
      
    } else if(is.null(test$strWL)==FALSE){
      
      tab <- data.frame(Date=test$Date,Team=test$Team,PrL=preds$PrL,PrW=preds$PrW,PrWL=predc,TrWL=test$strWL)
      tab[3:4] <- round(tab[3:4],digits = 3)    }
    
  } else if(type=="GLM"){
    
    z <- qnorm(.99)
    
    preds <- stats::predict(model,test,type='response',se.fit=TRUE)
    
    low <- preds$fit - z*preds$se.fit
    high <- preds$fit + z*preds$se.fit
    
    if(is.null(test$strWL)==TRUE){
      
      tab <- data.frame(Date=test$Date,Team=test$Team,Lo=low,Avg=preds$fit,Hi=high,PrWL=ifelse(preds$fit>.5,"1","0"))
      tab[3:5] <- round(tab[3:5],digits = 3)
    } else if(is.null(test$strWL)==FALSE){
      
      tab <- data.frame(Date=test$Date,Team=test$Team,Lo=low,Avg=preds$fit,Hi=high,PrWL=ifelse(preds$fit>.5,"1","0"),TrWL=test$strWL)
      tab[3:5] <- round(tab[3:5],digits = 3)
      
    }
    
  }
  
  out[[1]] <- tab
  return(out)
}
dennist2/MerlinV2 documentation built on Dec. 23, 2019, 6:36 p.m.