── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.6
✔ forcats 1.0.1 ✔ stringr 1.6.0
✔ ggplot2 4.0.1 ✔ tibble 3.3.1
✔ lubridate 1.9.4 ✔ tidyr 1.3.2
✔ purrr 1.2.1
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
Attaching package: 'purrrfect'
The following objects are masked from 'package:base':
replicate, tabulate
sim_results <- expand.grid(
mu = c(-2,-1,0,1,2),
n = c(5, 10, 20, 40 ,80),
.rep = 1:10000
) %>%
mutate(
y=map2(mu, n, ~rnorm(.y, mean = .x, sd=1)),
theta_true = mu^2,
ybar1 = map_dbl(y, mean),
ybar2 = map_dbl(y, ~ mean(.x^2)),
theta1_hat = ybar1^2-1/n,
theta2_hat = ybar2-1
)
summary_results <- sim_results %>%
group_by(mu, n, theta_true) %>%
summarize(
mean_theta1 = mean(theta1_hat),
mean_theta2 = mean(theta2_hat),
bias_theta1 = mean(theta1_hat - theta_true),
bias_theta2 = mean(theta2_hat - theta_true),
var_theta1 = var(theta1_hat),
var_theta2 = var(theta2_hat),
.groups = "drop"
)
summary_results
# A tibble: 25 × 9
mu n theta_true mean_theta1 mean_theta2 bias_theta1 bias_theta2
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 -2 5 4 3.99 3.98 -0.0133 -0.0173
2 -2 10 4 3.99 3.99 -0.00761 -0.00915
3 -2 20 4 4.02 4.03 0.0234 0.0278
4 -2 40 4 4.00 3.99 -0.00374 -0.00537
5 -2 80 4 4.01 4.01 0.00996 0.0122
6 -1 5 1 1.00 1.01 0.00325 0.0104
7 -1 10 1 0.997 1.01 -0.00342 0.00661
8 -1 20 1 1.000 1.00 -0.000250 0.00282
9 -1 40 1 1.00 1.00 0.000687 0.0000771
10 -1 80 1 1.000 0.997 -0.0000393 -0.00283
# ℹ 15 more rows
# ℹ 2 more variables: var_theta1 <dbl>, var_theta2 <dbl>
have a good morning/day/night !