R/reg_regression.R

Defines functions evaluate.regression regression

Documented in regression

#'@title Regression
#'@description Ancestor class for regression problems
#'@param attribute attribute target to model building
#'@return regression object
#'@examples
#'#See ?reg_dtree for a regression example using a decision tree
#'@export
regression <- function(attribute) {
  obj <- predictor()
  obj$attribute <- attribute
  class(obj) <- append("regression", class(obj))
  return(obj)
}

#'@export
evaluate.regression <- function(obj, values, prediction, ...) {
  MSE <- function (actual, prediction) {
    if (length(actual) != length(prediction))
      stop("actual and prediction have different lengths")
    n <- length(actual)
    res <- mean((actual - prediction)^2)
    res
  }

  sMAPE <- function (actual, prediction) {
    if (length(actual) != length(prediction))
      stop("actual and prediction have different lengths")
    n <- length(actual)
    res <- (1/n) * sum(abs(actual - prediction)/((abs(actual) +
                                                    abs(prediction))/2))
    res
  }

  R2 <- function (actual, prediction) {
    if (length(actual) != length(prediction))
      stop("actual and prediction have different lengths")
    res <-  1 - sum((prediction - actual)^2)/sum((mean(actual) - actual)^2)
    res
  }


  result <- list(values=values, prediction=prediction)

  result$smape <- sMAPE(values, prediction)
  result$mse <- MSE(values, prediction)
  result$R2 <- R2(values, prediction)

  result$metrics <- data.frame(mse=result$mse, smape=result$smape, R2 = result$R2)

  return(result)
}

Try the daltoolbox package in your browser

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

daltoolbox documentation built on May 29, 2024, 1:57 a.m.