{"id":21028116,"url":"https://github.com/leonjessen/confidence_intervals_visualised","last_synced_at":"2025-05-15T10:33:10.243Z","repository":{"id":145190199,"uuid":"115655065","full_name":"leonjessen/confidence_intervals_visualised","owner":"leonjessen","description":"A brief demonstration on how to visualise confidence intervals in R","archived":false,"fork":false,"pushed_at":"2017-12-28T19:56:40.000Z","size":51,"stargazers_count":7,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-03T08:02:57.757Z","etag":null,"topics":["datascience","datavisualization","demo","demonstration","ggplot","r","statistics","tidyverse","tutorial"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/leonjessen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-12-28T19:55:13.000Z","updated_at":"2020-01-12T19:47:06.000Z","dependencies_parsed_at":null,"dependency_job_id":"678550af-cefb-42fd-8999-7ceec8f3c1ed","html_url":"https://github.com/leonjessen/confidence_intervals_visualised","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leonjessen%2Fconfidence_intervals_visualised","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leonjessen%2Fconfidence_intervals_visualised/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leonjessen%2Fconfidence_intervals_visualised/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leonjessen%2Fconfidence_intervals_visualised/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/leonjessen","download_url":"https://codeload.github.com/leonjessen/confidence_intervals_visualised/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254323252,"owners_count":22051746,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["datascience","datavisualization","demo","demonstration","ggplot","r","statistics","tidyverse","tutorial"],"created_at":"2024-11-19T11:53:57.497Z","updated_at":"2025-05-15T10:33:10.228Z","avatar_url":"https://github.com/leonjessen.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"Confidence Intervals Visualised\n================\n\nThis is a very brief demonstration on how to visualise confidence intervals\n\n#### Getting started\n\nFirst we clear the workspace, load required packages and set a seed for reproducibility when working with random number generation\n\n``` r\nrm(list=ls())\nlibrary('tidyverse')\nset.seed(196628)\n```\n\n#### Define functions\n\nThen we create a function for repeatedly drawing random normal distributed numbers and calculating sample mean and confidence intervals\n\n``` r\nmk_samples = function(n, size, mean = 0, sd = 1){\n  m = sapply(1:n, function(i){\n    s = rnorm(n = size, mean = mean, sd = sd)\n    return( c(mean(s), t.test(s)$conf.int) )\n  })\n  rownames(m) = c(\"sample_mean\", \"ci_lower\", \"ci_upper\")\n  m = m %\u003e% t %\u003e% as_tibble\n  return(m)\n}\n```\n\n#### Make data\n\nNow we run the `mk_samples()` function\n\n``` r\nn = 100\nm = mk_samples(n = n, size = 100) %\u003e%\n  arrange(desc(ci_lower)) %\u003e% \n  mutate(i = seq(1,n)) %\u003e% \n  mutate(contains_pop_mean = ifelse(0 \u003e ci_lower \u0026 0 \u003c ci_upper,'yes','no') %\u003e%\n           factor(levels = c('yes', 'no')))\nm\n```\n\n    ## # A tibble: 100 x 5\n    ##    sample_mean     ci_lower  ci_upper     i contains_pop_mean\n    ##          \u003cdbl\u003e        \u003cdbl\u003e     \u003cdbl\u003e \u003cint\u003e            \u003cfctr\u003e\n    ##  1  0.27107828  0.056091337 0.4860652     1                no\n    ##  2  0.21680406  0.018569854 0.4150383     2                no\n    ##  3  0.20741777  0.008901981 0.4059336     3                no\n    ##  4  0.17929221 -0.012861105 0.3714455     4               yes\n    ##  5  0.17851806 -0.064949428 0.4219856     5               yes\n    ##  6  0.13496431 -0.069553520 0.3394821     6               yes\n    ##  7  0.12009085 -0.075892146 0.3160738     7               yes\n    ##  8  0.08889772 -0.090681676 0.2684771     8               yes\n    ##  9  0.10405637 -0.091063876 0.2991766     9               yes\n    ## 10  0.09227670 -0.112272478 0.2968259    10               yes\n    ## # ... with 90 more rows\n\n#### Visualise Confidence Intervals\n\nFinally, we can visualise the calculated confidence intervals and see the definition of a confidence interval: If samples are repeatedly drawn from a population, then the 95% confidence interval of 95 in 100 samples contain the population mean:\n\n``` r\nm %\u003e%\n  ggplot(aes(x=sample_mean,y=i,colour=contains_pop_mean)) +\n  geom_point() +\n  geom_errorbarh(aes(xmin=ci_lower,xmax=ci_upper),height=1) +\n  geom_vline(xintercept = 0, linetype = 'dashed') +\n  scale_y_continuous(expand = c(0, 0)) +\n  scale_color_manual(values=c('darkgrey','red')) +\n  xlab(paste0('Sample mean +/- 95% confidence interval whiskers\\n',\n              'Each sample is n=100 draws from N(0,1)')) +\n  ylab(\"Sample number\") +\n  theme_bw() +\n  theme(panel.grid.major.y = element_blank(),\n        panel.grid.minor.y = element_blank(),\n        legend.position='bottom')\n```\n\n![](README_files/figure-markdown_github/visualise_confints-1.png)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleonjessen%2Fconfidence_intervals_visualised","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fleonjessen%2Fconfidence_intervals_visualised","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleonjessen%2Fconfidence_intervals_visualised/lists"}