-
Notifications
You must be signed in to change notification settings - Fork 1
/
p_value_plot.R
72 lines (65 loc) · 3.02 KB
/
p_value_plot.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# file : p_value_plot.R
# title : p值法
# author : Ming-Chang Lee
# date : 2024.3.12
# YouTube : https://www.youtube.com/@alan9956
# RWEPA : http://rwepa.blogspot.tw/
# GitHub : https://github.com/rwepa
# Email : alan9956@gmail.com
library(ggplot2)
library(ggpubr)
mu = 0
std = 1
xmin <- -3
xmax <- 3
# 雙尾檢定
score = 1.96
two_tail <- ggplot(data.frame(x = c(xmin, xmax)), aes(x = x)) +
ggtitle("雙尾檢定") +
theme(plot.title = element_text(hjust = 0.5)) +
stat_function(fun = dnorm, args = list(mean = mu, sd = std)) +
stat_function(fun = dnorm, args = list(mean = mu, sd = std), xlim = c(score, xmax),
geom = "area", fill = "blue", alpha = .2) +
stat_function(fun = dnorm, args = list(mean = mu, sd = std), xlim = c(xmin, -score),
geom = "area", fill = "blue", alpha = .2) +
scale_x_continuous(name = "x", breaks = seq(xmin, xmax, std)) +
scale_y_continuous(name = "Probability density") +
geom_segment(aes(x = 2.5, y = 0.1, xend = 2.2, yend = 0.01),
arrow = arrow(length = unit(0.2, "cm")),
lwd = 0.8) +
annotate("text", x = 2.5, y = 0.12, label = "p/2值", size = 6) +
geom_segment(aes(x = -2.5, y = 0.1, xend = -2.2, yend = 0.01),
arrow = arrow(length = unit(0.2, "cm")),
lwd = 0.8) +
annotate("text", x = -2.5, y = 0.12, label = "p/2值", size = 6)
# 單尾檢定(左尾檢定)
score = -1.645
left_tail <- ggplot(data.frame(x = c(xmin, xmax)), aes(x = x)) +
ggtitle("單尾檢定(左尾檢定)") +
theme(plot.title = element_text(hjust = 0.5)) +
stat_function(fun = dnorm, args = list(mean = mu, sd = std)) +
stat_function(fun = dnorm, args = list(mean = mu, sd = std), xlim = c(xmin, score),
geom = "area", fill = "blue", alpha = .2) +
scale_x_continuous(name = "x", breaks = seq(xmin, xmax, std)) +
scale_y_continuous(name = "Probability density") +
geom_segment(aes(x = -2.5, y = 0.1, xend = -2.2, yend = 0.01),
arrow = arrow(length = unit(0.2, "cm")),
lwd = 0.8) +
annotate("text", x = -2.5, y = 0.12, label = "p值", size = 6)
# 單尾檢定(右尾檢定)
score = 1.645
right_tail <- ggplot(data.frame(x = c(xmin, xmax)), aes(x = x)) +
ggtitle("單尾檢定(右尾檢定)") +
theme(plot.title = element_text(hjust = 0.5)) +
stat_function(fun = dnorm, args = list(mean = mu, sd = std)) +
stat_function(fun = dnorm, args = list(mean = mu, sd = std), xlim = c(score, xmax),
geom = "area", fill = "blue", alpha = .2) +
scale_x_continuous(name = "x", breaks = seq(xmin, xmax, std)) +
scale_y_continuous(name = "Probability density") +
geom_segment(aes(x = 2.5, y = 0.1, xend = 2.2, yend = 0.01),
arrow = arrow(length = unit(0.2, "cm")),
lwd = 0.8) +
annotate("text", x = 2.5, y = 0.12, label = "p值", size = 6)
# 設定上下圖以1行顯示, 上圖為雙尾檢定, 下圖左側為左尾檢定, 下圖右側為右尾檢定
ggarrange(two_tail, ggarrange(left_tail, right_tail), ncol = 1)
# end