Skip to content

Instantly share code, notes, and snippets.

@PatrickRWright
Created July 8, 2020 12:13
Show Gist options
  • Save PatrickRWright/532a71bd533b34b4b7d4d81a5d712c8f to your computer and use it in GitHub Desktop.
Save PatrickRWright/532a71bd533b34b4b7d4d81a5d712c8f to your computer and use it in GitHub Desktop.
Created on Skills Network Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Calculate confidence intervals (CI) in R"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A step by step explanation of the process can be found [here](http://www.stat.ucla.edu/~rgould/110as02/bsci) (i.e. \"Setting II\"). \n",
"The `Rmisc` package provides a function `CI()` to perform the task easily."
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"library(\"Rmisc\") # if you use \"Rmisc\" in code with \"dplyr\" then load \"dplyr\" after \"Rmisc\""
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"x <- c(44617, 7066, 17594, 2726, 1178, 18898, 5033, 37151, 4514, 4000)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<style>\n",
".dl-inline {width: auto; margin:0; padding: 0}\n",
".dl-inline>dt, .dl-inline>dd {float: none; width: auto; display: inline-block}\n",
".dl-inline>dt::after {content: \":\\0020\"; padding-right: .5ex}\n",
".dl-inline>dt:not(:first-of-type) {padding-left: .5ex}\n",
"</style><dl class=dl-inline><dt>upper</dt><dd>23173.4595100454</dd><dt>mean</dt><dd>14277.7</dd><dt>lower</dt><dd>5381.94048995461</dd></dl>\n"
],
"text/latex": [
"\\begin{description*}\n",
"\\item[upper] 23173.4595100454\n",
"\\item[mean] 14277.7\n",
"\\item[lower] 5381.94048995461\n",
"\\end{description*}\n"
],
"text/markdown": [
"upper\n",
": 23173.4595100454mean\n",
": 14277.7lower\n",
": 5381.94048995461\n",
"\n"
],
"text/plain": [
" upper mean lower \n",
"23173.46 14277.70 5381.94 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# 90% confidence interval of values in x\n",
"CI(x, ci = 0.9)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<style>\n",
".dl-inline {width: auto; margin:0; padding: 0}\n",
".dl-inline>dt, .dl-inline>dd {float: none; width: auto; display: inline-block}\n",
".dl-inline>dt::after {content: \":\\0020\"; padding-right: .5ex}\n",
".dl-inline>dt:not(:first-of-type) {padding-left: .5ex}\n",
"</style><dl class=dl-inline><dt>upper</dt><dd>25255.5321540832</dd><dt>mean</dt><dd>14277.7</dd><dt>lower</dt><dd>3299.86784591678</dd></dl>\n"
],
"text/latex": [
"\\begin{description*}\n",
"\\item[upper] 25255.5321540832\n",
"\\item[mean] 14277.7\n",
"\\item[lower] 3299.86784591678\n",
"\\end{description*}\n"
],
"text/markdown": [
"upper\n",
": 25255.5321540832mean\n",
": 14277.7lower\n",
": 3299.86784591678\n",
"\n"
],
"text/plain": [
" upper mean lower \n",
"25255.532 14277.700 3299.868 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# 95% confidence interval of values in x\n",
"CI(x, ci = 0.95)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<style>\n",
".dl-inline {width: auto; margin:0; padding: 0}\n",
".dl-inline>dt, .dl-inline>dd {float: none; width: auto; display: inline-block}\n",
".dl-inline>dt::after {content: \":\\0020\"; padding-right: .5ex}\n",
".dl-inline>dt:not(:first-of-type) {padding-left: .5ex}\n",
"</style><dl class=dl-inline><dt>upper</dt><dd>30048.553453808</dd><dt>mean</dt><dd>14277.7</dd><dt>lower</dt><dd>-1493.15345380796</dd></dl>\n"
],
"text/latex": [
"\\begin{description*}\n",
"\\item[upper] 30048.553453808\n",
"\\item[mean] 14277.7\n",
"\\item[lower] -1493.15345380796\n",
"\\end{description*}\n"
],
"text/markdown": [
"upper\n",
": 30048.553453808mean\n",
": 14277.7lower\n",
": -1493.15345380796\n",
"\n"
],
"text/plain": [
" upper mean lower \n",
"30048.553 14277.700 -1493.153 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# 99% confidence interval of values in x\n",
"CI(x, ci = 0.99)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"R version 3.5.1 (2018-07-02)\n",
"Platform: x86_64-conda_cos6-linux-gnu (64-bit)\n",
"Running under: Debian GNU/Linux 10 (buster)\n",
"\n",
"Matrix products: default\n",
"BLAS/LAPACK: /home/jupyterlab/conda/envs/r/lib/R/lib/libRlapack.so\n",
"\n",
"locale:\n",
" [1] LC_CTYPE=C.UTF-8 LC_NUMERIC=C LC_TIME=C.UTF-8 \n",
" [4] LC_COLLATE=C.UTF-8 LC_MONETARY=C.UTF-8 LC_MESSAGES=C.UTF-8 \n",
" [7] LC_PAPER=C.UTF-8 LC_NAME=C LC_ADDRESS=C \n",
"[10] LC_TELEPHONE=C LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C \n",
"\n",
"attached base packages:\n",
"[1] stats graphics grDevices utils datasets methods base \n",
"\n",
"other attached packages:\n",
"[1] Rmisc_1.5 plyr_1.8.6 lattice_0.20-41\n",
"\n",
"loaded via a namespace (and not attached):\n",
" [1] Rcpp_1.0.4.6 digest_0.6.25 crayon_1.3.4 IRdisplay_0.7.0\n",
" [5] repr_1.1.0 grid_3.5.1 jsonlite_1.6.1 evaluate_0.14 \n",
" [9] pillar_1.4.3 rlang_0.4.5 uuid_0.1-4 IRkernel_0.8.12\n",
"[13] tools_3.5.1 compiler_3.5.1 base64enc_0.1-3 htmltools_0.4.0\n",
"[17] pbdZMQ_0.3-3 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"sessionInfo()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "R",
"language": "R",
"name": "conda-env-r-r"
},
"language_info": {
"codemirror_mode": "r",
"file_extension": ".r",
"mimetype": "text/x-r-source",
"name": "R",
"pygments_lexer": "r",
"version": "3.5.1"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment