Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions LinaKramer/Assignment1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Utrecht
2022
70 changes: 70 additions & 0 deletions LinaKramer/Assignment1.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
title: "Monte Carlo simulation (exercise1)"
author: "Lina Kramer"
date: "9/14/2022"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r}
library(future)
library(furrr)
library(dplyr)
library(magrittr)
```

```{r}

nsim = 100 # 100 samples
plan(multisession)

# start future mapping
SIM <- future_map(1:nsim, function(x){
x <- rnorm(5000, mean = 0, sd = 1)
M <- mean(x)
DF <- length(x) - 1
SE <- 1 / sqrt(length(x))
INT <- qt(.975, DF) * SE
return(c(mean = M,
bias = M - 0,
std.err = SE,
lower = M - INT,
upper = M + INT,
cov = M - INT < 0 & 0 < M + INT))
},
.options = furrr_options(seed = 123),
.progress = TRUE) %>%
do.call("rbind", args = .) %>%
as_tibble


```

```{r}
SIM %>% colMeans
```
```{r}
SIM %>% filter(!cov)
```

```{r}
library(DT)
SIM %>%
round(4) %>%
datatable()
```

```{r}
library(ggplot2)
limits <- aes(ymax = SIM$upper, ymin = SIM$lower)
SIM %>% mutate(covered = as.factor(cov)) %>%
ggplot(aes(y=mean, x=1:100, colour = covered)) +
geom_hline(aes(yintercept = 0), color = "dark grey", size = 2) +
geom_pointrange(limits) +
xlab("Simulations 1-100") +
ylab("Means and 95% Confidence Intervals")
```