R/ModCV.R

#' Cross validation of a regression Model using indexes created by Kcross
#'
#' \code{ModCV(index, indexlist, Formula, DFrame, Func, Resp, ...)}
#'
#' @param index numerical index representing length of \code{indexlist} list
#' @param indexlist list created using Kcross, of length K with $train and $test indices
#' @param Formula formula for the model
#' @param DFrame the name of the original data frame
#' @param Func a function, e.g., lm or randomForest (regression model)
#' @param Resp either the column number or the 'name' of the response variable
#' @param ... additional arguments to pass to the model, e.g., hidden=3 for ANN
#'
#' @details Allows k-fold cross validation of a regression model
#' (with continuous response) and stores output in a list. See vignette.

ModCV = function(index, indexlist, Formula, DFrame, Func, Resp, ...){
M1 = Func(Formula, DFrame[indexlist[[index]]$train,], ...)
Predz = predict(M1, newdata=DFrame[indexlist[[index]]$test,])
Actualz = DFrame[indexlist[[index]]$test,Resp]
Error = Predz-Actualz
MSE = mse(Predz,Actualz)
return(list(MSE = MSE, Error = Error, Coef=M1$coef))
}
helophilus/ColsTools documentation built on May 30, 2019, 4:03 p.m.