-
Notifications
You must be signed in to change notification settings - Fork 0
/
jackknife_sim.R
62 lines (48 loc) · 1.38 KB
/
jackknife_sim.R
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
library(tidyverse)
library(furrr)
set.seed(324628764)
biased_sd <- function(x) {
sqrt(sum((x - mean(x)) ^ 2) / length(x))
}
jackknife_sd <- function(x) {
ps <- numeric(length(x))
for (ii in 1:length(x)) {
x_sub <- x[-ii]
ps[ii] <- length(x) * biased_sd(x) - length(x_sub) * biased_sd(x_sub)
}
return(mean(ps))
}
sim_sds <- function(ii, xbar, xsd, n) {
x <- rnorm(n, xbar, xsd)
x_sd <- sd(x)
x_biased_sd <- biased_sd(x)
x_jackkife_sd <- jackknife_sd(x)
return(tibble(x_sd, x_biased_sd, x_jackkife_sd))
}
plan(multisession, workers = 5)
sims <- future_map(.x = seq_len(1e4),
.f = sim_sds,
xbar = 0,
xsd = 1,
n = 50,
.progress = TRUE,
.options = furrr_options(seed = TRUE)) |>
list_rbind()
plan(sequential)
sims_long <- pivot_longer(sims, cols = -x_sd)
ggplot(sims_long, aes(x = x_sd, y = value, color = name)) +
geom_abline(slope = 1, intercept = 0) +
geom_point() +
facet_grid(name ~ .) +
cowplot::theme_cowplot()
sims <- sims |>
mutate(d_biased = x_biased_sd - x_sd,
d_jackknife = x_jackkife_sd - x_sd)
sims |>
select(starts_with("d")) |>
pivot_longer(cols = everything()) |>
ggplot(aes(value)) +
geom_vline(xintercept = 0, color = "red") +
geom_histogram(bins = 50) +
facet_grid(. ~ name) +
cowplot::theme_cowplot()