library(ggplot2)
alpha <- 0.05 # a significance level
n <- 10 # degree of freedom
accept <- function(x){
y <- dt(x, n)
y[x < qt(alpha/2, n) | x > qt(1 - alpha/2, n)] <- NA
return(y)
}
reject <- function(x){
y <- dt(x, n)
y[x > qt(alpha/2, n) & x < qt(1 - alpha/2, n)] <- NA
return(y)
}
N <- 1e4 # a large number
s <- 5 # fontsize
p <-
ggplot(data.frame(x=c(-3, +3)), aes(x=x)) +
stat_function(
fun=accept, geom="area",
fill="#0072B2", n=N) +
stat_function(
fun=reject, geom="area",
fill="#D55E00", n=N) +
annotate(
"text", x=0, y=0.2,
parse=T, label="1-alpha",
size=s, colour="#EEEEEE") +
annotate(
"text", x=-2.5, y=0.2,
parse=T, label="alpha/2",
size=s, colour="#D55E00") +
annotate(
"text", x=+2.5, y=0.2,
parse=T, label="alpha/2",
size=s, colour="#D55E00") +
annotate(
"text", x=qt(alpha/2, n), y=-0.02,
parse=T, label="-t[n-1](alpha/2)",
size=s, colour="#000000") +
annotate(
"text", x=0, y=-0.02,
label="0",
size=s, colour="#000000") +
annotate(
"text", x=qt(1 - alpha/2, n), y=-0.02,
parse=T, label="+t[n-1](alpha/2)",
size=s, colour="#000000") +
theme(
axis.text.y=element_blank(),
axis.text.x=element_blank(),
axis.ticks=element_blank()) +
labs(x="", y="")
svg(filename="confidence-interval.svg", width=16/2, height=9/2)
plot(p)
dev.off()