-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBeta_cod
143 lines (96 loc) · 4.26 KB
/
Beta_cod
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
## Enter Data ##
x = # scan("C:/.txt")
## A first estimation by the Method of Moments (MM) ##
mu <- mean(x)
var <- var(x)
s1 <- ((1 - mu) / var - 1 / mu) * mu ^ 2
s2 <- s1 * (1 / mu - 1)
## Maximum Likelihood Estimation (MLE) ##
log.vero <- function(theta) - sum(log(dbeta(x, theta[1], theta[2])))
Lmax.1 <- nlm(log.vero, c(s1, s2)) # Moment estimates as initial values for the iterative scheme
Lmax.1
## Histogram with Beta curve (optional) ##
hist(x, breaks=nclass.Sturges(x), prob=TRUE,
xlab="Data", ylim=c(0, 9),
main="Histogram with Beta curve")
# In shape1, shape2 below, enter the ML estimators given in Lmax.1
curve(dbeta(x, shape1, shape2, ncp = 0), col = "red", lty = 2, add = TRUE)
## New Beta test ##
## Parameters of the Beta distribution to contrast, H_0: Data follows a Beta(a,b)
a <- # Enter a theoretical value or the MM or MLE estimator
b <- # Enter a theoretical value or the MM or MLE estimator
## Function to calculate Tn according to Eq.4 in Ebner & Liebenberg (2021)
calculate_Tn <- function(x, a, b) {
n <- length(x)
# First term
sum1 <- 0
for (j in 1:n) {
for (k in 1:n) {
sum1 <- sum1 + ((a + b)^2 * x[j] * x[k] - a * (a + b) * (x[j] + x[k]) + a^2) * min(x[j], x[k])
}
}
term1 <- sum1 / n
cat("Term 1:", term1, "\n")
# Second term
B1 <- beta(a + 1, b + 1) / beta(a, b)
sum2 <- 0
for (j in 1:n) {
sum2 <- sum2 + ((a + b) * x[j] - a) * pbeta(x[j], a + 1, b + 1)
}
term2 <- 2 * B1 * sum2
cat("Term 2:", term2, "\n")
# Third term
B2 <- beta(2 * a + 1, 2 * b + 1) / beta(a, b)^2
term3 <- n * B2
cat("Term 3:", term3, "\n")
# Calculate Tn
Tn <- term1 - term2 + term3
return(Tn)
}
Tn_original <- calculate_Tn(x, a, b)
cat("Tn from the data:", Tn_original, "\n")
## Generate B bootstrap samples of length n from a Beta(a, b) distribution, and calculate Tn from each one
B <- 5000
Tn_bootstrap <- numeric(B)
n <- length(x)
for (i in 1:B) {
sample_beta <- rbeta(n, a, b)
Tn_bootstrap[i] <- calculate_Tn(sample_beta, a, b)
}
## Obtain 1-alpha quantiles from the bootstrap Tn's, with alpha = 0.01, 0.05, 0.1
alpha_levels <- c(0.01, 0.05, 0.1)
quantiles_1_alpha <- sapply(alpha_levels, function(alpha) quantile(Tn_bootstrap, 1 - alpha))
cat("1-alpha quantiles from the distribution of Tn under the null hypothesis:\n")
print(quantiles_1_alpha)
cat("Tn from the data:", Tn_original, "\n") # Hypothesis of goodness-of-fit is rejected at the level alpha if the observed Tn from the data is greater than the corresponding 1-alpha quantile under the null hypothesis
## Generate graphical hypothesis testing
library(ggplot2)
df <- data.frame(Tn_bootstrap = Tn_bootstrap)
dens <- density(Tn_bootstrap)
y_max <- max(dens$y)
ggplot(df, aes(x = Tn_bootstrap)) +
geom_density(fill = "blue", alpha = 0.3) +
geom_vline(aes(xintercept = Tn_original), color = "red", linetype = "dashed", size = 1) +
geom_vline(aes(xintercept = quantiles_1_alpha[1]), color = "green", linetype = "dotted", size = 1) +
geom_vline(aes(xintercept = quantiles_1_alpha[2]), color = "purple", linetype = "dotted", size = 1) +
geom_vline(aes(xintercept = quantiles_1_alpha[3]), color = "orange", linetype = "dotted", size = 1) +
labs(x = "Tn", y = "Probability density") +
# xlim(0, 0.3) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5, size = 14, face = "bold"),
axis.title = element_text(size = 12),
axis.text = element_text(size = 10)
) +
annotate("text", x = Tn_original, y = y_max / 2,
label = paste("Tn obs =", round(Tn_original, 5)),
vjust = -1, hjust = -0.2, color = "red", size = 3.5, angle = 90) +
annotate("text", x = quantiles_1_alpha[1], y = y_max / 2,
label = paste("quantile 99% =", round(quantiles_1_alpha[1], 5)),
vjust = -1, hjust = -0.2, color = "green", size = 3.5, angle = 90) +
annotate("text", x = quantiles_1_alpha[2], y = y_max / 2,
label = paste("quantile 95% =", round(quantiles_1_alpha[2], 5)),
vjust = -1, hjust = -0.2, color = "purple", size = 3.5, angle = 90) +
annotate("text", x = quantiles_1_alpha[3], y = y_max / 2,
label = paste("quantile 90% =", round(quantiles_1_alpha[3], 5)),
vjust = -1, hjust = -0.2, color = "orange", size = 3.5, angle = 90)