R/postResample.R

postResample <- function(pred, obs)
{

  isNA <- is.na(pred)
  pred <- pred[!isNA]
  obs <- obs[!isNA]

  if(!is.factor(obs) & is.numeric(obs))
    {
      if(length(obs) + length(pred) == 0)
        {
          out <- rep(NA, 2)
        } else {
          if(length(unique(pred)) < 2 || length(unique(obs)) < 2)
            {
              resamplCor <- NA
            } else {
              resamplCor <- try(cor(pred, obs, use = "pairwise.complete.obs"), silent = TRUE)
              if(class(resamplCor) == "try-error") resamplCor <- NA 
            }
          mse <- mean((pred - obs)^2)
          n <- length(obs)

          out <- c(sqrt(mse), resamplCor^2)
        }
      names(out) <- c("RMSE", "Rsquared")    
    } else {
      if(length(obs) + length(pred) == 0)
        {
          out <- rep(NA, 2)
        } else {
          pred <- factor(pred, levels = levels(obs))  
          library(e1071)   
          out <- unlist(classAgreement(table(obs, pred)))[c("diag", "kappa")]
        }
      names(out) <- c("Accuracy", "Kappa")         
    }
  if(any(is.nan(out))) out[is.nan(out)] <- NA
  out
}

Try the caret package in your browser

Any scripts or data that you put into this service are public.

caret documentation built on May 2, 2019, 5:47 p.m.