Introduction
See the Handbook for information on this topic.
Confidence limits for measurement variables
Methods are described in the “How to calculate confidence limits” section below.
Confidence limits for nominal variables
Examples are given in the “How to calculate confidence limits” section below.
Statistical testing with confidence intervals
Similar statistics
Examples
See the Handbook for information on these topics.
How to calculate confidence limits
The confidence limits about the mean—calculated using the t-value discussed in the Handbook—can be determined with variety of functions. One is t.test in the native stats package. Another is the CI function in the Rmisc package, which also has the function summarySE that presents the mean, standard deviation, standard error, and confidence interval for data designated as groups.
The bootstrap method noted in the Handbook can be achieved with the boot and boot.ci functions in the boot package.
Confidence intervals for mean with t.test, Rmisc, and DescTools
###
--------------------------------------------------------------
### Confidence interval for measurement data, blacknose fish , p. 120
### --------------------------------------------------------------
Input =("
Stream Fish
Mill_Creek_1 76
Mill_Creek_2 102
North_Branch_Rock_Creek_1 12
North_Branch_Rock_Creek_2 39
Rock_Creek_1 55
Rock_Creek_2 93
Rock_Creek_3 98
Rock_Creek_4 53
Turkey_Branch 102
")
Data = read.table(textConnection(Input),header=TRUE)
### Use t.test to produce confidence
interval
t.test(Data$ Fish,
conf.level=0.95) # Confidence
interval of the mean
95 percent confidence interval:
45.33665 94.66335
### Use CI in Rmisc
package to produce confidence interval
library(Rmisc)
CI(Data$ Fish,
ci=0.95) # Confidence
interval of the mean
upper mean lower
94.66335 70.00000 45.33665
### Use MeanCI in DescTools
package to produce confidence interval
library(DescTools)
MeanCI(Data$ Fish,
conf.level=0.95) # Confidence
interval of the mean
mean lwr.ci upr.ci
70.00000 45.33665 94.66335
# # #
Confidence intervals for means for grouped data
### --------------------------------------------------------------
### Confidence interval for grouped data, hypothetical data
### --------------------------------------------------------------
Input =("
Stream Animal Count
Mill_Creek_1 Fish 76
Mill_Creek_2 Fish 102
North_Branch_Rock_Creek_1 Fish 12
North_Branch_Rock_Creek_2 Fish 39
Rock_Creek_1 Fish 55
Rock_Creek_2 Fish 93
Rock_Creek_3 Fish 98
Rock_Creek_4 Fish 53
Turkey_Branch Fish 102
Mill_Creek_1 Insect 76
Mill_Creek_2 Insect 102
North_Branch_Rock_Creek_1 Insect 12
North_Branch_Rock_Creek_2 Insect 39
")
D2 = read.table(textConnection(Input),header=TRUE)
library(Rmisc)
summarySE(data=D2, # Will produce
confidence intervals
measurevar="Count", # for
groups defined by a variable
groupvars="Animal",
conf.interval = 0.95)
Animal N Count sd se ci
1 Fish 9 70.00 32.08582 10.69527 24.66335
2 Insect 4 57.25 39.72719 19.86360 63.21483
# # #
Confidence intervals for mean by bootstrap
###
--------------------------------------------------------------
### Confidence interval for measurement data, blacknose fish , p. 120
### --------------------------------------------------------------
Input =("
Stream Fish
Mill_Creek_1 76
Mill_Creek_2 102
North_Branch_Rock_Creek_1 12
North_Branch_Rock_Creek_2 39
Rock_Creek_1 55
Rock_Creek_2 93
Rock_Creek_3 98
Rock_Creek_4 53
Turkey_Branch 102
")
Data = read.table(textConnection(Input),header=TRUE)
Confidence intervals for mean by bootstrap with DescTools
MeanCI(Data$Fish, method="boot", type="norm", R=10000)
mean lwr.ci upr.ci
70.00000 50.17986 89.84836
# May be different for different iterations
MeanCI(Data$Fish, method="boot", type="basic", R=10000)
mean lwr.ci upr.ci
70.00000 51.44444 90.66667
# May be different for different iterations
Confidence intervals for mean by bootstrap with boot package
library(boot)
Fun = function(x, index) {
return(c(mean(x[index]),
var(x[index]) / length(index)))
}
Boot = boot(data=Data$Fish,
statistic=Fun,
R=10000)
mean(Boot$t[,1])
[1] 70.01229 # Mean by bootstrap
# May be different for different iterations
boot.ci(Boot,
conf=0.95)
Intervals :
Level Normal Basic Studentized
95% (50.22, 89.76 ) (51.11, 90.44 ) (38.85, 91.72 )
Level Percentile BCa
95% (49.56, 88.89 ) (47.44, 87.22 )
Calculations and Intervals on Original Scale
# Note that the bootstrapped confidence limits vary from
# the calculated ones above because the original data set has
# few values and is not necessarily normally distributed.
# # #
Confidence interval for proportions
The confidence interval for a proportion can be determined with the binom.test function, and more options are available in the BinomCI function and MultinomCI function in the DescTools package. More advanced techniques for confidence intervals on proportions and differences in proportions can be found in the PropCIs package.
###
--------------------------------------------------------------
### Confidence interval for nominal data, colorblind example, p. 118
### --------------------------------------------------------------
binom.test(2, 20, 0.5,
alternative="two.sided",
conf.level=0.95)
95 percent confidence interval:
0.01234853 0.31698271
# # #
###
--------------------------------------------------------------
### Confidence interval for nominal data, Gus data, p. 121
### --------------------------------------------------------------
Input =("
Paw
right
left
right
right
right
right
left
right
right
right
")
Gus = read.table(textConnection(Input),header=TRUE)
Successes = sum(Gus$ Paw == "left") #
Note the == operator
Failures = sum(Gus$ Paw == "right")
Total = Successes + Failures
Expected = 0.5
binom.test(Successes, Total, Expected,
alternative="two.sided",
conf.level=0.95)
95 percent confidence interval:
0.02521073 0.55609546
### Agrees with exact confidence interval from SAS
# # #
Confidence interval for proportions using DescTools
Confidence interval for single proportion
###
--------------------------------------------------------------
### Confidence intervals for nominal data, colorblind example, p. 118
### --------------------------------------------------------------
library(DescTools)
BinomCI(2, 20,
conf.level = 0.95,
method = "modified wilson")
### Other methods: "wilson",
"wald", "agresti-coull", "jeffreys",
### "modified wilson", "modified jeffreys",
### "clopper-pearson", "arcsine",
"logit", "witting"
est lwr.ci upr.ci
[1,] 0.1 0.01776808 0.3010336
# # #
Confidence interval for multinomial proportion
###
--------------------------------------------------------------
### Confidence intervals for multinomial proportions, p. 33
### --------------------------------------------------------------
observed = c(35,74,22,69)
library(DescTools)
MultinomCI(observed, conf.level=0.95, method="goodman")
### Other methods: "sisonglaz", "cplus1"
est lwr.ci upr.ci
[1,] 0.175 0.11253215 0.2619106
[2,] 0.370 0.28113643 0.4686407
[3,] 0.110 0.06224338 0.1870880
[4,] 0.345 0.25846198 0.4431954
# # #