get_regression_points: Get regression points

View source: R/regression_functions.R

get_regression_pointsR Documentation

Get regression points

Description

Output information on each point/observation used in an lm() regression in "tidy" format. This function is a wrapper function for broom::augment() and renames the variables to have more intuitive names.

Usage

get_regression_points(
  model,
  digits = 3,
  print = FALSE,
  newdata = NULL,
  ID = NULL
)

Arguments

model

an lm() model object

digits

number of digits precision in output table

print

If TRUE, return in print format suitable for R Markdown

newdata

A new data frame of points/observations to apply model to obtain new fitted values and/or predicted values y-hat. Note the format of newdata must match the format of the original data used to fit model.

ID

A string indicating which variable in either the original data used to fit model or newdata should be used as an identification variable to distinguish the observational units in each row. This variable will be the left-most variable in the output data frame. If ID is unspecified, a column ID with values 1 through the number of rows is returned as the identification variable.

Value

A tibble-formatted regression table of outcome/response variable, all explanatory/predictor variables, the fitted/predicted value, and residual.

See Also

augment(), get_regression_table(), get_regression_summaries()

Examples

library(dplyr)
library(tibble)

# Convert rownames to column
mtcars <- mtcars %>%
  rownames_to_column(var = "automobile")

# Fit lm() regression:
mpg_model <- lm(mpg ~ cyl, data = mtcars)

# Get information on all points in regression:
get_regression_points(mpg_model, ID = "automobile")

# Create training and test set based on mtcars:
training_set <- mtcars %>%
  sample_frac(0.5)
test_set <- mtcars %>%
  anti_join(training_set, by = "automobile")

# Fit model to training set:
mpg_model_train <- lm(mpg ~ cyl, data = training_set)

# Make predictions on test set:
get_regression_points(mpg_model_train, newdata = test_set, ID = "automobile")

moderndive/moderndive documentation built on Feb. 20, 2024, 8:13 p.m.