-
Notifications
You must be signed in to change notification settings - Fork 12
/
ss.ci.R
executable file
·209 lines (195 loc) · 8.32 KB
/
ss.ci.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#' Confidence Interval for the mean
#'
#' Computes a confidence interval for the mean of the variable (parameter
#' or feature of the process), and prints the data, a histogram with a density line,
#' the result of the Shapiro-Wilks normality test and a quantile-quantile plot.
#'
#' When the population variance is known, or the size is greater than 30,
#' it uses z statistic. Otherwise, it is uses t statistic.\cr
#' If the sample size is lower than 30, a warning is displayed so as to
#' verify normality.
#'
#' @param x A numeric vector with the variable data
#' @param sigma2 The population variance, if known
#' @param alpha The eqn{\\alpha} error used to compute the \eqn{100*(1-\\alpha)\%} confidence interval
#' @param data The data frame containing the vector
#' @param xname The name of the variable to be shown in the graph
#' @param approx.z If TRUE it uses z statistic instead of t when sigma is unknown and sample size
#' is greater than 30. The default is FALSE, change only if you want to compare with
#' results obtained with the old-fashioned method mentioned in some books.
#' @param main The main title for the graph
#' @param digits Significant digits for output
#' @param sub The subtitle for the graph (recommended: six sigma project name)
#' @param ss.col A vector with colors
#' @return
#' The confidence Interval.\cr
#' A graph with the figures, the Shapiro-Wilks test, and a histogram.
#'
#' @references
#' Cano, Emilio L., Moguerza, Javier M. and Redchuk, Andres. 2012.
#' \emph{Six Sigma with {R}. Statistical Engineering for Process
#' Improvement}, Use R!, vol. 36. Springer, New York.
#' \url{https://link.springer.com/book/10.1007/978-1-4614-3652-2/}.
#'
#' @author EL Cano
#'
#' @note
#' Thanks to the kind comments and suggestions from the anonymous reviewer
#' of a tentative article.
#'
#' @seealso
#' \code{\link{ss.data.rr}}
#' @examples
#' ss.ci(len, data=ss.data.strings, alpha = 0.05,
#' sub = "Guitar Strings Test | String Length",
#' xname = "Length")
#' @keywords confidence interval normality test mean
#' @export
ss.ci<-function(x, sigma2 = NA, alpha = 0.05, data = NA,
xname = "x", approx.z = FALSE, main = "Confidence Interval for the Mean",
digits = 3,
sub = "", ss.col = c("#666666", "#BBBBBB", "#CCCCCC", "#DDDDDD", "#EEEEEE",
"#FFFFFF", "#000000", "#000000")){
if (is.data.frame(data)){
x <- data[[deparse(substitute(x))]]
}
na <- length(which(is.na(x)))
if (na > 0) { cat(na, " missing values were ommitted\n")}
m <- mean(x, na.rm = TRUE)
n <- length(x) - na
s <- ifelse(is.numeric(sigma2),
sqrt(sigma2),
sd(x, na.rm = TRUE))
if (is.numeric(sigma2) | approx.z == TRUE){
st <- qnorm(1 - (alpha/2))
st.dist <- c("z")
}
else{
if (n < 30) {
warning("\nThe sample size is lower than 30. Check Normality\n\n")
}
st <- qt(1-(alpha/2), n-1)
st.dist <- c("t")
}
dist <- st * (s/sqrt(n))
cat("\tMean = ", round(m, digits), "; sd = ", round(s, digits), "\n", sep = "")
cat("\t", (1-alpha)*100, "% Confidence Interval= ",
round(m-dist, digits), " to ", round(m+dist, digits),"\n\n", sep = "")
ci <- c(m-dist, m+dist)
names(ci) <- c("LL", "UL")
##Canvas-container
.ss.prepCanvas(main,sub, ss.col)
##figures
vp.figures <- grid::viewport(name = "figures",
x = 0,
width = 1,
height = unit(8, "lines"),
y = 1,
just = c("left", "top"),
layout = grid::grid.layout(1, 2, widths = c(0.4, 0.6)) )
grid::pushViewport(vp.figures)
vp.figures1 <- grid::viewport(name = "figures1",
layout.pos.col = 1,
layout.pos.row = 1)
grid::pushViewport(vp.figures1)
grid:: grid.roundrect(height = unit(7, "lines"),
width = 0.95,
gp = grid::gpar(fill = ss.col[5], col = ss.col[2], lwd=2))
grid:: grid.text("Mean:\nStdDev:\nn:\nMissing:", just = "left",
x = unit(1, "npc") - unit(5.5, "cm"),
gp = grid::gpar(fontface = c("bold"), col = ss.col[7]))
grid:: grid.text(paste(round(m, digits), "\n", round(s, digits), "\n", n,
"\n", na, sep = ""), just = "right",
gp = grid::gpar(col = ss.col[8]),
x = unit(1, "npc") - unit(1, "cm"))
grid::popViewport()
vp.figures2 <- grid::viewport(name = "figures2", layout.pos.col = 2,
layout.pos.row = 1)
grid::pushViewport(vp.figures2)
grid:: grid.roundrect(height = unit(7, "lines"),
width = 0.95,
gp = grid::gpar(fill = ss.col[5], col = ss.col[2], lwd = 2))
grid:: grid.text(paste((1-alpha)*100, "% CI:\nP-Var:\n",
st.dist, ":", sep = ""),
just = "left",
x = unit(0,"npc") + unit(1,"cm"),
gp = grid::gpar(fontface=c("bold"), col = ss.col[7]))
grid:: grid.text(paste("[", round(ci[1], digits), ", ", round(ci[2], digits),
"]\n", ifelse(is.numeric(sigma2), sigma2, "unknown"),
"\n", round(st, digits),
sep = ""), just = "right",
gp = grid::gpar(col = ss.col[8]),
x = unit(0,"npc") + unit(7.5,"cm"))
grid::popViewport()
grid::popViewport()
#graph
vp.graph <- grid::viewport(name = "graph",
y = 0,
width = 0.95,
height = unit(1, "npc") - unit(8, "lines"),
just = c("center", "bottom"),
layout = grid::grid.layout(1, 2,
widths = unit(c(1, 6), c("null", "cm"))))
grid::pushViewport(vp.graph)
vp.test <- grid::viewport(name = "test", layout.pos.row = 1, layout.pos.col = 2)
grid::pushViewport(vp.test)
grid::grid.rect()
grid:: grid.roundrect(height = unit(6, "lines"),
width = 0.9,
y = unit(1, "npc") + unit(-1, "lines"),
just = "top",
gp = grid::gpar(fill = ss.col[5], col = ss.col[2], lwd = 2))
grid:: grid.text("Shapiro-Wilks\nNormality Test\n",
y = unit(1, "npc") - unit(3, "lines"),
gp = grid::gpar(fontface = c("bold"), col = ss.col[7]))
pval <- shapiro.test(x)[2]$p.value
if (pval < 0.05){
warning("Sample data is non-normal.")
}
grid:: grid.text(paste(round(shapiro.test(x)[1]$statistic, digits), "\n"),
gp = grid::gpar(col = ss.col[8]),
y = unit(1, "npc") - unit(5, "lines"))
grid:: grid.text(paste("p-value:", round(pval, digits), "\n"),
gp = grid::gpar(col = ss.col[8]),
y = unit(1, "npc") - unit(6, "lines"))
vp.qq <- grid::viewport(name="qqp",
x = 0.5, y=0.25,
height = unit(0.6,"npc"))
grid::pushViewport(vp.qq)
qqp <- qplot(sample = x
# , stat="qq"
) +
xlab(NULL) + ylab(NULL) +
theme(axis.text.x = element_blank(),
axis.text.y = element_blank()) +
ggtitle("Normal q-q Plot")
print(qqp,newpage=FALSE)
grid::popViewport()
grid::popViewport()
vp.hist <- grid::viewport(name = "hist",
layout.pos.row = 1,
layout.pos.col = 1)
grid::pushViewport(vp.hist)
grid::grid.rect()
ggdata <- reshape2::melt(x)
nbins <- nclass.Sturges(x)
qqp <- ggplot(ggdata, aes(x = .data$value))
myhist <- qqp +
geom_histogram(aes(y = after_stat(density)),
bins = nbins,
# binwidth = binw,
fill = "white",
col = "gray"
# , stat = "bin"
) +
xlab(paste("Value of", xname)) +
ggtitle("Histogram & Density Plot") +
stat_density(geom = "path",
position = "identity",
# binwidth = binw,
size = 1)
suppressWarnings(
print(myhist, newpage=FALSE)
)
return (ci)
}