knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  warning = FALSE,
  gganimate = list(nframes = 50)
)

library(ggplot2)

theme_set(theme_minimal(base_size = 21))

if(require(showtext)){

  sysfonts::font_add_google("IBM Plex Sans", "plex")
  showtext::showtext_auto()

}

Generating data set

The main function is sim_xy, you need to define:

library(klassets)
library(ggplot2)
library(patchwork)

set.seed(123)

df_default <- sim_xy()

df_default

plot(df_default)

We can modify the data frame to get other types of relationships.

df <- sim_xy(n = 1000, x_dist = runif)

df <- dplyr::mutate(df, y = y + 2*sin(5 * x) + sin(10 * x))

plot(df)

Fit regression algorithms

Linear Regression

This function uses stats::lm to fit a model.

df_lr <- fit_linear_model(df)

df_lr

plot(df_lr)

By default the model use order = 1 of the variables, i.e, response ~ x + y. We can get a better fit if we increase the order.

df_lr2 <- fit_linear_model(df, order = 4, stepwise = TRUE)

attr(df_lr2, "model")

plot(df_lr2)

Testing various orders.

orders <- c(1, 2, 3, 4)

orders |> 
  purrr::map(fit_linear_model, df = df) |> 
  purrr::map(plot) |> 
  purrr::reduce(`+`) +
  patchwork::plot_layout(guides = "collect") &
  theme_void() + theme(legend.position = "none")

Regression Tree

Internally the functions uses partykit::ctree.

df_rt <- fit_regression_tree(df)

plot(df_rt)

plot(attr(df_rt, "model"))

Linear Model Tree

Internally the functions uses partykit::mltree.

df_lmt <- fit_linear_model_tree(df)

plot(df_lmt)

plot(attr(df_lmt, "model"))

Random Forest

Internally the functions uses partykit::cforest.

df_rf <- fit_regression_random_forest(df)

plot(df_rf)

# this will be relative similar to `fit_regression_tree` due 
# we are using 1 tree
plot(fit_regression_random_forest(df, ntree = 1, maxdepth = 3))

Local Polynomial Regression Fitting (LOESS)

Using stats::loess.

df_loess <- fit_loess(df)

plot(df_loess)

Multivariate Adaptive Regression Splines (MARS)

Using earth::earth.

df_mars <- fit_mars(df)

plot(df_mars)


jbkunst/klassets documentation built on Dec. 7, 2022, 9:18 p.m.