Marcio Diniz | Michael Luu
Cedars Sinai Medical Center
02 November, 2022
n <- 20
size <- 20
p <- 0.5
data_plot <- data.frame(y = 0:20, prob = dbinom(0:20, size, p),
col = c(rep("y", 5), rep("n", 11), rep("y", 5)))
ggplot(data_plot, aes(x = y, y = prob, fill = col)) +
geom_bar(stat="identity") +
#scale_y_continuous(limits = c(0, 1)) +
scale_x_continuous(breaks = seq(0:n)) +
labs(x = "Y", y = "P(Y = y)") +
theme_bw() + theme(text = element_text(size=20),
legend.text = element_text(size = 12),
legend.title = element_text(size = 12)) +
scale_fill_manual("col", values = c("grey", gg_color_hue(2)[2])) +
theme(legend.position = "none")
n <- 20
size <- 20
p <- 0.5
data_plot <- data.frame(y = 0:20, prob = dbinom(0:20, size, p),
col = c(rep("y", 5), rep("n", 11), rep("y", 5)))
p1 <- ggplot(data_plot, aes(x = y, y = prob, fill = col)) +
geom_bar(stat="identity") +
#scale_y_continuous(limits = c(0, 1)) +
scale_x_continuous(breaks = seq(0:n)) +
labs(x = "Y", y = "P(Y = y)") +
theme_bw() + theme(text = element_text(size=20),
legend.text = element_text(size = 12),
legend.title = element_text(size = 12)) +
scale_fill_manual("col", values = c("grey", gg_color_hue(2)[2])) +
theme(legend.position = "none")
data_plot <- data.frame(y = 0:20, prob = dbinom(0:20, size, p),
col = c(rep("n", 5), rep("n", 11), rep("y", 5)))
p2 <- ggplot(data_plot, aes(x = y, y = prob, fill = col)) +
geom_bar(stat="identity") +
#scale_y_continuous(limits = c(0, 1)) +
scale_x_continuous(breaks = seq(0:n)) +
labs(x = "Y", y = "P(Y = y)") +
theme_bw() + theme(text = element_text(size=20),
legend.text = element_text(size = 12),
legend.title = element_text(size = 12)) +
scale_fill_manual("col", values = c("grey", gg_color_hue(2)[2])) +
theme(legend.position = "none")
data_plot <- data.frame(y = 0:20, prob = dbinom(0:20, size, p),
col = c(rep("y", 5), rep("y", 12), rep("n", 4)))
p3 <- ggplot(data_plot, aes(x = y, y = prob, fill = col)) +
geom_bar(stat="identity") +
#scale_y_continuous(limits = c(0, 1)) +
scale_x_continuous(breaks = seq(0:n)) +
labs(x = "Y", y = "P(Y = y)") +
theme_bw() + theme(text = element_text(size=20),
legend.text = element_text(size = 12),
legend.title = element_text(size = 12)) +
scale_fill_manual("col", values = c("grey", gg_color_hue(2)[2])) +
theme(legend.position = "none")
p1 + p2 + p3 + plot_layout(nrow = 3) +
plot_annotation(tag_levels = "A",
tag_suffix = ".")
If \(\sigma^2 = 10\) and \(n = 10\), then \(Z \sim N(0, 1)\);
z <- 3
pz <- 2*(1 - pnorm(z, 0, 1))
data_plot <- data.frame(x = rep(c(-3, 3)))
ggplot(data_plot, aes(x = x)) +
stat_function(fun = dnorm, args = list(0, 1)) +
geom_area(fill = "blue",
stat = "function", fun = dnorm,
args = list(0, 1),
xlim = c(z, 4),
alpha = 0.5) +
geom_area(fill = "blue",
stat = "function", fun = dnorm,
args = list(0, 1),
xlim = c(-4, -z),
alpha = 0.5) +
labs(y = "Density", x = "Z") +
theme_bw() +
theme(legend.position = "None") +
geom_vline(xintercept =
c(qnorm(0.025, 0, 1), qnorm(0.975, 0, 1)))
If \(\sigma^2 = 100\) and \(n = 10\), then \(Z \sim N(0, 10)\);
z <- 3
pz <- 2*(1 - pnorm(z, 0, 10))
data_plot <- data.frame(x = rep(c(-50, 50)))
ggplot(data_plot, aes(x = x)) +
stat_function(fun = dnorm, args = list(0, 10)) +
geom_area(fill = "blue",
stat = "function", fun = dnorm,
args = list(0, 10),
xlim = c(z, 50),
alpha = 0.5) +
geom_area(fill = "blue",
stat = "function", fun = dnorm,
args = list(0, 10),
xlim = c(-50, -z),
alpha = 0.5) +
labs(y = "Density", x = "Z") +
theme_bw() +
theme(legend.position = "None") +
geom_vline(xintercept =
c(qnorm(0.025, 0, 10), qnorm(0.975, 0, 10)))
If \(\sigma^2 = 1\) and \(n = 10\), then \(Z \sim N(0, 0.1)\);
z <- 3
pz <- 2*(1 - pnorm(z, 0, 0.1))
data_plot <- data.frame(x = rep(c(-3.5, 3.5)))
ggplot(data_plot, aes(x = x)) +
stat_function(fun = dnorm, args = list(0, 0.1)) +
geom_area(fill = "blue",
stat = "function", fun = dnorm,
args = list(0, 0.1),
xlim = c(z, 1),
alpha = 0.5) +
geom_area(fill = "blue",
stat = "function", fun = dnorm,
args = list(0, 0.1),
xlim = c(-1, -z),
alpha = 0.5) +
labs(y = "Density", x = "Z") +
scale_x_continuous(breaks = seq(-3, 3, 1)) +
theme_bw() +
theme(legend.position = "None") +
geom_vline(xintercept =
c(qnorm(0.025, 0, 0.1), qnorm(0.975, 0, 0.1)))
set.seed(1234)
x <- rnorm(50, 2.8, 1)
z <- (mean(x) - 2)/sqrt(1^2/length(x))
t <- (mean(x) - 2)/sqrt(var(x)/length(x))
pz <- 2*(1 - pnorm(z, 0, 1))
pt <- 2*(1 - pt(t, df = (length(x) - 1)))
data_plot <- data.frame(x = rep(c(-5, 5)))
ggplot(data_plot, aes(x = x)) +
stat_function(fun = dnorm, args = list(0, 1)) +
geom_area(fill = "blue",
stat = "function", fun = dnorm,
args = list(0, 1),
xlim = c(z, 5),
alpha = 0.5) +
geom_area(fill = "blue",
stat = "function", fun = dnorm,
args = list(0, 1),
xlim = c(-5, -z),
alpha = 0.5) +
labs(y = "Density", x = "Z") +
theme_bw() +
theme(legend.position = "None") +
geom_vline(xintercept =
c(qnorm(0.025, 0, 1), qnorm(0.975, 0, 1)))
ggplot(data_plot, aes(x = x)) +
stat_function(fun = dnorm, args = list(0, 1)) +
geom_area(fill = "blue",
stat = "function", fun = dnorm,
args = list(0, 1),
xlim = c(z, 5),
alpha = 0.5) +
labs(y = "Density", x = "Z") +
theme_bw() +
theme(legend.position = "None") +
geom_vline(xintercept =
qnorm(0.95, 0, 1))
ggplot(data_plot, aes(x = x)) +
stat_function(fun = dnorm, args = list(0, 1)) +
geom_area(fill = "blue",
stat = "function", fun = dnorm,
args = list(0, 1),
xlim = c(-5, z),
alpha = 0.5) +
labs(y = "Density", x = "Z") +
theme_bw() +
theme(legend.position = "None") +
geom_vline(xintercept =
qnorm(0.05, 0, 1))
set.seed(892)
x <- rnorm(50, 2.2, 1)
z <- (mean(x) - 2)/sqrt(1^2/length(x))
t <- (mean(x) - 2)/sqrt(var(x)/length(x))
pz <- 2*(1 - pnorm(z, 0, 1))
pt <- 2*(1 - pt(t, df = (length(x) - 1)))
data_plot <- data.frame(x = rep(c(-5, 5)))
ggplot(data_plot, aes(x = x)) +
stat_function(fun = dnorm, args = list(0, 1)) +
geom_area(fill = "blue",
stat = "function", fun = dnorm,
args = list(0, 1),
xlim = c(z, 5),
alpha = 0.5) +
geom_area(fill = "blue",
stat = "function", fun = dnorm,
args = list(0, 1),
xlim = c(-5, -z),
alpha = 0.5) +
labs(y = "Density", x = "Z") +
theme_bw() +
theme(legend.position = "None") +
geom_vline(xintercept =
c(qnorm(0.025, 0, 1), qnorm(0.975, 0, 1)))
ggplot(data_plot, aes(x = x)) +
stat_function(fun = dnorm, args = list(0, 1)) +
geom_area(fill = "blue",
stat = "function", fun = dnorm,
args = list(0, 1),
xlim = c(z, 5),
alpha = 0.5) +
labs(y = "Density", x = "Z") +
theme_bw() +
theme(legend.position = "None") +
geom_vline(xintercept =
qnorm(0.95, 0, 1))
ggplot(data_plot, aes(x = x)) +
stat_function(fun = dnorm, args = list(0, 1)) +
geom_area(fill = "blue",
stat = "function", fun = dnorm,
args = list(0, 1),
xlim = c(-5, z),
alpha = 0.5) +
labs(y = "Density", x = "Z") +
theme_bw() +
theme(legend.position = "None") +
geom_vline(xintercept =
qnorm(0.05, 0, 1))
set.seed(2527)
x <- rnorm(100, 2, 1)
z <- (mean(x) - 2)/sqrt(1^2/length(x))
t <- (mean(x) - 2)/sqrt(var(x)/length(x))
pz <- 2*(1 - pnorm(z, 0, 1))
pt <- 2*(1 - pt(t, df = (length(x) - 1)))
ggplot(data_plot, aes(x = x)) +
stat_function(fun = dnorm, args = list(0, 1)) +
geom_area(fill = "blue",
stat = "function", fun = dnorm,
args = list(0, 1),
xlim = c(z, 5),
alpha = 0.5) +
geom_area(fill = "blue",
stat = "function", fun = dnorm,
args = list(0, 1),
xlim = c(-5, -z),
alpha = 0.5) +
labs(y = "Density", x = "Z") +
theme_bw() +
theme(legend.position = "None") +
geom_vline(xintercept =
c(qnorm(0.025, 0, 1), qnorm(0.975, 0, 1)))
ggplot(data_plot, aes(x = x)) +
stat_function(fun = dnorm, args = list(0, 1)) +
geom_area(fill = "blue",
stat = "function", fun = dnorm,
args = list(0, 1),
xlim = c(z, 5),
alpha = 0.5) +
labs(y = "Density", x = "Z") +
theme_bw() +
theme(legend.position = "None") +
geom_vline(xintercept =
qnorm(0.95, 0, 1))
ggplot(data_plot, aes(x = x)) +
stat_function(fun = dnorm, args = list(0, 1)) +
geom_area(fill = "blue",
stat = "function", fun = dnorm,
args = list(0, 1),
xlim = c(-5, z),
alpha = 0.5) +
labs(y = "Density", x = "Z") +
theme_bw() +
theme(legend.position = "None") +
geom_vline(xintercept =
qnorm(0.05, 0, 1))
set.seed(1234)
x <- rnorm(50, 2.8, 1)
z <- (mean(x) - 2)/sqrt(1^2/length(x))
t <- (mean(x) - 2)/sqrt(var(x)/length(x))
pz <- 2*(1 - pnorm(z, 0, 1))
pt <- 2*(1 - pt(t, df = (length(x) - 1)))
data_plot <- data.frame(x = rep(c(-5, 5)))
ggplot(data_plot, aes(x = x)) +
stat_function(fun = dt, args = list((length(x) - 1))) +
geom_area(fill = "blue",
stat = "function", fun = dt,
args = list((length(x) - 1)),
xlim = c(t, 5),
alpha = 0.5) +
geom_area(fill = "blue",
stat = "function", fun = dt,
args = list((length(x) - 1)),
xlim = c(-5, -t),
alpha = 0.5) +
labs(y = "Density", x = "T") +
theme_bw() +
theme(legend.position = "None") +
geom_vline(xintercept =
c(qt(0.025, df = (length(x) - 1)),
qt(0.975, df = (length(x) - 1))))
set.seed(892)
x <- rnorm(50, 2.2, 1)
z <- (mean(x) - 2)/sqrt(1^2/length(x))
t <- (mean(x) - 2)/sqrt(var(x)/length(x))
pz <- 2*(1 - pnorm(z, 0, 1))
pt <- 2*(1 - pt(t, df = (length(x) - 1)))
ggplot(data_plot, aes(x = x)) +
stat_function(fun = dt, args = list((length(x) - 1))) +
geom_area(fill = "blue",
stat = "function", fun = dt,
args = list((length(x) - 1)),
xlim = c(t, 5),
alpha = 0.5) +
geom_area(fill = "blue",
stat = "function", fun = dt,
args = list((length(x) - 1)),
xlim = c(-5, -t),
alpha = 0.5) +
labs(y = "Density", x = "T") +
theme_bw() +
theme(legend.position = "None") +
geom_vline(xintercept =
c(qt(0.025, df = (length(x) - 1)),
qt(0.975, df = (length(x) - 1))))
set.seed(2527)
x <- rnorm(100, 2, 1)
z <- (mean(x) - 2)/sqrt(1^2/length(x))
t <- (mean(x) - 2)/sqrt(var(x)/length(x))
pz <- 2*(1 - pnorm(z, 0, 1))
pt <- 2*(1 - pt(t, df = (length(x) - 1)))
ggplot(data_plot, aes(x = x)) +
stat_function(fun = dt, args = list((length(x) - 1))) +
geom_area(fill = "blue",
stat = "function", fun = dt,
args = list((length(x) - 1)),
xlim = c(t, 5),
alpha = 0.5) +
geom_area(fill = "blue",
stat = "function", fun = dt,
args = list((length(x) - 1)),
xlim = c(-5, -t),
alpha = 0.5) +
labs(y = "Density", x = "T") +
theme_bw() +
theme(legend.position = "None") +
geom_vline(xintercept =
c(qt(0.025, df = (length(x) - 1)),
qt(0.975, df = (length(x) - 1))))
set.seed(1234)
x <- c(rgamma(100, 5, 1), rgamma(100, 10, 1))
sex <- c(rep("F", 100), rep("M", 100))
dp <- data.frame(x, sex)
ggplot(dp, aes(x = x, y = stat(density), fill = sex)) + geom_histogram() +
facet_grid(sex ~ ., labeller = label_both) +
labs(x = "Troponin", y = "Density") +
theme_bw() + theme(text = element_text(size = 14), legend.position = "none")
set.seed(1234)
x <- c(rgamma(100, 5, 1), rweibull(100, 0.5, 0.8))
sex <- c(rep("F", 100), rep("M", 100))
dp <- data.frame(x, sex)
ggplot(dp, aes(x = x, y = stat(density), fill = sex)) + geom_histogram() +
facet_grid(sex ~ ., labeller = label_both) +
labs(x = "Troponin", y = "Density") +
theme_bw() + theme(text = element_text(size = 14), legend.position = "none")
n <- 100000
sd <- 0.5
set.seed(1234)
x <- rnorm(n, mean=3, sd = sd)
y <- rnorm(n, mean=3, sd = sd)
aux01 <- data.frame(group = factor(rep(c("A", "B"), each=n)),
biomarker = c(x, y),
PI = paste("P(B > A) = ", round(1 - pnorm(0, 0, sd = sd), 2)))
x <- rnorm(n, mean=3, sd = sd)
y <- rnorm(n, mean=3.5, sd = sd)
aux02 <- data.frame(group = factor(rep(c("A", "B"), each=n)),
biomarker = c(x, y),
PI = paste("P(B > A) = ", round(1 - pnorm(0, 0.5, sd = sd), 2)))
x <- rnorm(n, mean=3, sd = sd)
y <- rnorm(n, mean=4, sd = sd)
aux03 <- data.frame(group = factor(rep(c("A", "B"), each=n)),
biomarker = c(x, y),
PI = paste("P(B > A) = ", round(1 - pnorm(0, 1, sd = sd), 2)))
x <- rnorm(n, mean=3, sd = sd)
y <- rnorm(n, mean=2.5, sd = sd)
aux04 <- data.frame(group = factor(rep(c("A", "B"), each=n)),
biomarker = c(x, y),
PI = paste("P(B > A) = ", round(1 - pnorm(0, -0.5, sd = sd), 2)))
x <- rnorm(n, mean=3, sd = sd)
y <- rnorm(n, mean=2, sd = sd)
aux05 <- data.frame(group = factor(rep(c("A", "B"), each=n)),
biomarker = c(x, y),
PI = paste("P(B > A) = ", round(1 - pnorm(0, -1, sd = sd), 2)))
dp <- bind_rows(aux01, aux02, aux03, aux04, aux05)
ggplot(dp, aes(x = biomarker, color = group)) +
geom_density() +
theme_bw() +
facet_grid(PI ~ ., switch = "y") +
labs(y = "Density", x = "Biomarker", color = "Group") +
theme(legend.position = "right", strip.text.y = element_text(angle = 180)) +
scale_x_continuous(limits = c(0, 6))