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() }
The main function is sim_xy
, you need to define:
stats::runif
or purrr::partial(stats::rnorm, mean = 5, sd = 1)
.purrr::partial(stats::rnorm, sd = 0.5)
.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)
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")
Internally the functions uses partykit::ctree
.
df_rt <- fit_regression_tree(df) plot(df_rt) plot(attr(df_rt, "model"))
Internally the functions uses partykit::mltree
.
df_lmt <- fit_linear_model_tree(df) plot(df_lmt) plot(attr(df_lmt, "model"))
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))
Using stats::loess
.
df_loess <- fit_loess(df) plot(df_loess)
Using earth::earth
.
df_mars <- fit_mars(df) plot(df_mars)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.