R/myStatsHelpers.R

Defines functions my.sum.of.squared.errors my.errors.numeric my.degrees.of.freedom

Documented in my.degrees.of.freedom my.errors.numeric my.sum.of.squared.errors

#' Get the degrees of freedom of a sample or population.
#' @importFrom assertthat assert_that
#' @param src.vsctor ; A numeric vector to calculate the degrees of freedom over.
#' @param is.sample ; A boolean indicating that src.vector is a sample, not a population.
#' @param npe ; A numeric, how many parameters are estimated. For variance, we
#' estimate 1 parameter (the mean) from the data. In linear regression, we estimate
#' parameters from the data (slope and intercept).
#' @return numeric ; The degrees of freedom.
#' @export
my.degrees.of.freedom <- function(src.vsctor, is.sample = TRUE, npe = 1) {
  assert_that(is.numeric(src.vsctor))
  assert_that(is.vector(src.vsctor))
  assert_that(is.logical(is.sample))
  assert_that(is.numeric(npe))
  return(length(src.vsctor) - ifelse(is.sample, npe, 0))
}


#' Calculate the errors from the mean.
#' @importFrom assertthat assert_that
#' @param object ; A numeric vector to calculate the erors for.
#' @return numeric vector ; The errors of the response values compared to the mean.
#' @export
my.errors.numeric <- function(object) {
  assert_that(is.numeric(object))
  assert_that(is.vector(object))
  dst.mean <- my.arithmetic.mean(object)
  return(object - dst.mean)
}


#' Calculate the sum of squared errors. This uses 'my.errors(object)'.
#' @param object ; An object to calculate the sum of squared errors for.
#' @return numeric ; The sum of squared errors.
#' @export
my.sum.of.squared.errors <- function(object) {
  dst.errors <- my.errors(object)
  return(sum(dst.errors^2))
}
vanNijnatten/Learn-Statistics documentation built on Dec. 23, 2021, 2:09 p.m.