R/ols-fitted-line-properties.R

Defines functions ols_plot_reg_line ols_reg_line

Documented in ols_plot_reg_line ols_reg_line

#' Simple linear regression line
#'
#' @description
#' Plot to demonstrate that the regression line always passes  through mean of
#' the response and predictor variables.
#'
#' @param response Response variable.
#' @param predictor Predictor variable.
#' @param print_plot logical; if \code{TRUE}, prints the plot else returns a plot object.
#'
#' @section Deprecated Function:
#' \code{ols_reg_line()} has been deprecated. Instead use \code{ols_plot_reg_line()}.
#'
#' @examples
#' ols_plot_reg_line(mtcars$mpg, mtcars$disp)
#'
#' @importFrom ggplot2 labs geom_smooth
#'
#' @export
#'
ols_plot_reg_line <- function(response, predictor, print_plot = TRUE) {

  resp        <- l(deparse(substitute(response)))
  preds       <- l(deparse(substitute(predictor)))
  m_predictor <- round(mean(predictor), 2)
  m_response  <- round(mean(response), 2)
  x           <- NULL
  y           <- NULL
  d2          <- data.frame(x = m_predictor, y = m_response)
  d           <- data.frame(x = predictor, y = response)

  p <-
    ggplot(d, aes(x = x, y = y)) + geom_point(fill = "blue") +
    xlab(paste0(preds)) + ylab(paste0(resp)) + labs(title = "Regression Line") +
    geom_point(data = d2, aes(x = x, y = y), color = "red", shape = 2, size = 3) +
    geom_smooth(method = "lm", se = FALSE)

  if (print_plot) {
    print(p)
  } else {
    return(p)
  }

}


#' @export
#' @rdname ols_plot_reg_line
#' @usage NULL
#'
ols_reg_line <- function(response, predictor) {
  .Deprecated("ols_plot_reg_line()")
}

Try the olsrr package in your browser

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

olsrr documentation built on Feb. 10, 2020, 5:07 p.m.