-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlotDiagram.R
114 lines (79 loc) · 3.37 KB
/
PlotDiagram.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
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
# Plot
# Hypothesis Testing
# Binomial Distribution
# P-value
# obsv is the observed x; expx is the expected x
HTPlotBPV <- function(expx, size, prob, obsx, pvalue) {
x = seq(0, size)
hx = dbinom(x, size, prob)
df <- data.frame(stolpec1 = hx)
df.bar <- barplot(df$stolpec1)
## Create a vector of colors selected based on whether x is <obsx or >obsx
## (FALSE + 1 -> 1 -> "gray"; TRUE + 1 -> 2 -> "blue")
if(obsx > expx){
cols = c("gray", "blue")[(x > obsx) + 1]
}
else{
cols = c("gray", "blue")[(x < obsx) + 1]
}
barplot(df$stolpec1, col=cols, xlim=c(0, size), ylim=c(-max(hx)/4, max(hx)), names.arg=x, main="Binomial Distribution")
points(df.bar[obsx+1], 0, pch=4, col="blue", cex=3, lwd=2.5)
text(df.bar[obsx+1], -max(hx)/6, sprintf("Observed %d", obsx), col="blue", cex=1.)
points(df.bar[expx+1], 0, pch=17, col="darkgreen", cex=3, lwd=2.5)
text(df.bar[expx+1],-max(hx)/8, sprintf("Expected %d", expx), col="darkgreen", cex=1.)
}
# Critical Region
HTPlotBCR <- function(expx, size, prob, c, whatTail) {
x = seq(0, size)
hx = dbinom(x, size, prob)
df <- data.frame(stolpec1 = hx)
df.bar <- barplot(df$stolpec1)
barplot(df$stolpec1, xlim=c(0, size), ylim=c(-max(hx)/4, max(hx)), names.arg=x, main="Binomial Distribution")
points(df.bar[expx+1], 0, pch=17, col="darkgreen", cex=3, lwd=2.5)
text(df.bar[expx+1],-max(hx)/8, sprintf("Expected %d", expx), col="darkgreen", cex=1.)
if(whatTail == -1){
points(df.bar[c+1], 0, pch=4, col="blue", cex=3, lwd=2.5)
text(df.bar[c+1], -max(hx)/10, sprintf("c = %d", c), col="blue", cex=1.)
}
if(whatTail == 0){
points(df.bar[c[1]+1], 0, pch=4, col="blue", cex=3, lwd=2.5)
text(df.bar[c[1]+1], -max(hx)/10, sprintf("c1 = %d", c[1]), col="blue", cex=1.)
points(df.bar[c[2]+1], 0, pch=4, col="blue", cex=3, lwd=2.5)
text(df.bar[c[2]+1], -max(hx)/10, sprintf("c2 = %d", c[2]), col="blue", cex=1.)
}
if(whatTail == 1){
points(df.bar[c+1], 0, pch=4, col="blue", cex=3, lwd=2.5)
text(df.bar[c+1], -max(hx)/10, sprintf("c = %d", c), col="blue", cex=1.)
}
}
# Interval Estimating
# Chi-square Destribution
# Used by Testing For Homogeneity, Testing for Independence, GoodnessOfFit
# P-value
ChiSquPlotPV <- function(n, Q, df){ # n: total sample
x = rchisq(n, df)
hx = dchisq(x, df)
h <- hist(x, plot=FALSE)
cuts <- cut(h$breaks, c(-Inf, Q, Inf))
cols = c("gray", "blue")[cuts]
# force to plot in density (probability)
# h$counts=h$counts/sum(h$counts) # <= bad method
h$counts=h$density
plot(h, col=cols, main=paste("Histogram of Q and chi-square with degree of freedom of ",df), ylab="Density")
# useless
# hist(x, col=c("gray", "blue")[cut(h$breaks, c(-Inf, Q, Inf))], prob=TRUE, main=paste("Histogram of Q and chi-square with degree of freedom of ",df))
curve(dchisq(x, df) , col='green', add=TRUE)
points(Q, 0, pch=4, col="darkgreen", cex=3, lwd=2.5)
text(Q, max(h$density)/10, sprintf("Q = %f", Q), col="darkgreen", cex=1.)
}
# Critical Region
ChiSquPlotCR <- function(n, Q, df, c){
x = rchisq(n, df)
hx = dchisq(x, df)
h <- hist(x, prob=TRUE, main=paste("Histogram of Q and chi-square with degree of freedom of ",df))
curve(dchisq(x, df) , col='green', add=TRUE)
points(c, 0, pch=4, col="red", cex=3, lwd=2.5)
text(c, max(h$density)/8, sprintf("c = %f", c), col="red", cex=1.)
points(Q, 0, pch=4, col="blue", cex=3, lwd=2.5)
text(Q, max(h$density)/10, sprintf("Q = %f", Q), col="blue", cex=1.)
}