Created
May 17, 2016 15:41
-
-
Save sebastiansauer/98321f319eb118edb8d8ba43fe16e5b3 to your computer and use it in GitHub Desktop.
This file contains 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
plot_beta <- function(mean_1 = 0, mean_2 = 1, | |
sd_1 = 1, sd_2 = 1, | |
fill_alpha = "red", | |
fill_beta = "green", | |
alpha = .05, zend = 3, | |
title = ""){ | |
# plots alpha error and beta error in two normal curves | |
# arguments: | |
# mean_1, mean_2: mean values of normal curves | |
# sd_1, sd_2: sds of normal curves | |
# fill_alpha, fill_beta: fill color for alpha and beta error | |
# zend: end value (as z value) for alpha error shaded area | |
library(ggplot2) | |
library(cowplot) | |
zkrit <- qnorm(1 - alpha) | |
# from 3sd left to 3sd right to the mean | |
x_1 <- seq(from = mean_1 - 3 * sd_1, to = mean_1 + 3*sd_1, by = .01) | |
MyDF_1 <- data.frame(x = x_1, y = dnorm(x_1, mean = mean_1, sd = sd_1)) | |
x_2 <- seq(from = mean_2 - 3 * sd_2, to = mean_2 + 3*sd_2, by = .01) | |
MyDF_2 <- data.frame(x = x_2, y = dnorm(x_2, mean = mean_2, sd = sd_2)) | |
MyDF_1_subset <- subset(MyDF_1, | |
x >= mean_1 + zkrit * sd_1 & x < mean_1 + | |
zend * sd_1) | |
MyDF_2_subset <- subset(MyDF_2, | |
x >= mean_1 - zend * sd_1 & x < mean_1 + | |
zkrit * sd_1) | |
p1 <- ggplot(MyDF_1, aes(x = x, y = y)) + geom_line() + | |
geom_area(data = MyDF_1_subset, | |
aes(y = y), fill = fill_alpha, color = NA, alpha = .5) + | |
ylab("") + xlab("") + | |
geom_line(data = MyDF_2, aes(x = x, y = y)) + | |
geom_area(data = MyDF_2_subset, | |
aes(y = y), fill = fill_beta, color = NA, alpha = .5) + | |
ggtitle(title) | |
print(p1) | |
} | |
#debug(plot_beta) | |
plot_beta(sd_1 = 0.4, | |
sd_2 = 0.4) | |
plot_beta(sd_1 = 0.6, | |
sd_2 = 0.6) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment