-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfidenceIntervalMacros.R
149 lines (123 loc) · 5.07 KB
/
confidenceIntervalMacros.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
################################################################
# Helpers for confidence intervals, mostly for bootstrapping.
# Computes all confidence intervals with the sensible defaults.
# 2014-2015 Pierre Dragicevic
#
# WARNING - This is an alpha version, meaning it's unfinished
#
################################################################
# For more details about bootstrapping.
# See http://www.mayin.org/ajayshah/KB/R/documents/boot.html for boot
# and (Kirby and Gerlanc, 2013) http://web.williams.edu/Psychology/Faculty/Kirby/bootes-kirby-gerlanc-in-press.pdf for bootES and for referencing the bootstrap method in your paper
library(boot)
library(PropCIs)
# If set to TRUE, bootstrap intervals remain the same across executions.
deterministic <- TRUE
# The number of bootstrap resamples. Typically recommended is >= 1,000.
# The higher the number, the more precise the interval but also the slower
# the computation.
replicates <- 10000
# The method for computing intervals. The adjusted bootstrap
# percentile (BCa) method is recommended by (Kirby and Gerlanc, 2013)
# and should work in most cases. For other methods type help(boot.ci).
# FIXME: changing this does not work. You have to modify some of the code
# below.
intervalMethod <- "bca"
# Number of significant digits used for text output. Using many digits
# is not recommended as it gives a misleading impression of precision.
significantDigits <- 2
#####################################################################
# Statistics
samplemedian <- function(x, d) {
return(median(x[d]))
}
samplemean <- function(x, d) {
return(mean(x[d]))
}
# Data transformations
# No transformation, yields arithmetic means.
identityTransform <- c(
function(x) {return (x)}, # the transformation
function(x) {return (x)}, # the inverse transformation
TRUE # TRUE if increasing, FALSE otherwise
)
# Log transformation, yields geometric means.
logTransform <- c(
function(x) {return (log(x))}, # the transformation
function(x) {return (exp(x))}, # the inverse transformation
TRUE # TRUE if increasing, FALSE otherwise
)
# Inverse transformation, yields harmonic means.
inverseTransform <- c(
function(x) {return (1/x)}, # the transformation
function(x) {return (1/x)}, # the inverse transformation
FALSE # TRUE if increasing, FALSE otherwise
)
# Returns the point estimate and confidence interval in an array of length 3
bootstrapCI <- function(statistic, datapoints) {
# Compute the point estimate
pointEstimate <- statistic(datapoints)
# Make the rest of the code deterministic
if (deterministic) set.seed(0)
# Generate bootstrap replicates
b <- boot(datapoints, statistic, R = replicates, parallel="multicore")
# Compute interval
ci <- boot.ci(b, type = intervalMethod)
# Return the point estimate and CI bounds
# You can print the ci object for more info and debug
lowerBound <- ci$bca[4]
upperBound <- ci$bca[5]
return(c(pointEstimate, lowerBound, upperBound))
}
# Returns the mean and its confidence interval in an array of length 3
bootstrapMeanCI <- function(datapoints) {
return(bootstrapCI(samplemean, datapoints))
}
# Returns the median and its confidence interval in an array of length 3
bootstrapMedianCI <- function(datapoints) {
return(bootstrapCI(samplemedian, datapoints))
}
exactMeanCI <- function(datapoints, transformation = identityTransform) {
datapoints <- transformation[[1]](datapoints)
pointEstimate <- mean(datapoints)
ttest <- t.test(datapoints)
lowerBound <- ttest[4]$conf.int[1]
upperBound <- ttest[4]$conf.int[2]
if (transformation[[3]])
return(transformation[[2]](c(pointEstimate, lowerBound, upperBound)))
else
return(transformation[[2]](c(pointEstimate, upperBound, lowerBound)))
}
percentCI <- function(datapoints, value) {
ncorrect <- length(datapoints[datapoints == value])
total <- length(datapoints)
percent <- 100 * ncorrect / total
proportionCI <- midPci(x = ncorrect, n = total, conf.level = 0.95)
percentCIlow <- 100 * proportionCI$conf.int[1]
percentCIhigh <- 100 * proportionCI$conf.int[2]
return (c(percent, percentCIlow, percentCIhigh))
}
# Returns the point estimate and confidence interval in a human-legible text format
formatCI <- function(CI, unit) {
point <- CI[1]
interval <- c(CI[2], CI[3])
# Format results in human-legible format using APA style
text <- paste(
prettyNum(point, digits=significantDigits),
unit,
", 95% CI [",
prettyNum(interval[1], digits=significantDigits), # APA recommends not reporting the unit again
", ",
prettyNum(interval[2], digits=significantDigits), # APA recommends not reporting the unit again
"]",
sep = "")
return(text)
}
# Returns the mean and its confidence interval in a human-legible text format
bootstrapMeanCI.text <- function(datapoints, unit) {
return(bootstrapCI.text(samplemean, datapoints, unit))
}
# Returns the median and its confidence interval in a human-legible text format
bootstrapMedianCI.text <- function(datapoints, unit) {
return(bootstrapCI.text(samplemedian, datapoints, unit))
}