Skip to content

Commit a6114b6

Browse files
committed
Code to play round with bootstrap and other CI-building methods for the exponential
1 parent 87bd205 commit a6114b6

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

code/bootstrap_exponencial.r

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
is_in <- function(x, l, u){
2+
below <- x >= l
3+
above <- x <= u
4+
result <- as.logical(below * above)
5+
return(result)
6+
}
7+
8+
gera_dados <- function(n, theta){
9+
X <- rexp(n = n, rate = theta)
10+
return(X)
11+
}
12+
13+
computa_emv <- function(x){
14+
theta.chapeu <- 1/mean(x)
15+
return(theta.chapeu)
16+
}
17+
18+
intervalos_emv <- function(x, alpha = 0.95){
19+
n <- length(x)
20+
S <- sum(x)
21+
theta.chapeu <- computa_emv(x)
22+
##
23+
ZchiL <- qchisq(p = (1-alpha)/2, df = 2*n)
24+
ZchiU <- qchisq(p = (1+alpha)/2, df = 2*n)
25+
##
26+
Znorm <- qnorm(p = (1+alpha)/2)
27+
D <- Znorm*sqrt(theta.chapeu^2/n)
28+
##
29+
resultado <- tibble::tibble(
30+
point = c(theta.chapeu, theta.chapeu),
31+
lwr = c(ZchiL/(2*S), theta.chapeu-D),
32+
upr = c(ZchiU/(2*S), theta.chapeu+D),
33+
method = c("exact", "asymptotic")
34+
)
35+
return(resultado)
36+
}
37+
38+
NP_boot <- function(x, B, alpha = 0.95){
39+
n <- length(x)
40+
resample <- matrix(NA, nrow = B, ncol = n)
41+
for(i in 1:B){
42+
resample[i, ] <- x[sample(seq_along(x), n, replace = TRUE)]
43+
}
44+
thetas.chapeus <- apply(resample, 1, computa_emv)
45+
46+
out <- list(
47+
lwr = quantile(thetas.chapeus, probs = (1-alpha)/2),
48+
mean = mean(thetas.chapeus),
49+
upr = quantile(thetas.chapeus, probs = (1+alpha)/2)
50+
)
51+
return(out)
52+
}
53+
54+
P_boot <- function(x, B, alpha = 0.95){
55+
n <- length(x)
56+
theta_star <- computa_emv(x)
57+
resample <- matrix(NA, nrow = B, ncol = n)
58+
for(i in 1:B){
59+
resample[i, ] <- rexp(n = n, rate = theta_star)
60+
}
61+
thetas.chapeus <- apply(resample, 1, computa_emv)
62+
63+
out <- list(
64+
lwr = quantile(thetas.chapeus, probs = (1-alpha)/2),
65+
mean = mean(thetas.chapeus),
66+
upr = quantile(thetas.chapeus, probs = (1+alpha)/2)
67+
)
68+
return(out)
69+
}
70+
71+
intervalos_bootstrap <- function(x, B, alpha = 0.95){
72+
73+
NP.res <- NP_boot(x = x, B = B, alpha = alpha)
74+
P.res <- P_boot(x = x, B = B, alpha = alpha)
75+
76+
resultado <- tibble::tibble(
77+
point = c(NP.res$mean, P.res$mean),
78+
lwr = c(NP.res$lwr, P.res$lwr),
79+
upr = c(NP.res$upr, P.res$upr),
80+
method = c("non_parametric", "parametric")
81+
)
82+
return(resultado)
83+
}
84+
85+
gera_e_estima <- function(n, theta, B, alpha = 0.95){
86+
dados <- gera_dados(n = n, theta = theta)
87+
est1 <- intervalos_emv(dados, alpha = alpha)
88+
est2 <- intervalos_bootstrap(x = dados, B = B, alpha = alpha)
89+
return(rbind(est1, est2))
90+
}
91+
92+
#############
93+
M <- 5E2 ## repetições
94+
Nboot <- 1000 ## bootstrap reps
95+
theta.vdd <- 2
96+
Nsample <- 30
97+
98+
results <- do.call(rbind,
99+
lapply(1:M, function(i){
100+
raw <- gera_e_estima(
101+
n = Nsample,
102+
theta = theta.vdd,
103+
B = Nboot
104+
)
105+
raw$replicate <- i
106+
return(raw)
107+
}))
108+
109+
results$covers <- is_in(x = theta.vdd,
110+
l = results$lwr,
111+
u = results$upr)
112+
results$width <- results$upr - results$lwr
113+
114+
aggregate((point-theta.vdd)~method, mean,
115+
data = results)
116+
aggregate(point~method, var, data = results)
117+
aggregate(covers~method, mean, data = results)
118+

0 commit comments

Comments
 (0)