#' Get the prediction formula for a for a regression.
#' @importFrom assertthat assert_that
#' @param object ; A regression object.
#' @return function ; A function that gives the response values for a given explanatory variable.
#' @export
my.regression.prediction.formula.linear_regression <- function(object) {
assert_that(inherits(object, "linear_regression"))
assert_that(inherits(object, "regression"))
# I put everything that I want to return in a function and execute it, to make a
# separate environment/scope with the variables 'intercept' and 'slope'.
return((function() {
dst.intercept <- my.intercept(object)
dst.slope <- my.slope(object)
dst.fnctn <- function(src.x) {
return(dst.intercept + (dst.slope * src.x))
}
return(dst.fnctn)
})())
}
#' Get the Y-intercept for a regression.
#' @importFrom assertthat assert_that
#' @param object ; A regression object.
#' @return numeric ; A function that gives the response values for a given explanatory variable.
#' @export
my.intercept.linear_regression <- function(object) {
assert_that(inherits(object, "linear_regression"))
assert_that(inherits(object, "regression"))
return(my.arithmetic.mean(object$y) - (my.slope(object) * my.arithmetic.mean(object$x)))
}
#' Get the slope for a regression.
#' @importFrom assertthat assert_that
#' @param object ; A regression object.
#' @return numeric ; The slope of the given regression.
#' @export
my.slope.linear_regression <- function(object) {
assert_that(inherits(object, "linear_regression"))
assert_that(inherits(object, "regression"))
dst.mean.x <- my.arithmetic.mean(object$x)
dst.mean.y <- my.arithmetic.mean(object$y)
return(sum((object$x - dst.mean.x) * (object$y - dst.mean.y))/sum((object$x -
dst.mean.x)^2))
}
#' Get the covariance for a regression.
#' (Only useful for classifying a corelation/regression as positive/negative or no relationship.)
#' @importFrom assertthat assert_that
#' @param object ; A regression object.
#' @return numeric ; The covariance of a given regression.
#' @export
my.covariance.linear_regression <- function(object) {
assert_that(inherits(object, "linear_regression"))
assert_that(inherits(object, "regression"))
dst.mean.x <- my.arithmetic.mean(object$x)
dst.mean.y <- my.arithmetic.mean(object$y)
dst.degrees.of.freedom <- my.degrees.of.freedom(object$x, object$is.sample)
return(sum((object$x - dst.mean.x) * (object$y - dst.mean.y))/dst.degrees.of.freedom)
}
#' Calculate the correlation of the regression.
#' @importFrom assertthat assert_that
#' @param object ; A regression object.
#' @return numeric ; The correlation of the response values compared to the linear regression line.
#' @export
my.correlation.linear_regression <- function(object) {
assert_that(inherits(object, "linear_regression"))
assert_that(inherits(object, "regression"))
# return(my.covariance(object)/(sqrt(my.variance(object$x)) *
# sqrt(my.variance(object$y))))
dst.y.z_score <- my.z.score(object$y)
dst.x.z_score <- my.z.score(object$x)
dst.degrees.of.freedom <- my.degrees.of.freedom(object$x, object$is.sample)
return((sum(dst.x.z_score * dst.y.z_score))/dst.degrees.of.freedom)
}
#' Calculate the coefficient of determination of a linear regression (also called the R/correlation squared).
#' @importFrom assertthat assert_that
#' @param object ; A regression object.
#' @return numeric ; The coefficient of determination of the linear regression line.
#' @export
my.coefficient.of.determination.linear_regression <- function(object) {
assert_that(inherits(object, "linear_regression"))
assert_that(inherits(object, "regression"))
dst.mean <- my.arithmetic.mean(object$y)
dst.total.sum.of.squares <- sum((object$y - dst.mean)^2) # Should we extract this?
dst.sse <- my.sum.of.squared.errors(object)
return((dst.total.sum.of.squares - dst.sse)/dst.total.sum.of.squares)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.