Commit 6b2b6e22 authored by Florian Huber's avatar Florian Huber
Browse files

Hypothesis testing R script.

parent ea357ed3
## ---- include = FALSE----------------------------------------------------
library(tidyverse)
## ------------------------------------------------------------------------
rnorm(3)
rbinom(n = 1, size = 3, prob = 0.5)
rt(3, df = 2)
## ------------------------------------------------------------------------
v <- data.frame(x = rnorm(10000))
mean(v$x)
sd(v$x)
ggplot(v, aes(x = x)) +
geom_histogram(aes(y = ..density..)) +
geom_density(colour = "red")
## ------------------------------------------------------------------------
dnorm(0)
## ------------------------------------------------------------------------
pnorm(0)
## ------------------------------------------------------------------------
qnorm(0.5)
qnorm(0.975)
## ------------------------------------------------------------------------
samplesize <- 3
my_tstats <- replicate(10000, rnorm(samplesize), simplify = FALSE)
my_tstats <- tibble(sample = my_tstats)
head(my_tstats)
## ------------------------------------------------------------------------
my_tstats$mean <- map_dbl(my_tstats$sample, mean)
my_tstats$sem <- map_dbl(my_tstats$sample, function(x) {
sd(x) / sqrt(length(x))
})
## ------------------------------------------------------------------------
get_tstatistic <- function(x) {
sqrt(length(x)) * (mean(x)/sd(x))
}
my_tstats$tstatistic <- map_dbl(my_tstats$sample, get_tstatistic)
## ------------------------------------------------------------------------
my_tstats$rt <- rt(10000, df = samplesize)
my_tstats
ggplot(my_tstats) +
geom_histogram(aes(x = rt), alpha = 0.5, binwidth = 0.1, fill = "red") +
geom_histogram(aes(x = tstatistic), alpha = 0.5, binwidth = 0.1,
fill = "grey") +
coord_cartesian(xlim = c(-5, 5)) +
theme_bw()
## ------------------------------------------------------------------------
t_quantile <- qt(0.975, df = samplesize-1)
## ------------------------------------------------------------------------
my_tstats$lower <- my_tstats$mean - t_quantile * my_tstats$sem
my_tstats$upper <- my_tstats$mean + t_quantile * my_tstats$sem
my_tstats
## ------------------------------------------------------------------------
my_tstats$contains_mean <- my_tstats$lower < 0 & my_tstats$upper > 0
sum(my_tstats$contains_mean) / nrow(my_tstats)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment