This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Load packages | |
library(tidyverse) | |
library(gganimate) | |
library(tidymodels) | |
set.seed(2021) | |
# Generate true relationship between x and y as piecewise | |
piecewise <- function(x) { | |
case_when( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(furrr) | |
x <- numeric() | |
for (i in 1:20) { | |
future::plan(multisession, workers = i) | |
result <- system.time({ | |
r <- furrr::future_map(1:100, | |
~ sum(rnorm(1e7)), | |
.options = furrr_options(seed=TRUE)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Display data frame as a nicely formatted and interactive GT table | |
display <- function(df){ | |
df |> | |
gt::gt() |> | |
gt::opt_interactive(use_search = TRUE, | |
use_compact_mode = TRUE, | |
use_text_wrapping = TRUE) |> | |
gt::opt_stylize(style = 2) |> | |
gt::cols_width(everything() ~ px(250)) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(ggplot2) | |
library(patchwork) # For composing plots | |
# See https://patchwork.data-imaginist.com/ | |
library(ggtext) # For formatting text using markdown syntax | |
# See https://wilkelab.org/ggtext/ | |
# Make a ggplot histogram from a vector of data | |
gg_hist <- function(x){ | |
data.frame(x) |> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(MASS) | |
library(plotrix) | |
library(tidyverse) | |
library(gganimate) | |
#################################### | |
## Example 1 (Data by Group with means and estimated means) | |
#################################### | |
## Number of groups, observations per group |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gen_dataset <- function(){ | |
b0 <- 3 | |
b1 <- 7 | |
x <- 1:10 | |
linear_predictor <- b0 + b1*x | |
y <- rnorm(n = length(linear_predictor), mean=linear_predictor, sd=8) | |
df <- tibble(x,y) | |
# Fit OLS model | |
ols_model <- lm(y ~ x, data=df) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Using manim community edition | |
from manim import * | |
config.frame_width = 22 | |
text1 = MathTex(r"\begin{bmatrix} 3 & 9 & 5 \\ 9 & 4 & 2 \\ 7 & 3 & 6 \end{bmatrix} \begin{bmatrix} 5 & 7 & 2 & 4 \\ 3 & 6 & 9 & 8 \\ 2 & 7 & 1 & 4 \end{bmatrix}") | |
text2 = MathTex(r"\begin{bmatrix} 3 & 9 & 5 \\ 9 & 4 & 2 \\ 7 & 3 & 6 \end{bmatrix} \begin{bmatrix} 5 & 7 & 2 & 4 \\ 3 & 6 & 9 & 8 \\ 2 & 7 & 1 & 4 \end{bmatrix}", r"&=", r"\begin{bmatrix} 3 \\ 9 \\ 7 \end{bmatrix} \begin{bmatrix} 5 & 7 & 2 & 4 \end{bmatrix} + \begin{bmatrix} 9 \\ 4 \\ 3 \end{bmatrix} \begin{bmatrix} 3 & 6 & 9 & 8 \end{bmatrix} + \begin{bmatrix} 5 \\ 2 \\ 6 \end{bmatrix} \begin{bmatrix} 2 & 7 & 1 & 4 \end{bmatrix} \\") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(gganimate) | |
set.seed(777) | |
points <- 2* 1e3 | |
x <- runif(points, min = -1, max=1) | |
y <- runif(points, min=-1, max=1) | |
inside <- x^2 + y^2 < 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
set.seed(1) | |
df <- tibble(x=rnorm(2000), | |
y=rnorm(2000)) %>% | |
mutate(flag = x+y > 1.5) | |
df %>% | |
ggplot() + |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidymodels) | |
library(tidyverse) | |
library(discrim) | |
library(gganimate) | |
# grid size | |
n <- 100 | |
parabolic_grid <- | |
expand.grid(X1 = seq(-5, 5, length = n), |
NewerOlder