linearRegress: Linear Regression.

Description Usage Arguments Details Value Examples

View source: R/linearRegress.R

Description

Runs a linear regression model, and either:

  1. fits a basic lm() model and shows diagnostics and model fit

  2. Uses the tidymodels approach: evaluates it on training and testing set, and tunes hyperparameters.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
linearRegress(
  response = response,
  computeMarginalEffects = FALSE,
  data = df,
  train = train_df,
  test = test_df,
  tidyModelVersion = FALSE,
  recipe = rec,
  folds = folds,
  evalMetric = "rmse"
)

Arguments

response

Character. The variable that is the response for analysis.

computeMarginalEffects

Logical. Compute marginal effects for lm model?

data

The entire data frame. Used for tidyModelVersion = FALSE.

train

Data frame/tibble. The training data set.

test

Data frame/tibble. The testing data set.

tidyModelVersion

Logical. Run a tidymodel version of linear regression? If yes, will tune hyperparameters and return a tidymodels regression model. If no, will fit an lm() object and return output based on that computation.

recipe

A recipes::recipe object.

folds

A rsample::vfolds_cv object.

evalMetric

Character. The regression metric you want to evaluate the model's accuracy on (tidymodels only). Default is RMSE. Can choose from the following:

  • rmse

  • mae

  • rsq

  • mase

  • ccc

  • icc

  • huber_loss

Details

Note: Tidymodels version tunes the following parameters:

Value

A list.

If tidyModelVersion = TRUE:

If tidyModelVersion = FALSE:

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
library(easytidymodels)
library(dplyr)
library(recipes)
utils::data(penguins, package = "modeldata")

#Define your response variable and formula object here
resp <- "bill_length_mm"
formula <- stats::as.formula(paste(resp, ".", sep="~"))

#Split data into training and testing sets
split <- trainTestSplit(penguins, responseVar = resp)

#Create recipe for feature engineering for dataset, varies based on data working with
rec <- recipe(formula, split$train) %>% prep()
train_df <- bake(rec, split$train)
test_df <- bake(rec, split$test)
folds <- cvFolds(train_df)

#Fit a linear regression model (commented out only due to long run time)
#linReg <- linearRegress(recipe = rec, response = resp, data = penguins, tidyModelVersion = FALSE,
#folds = folds, train = train_df, test = test_df, evalMetric = "rmse")

#Visualize training data and its predictions
#linReg$trainPred %>% select(.pred, !!resp)

#View how model metrics for RMSE, R-Squared, and MAE look for training data
#linReg$trainScore

#Visualize testing data and its predictions
#linReg$testPred %>% select(.pred, !!resp)

#View how model metrics for RMSE, R-Squared, and MAE look for testing data
#linReg$testScore

#See the final model chosen by KNN based on optimizing for your chosen evaluation metric
#linReg$final

#See how model fit looks based on another evaluation metric
#linReg$tune %>% tune::show_best("mae")

amanda-park/easytidymodels documentation built on Dec. 13, 2021, 11:28 a.m.