{"id":173,"date":"2013-03-17T15:13:38","date_gmt":"2013-03-17T19:13:38","guid":{"rendered":"http:\/\/www.clayford.net\/statistics\/?p=173"},"modified":"2024-11-17T11:07:24","modified_gmt":"2024-11-17T16:07:24","slug":"using-simulation-to-compute-confidence-intervals","status":"publish","type":"post","link":"https:\/\/www.clayford.net\/statistics\/using-simulation-to-compute-confidence-intervals\/","title":{"rendered":"Using Simulation to Compute Confidence Intervals"},"content":{"rendered":"<p>I&#8217;ve been working through Gelman and Hill&#8217;s book, <em><a href=\"http:\/\/www.stat.columbia.edu\/~gelman\/arm\/\">Data Analysis Using Regression and Multilevel\/Hierarchical Models<\/a><\/em>. I originally wanted to read it and blog about each chapter the way I did <em>Machine Learning for Hackers<\/em>. But that book had 12 chapters. This one has 25. And the chapters are longer and denser. I don&#8217;t think I have the appetite for such an endeavor. However, I&#8217;m still working through the book and want to at least blog about certain topics that catch my eye. Today&#8217;s post comes from Chapter 2, Concepts and Methods from Basic Probability and Statistics.<\/p>\n<p>In reviewing confidence intervals, the authors mention using simulation to form confidence intervals for statistics that have complicated standard error formulas. It&#8217;s one thing to compute a confidence interval for a mean. The standard error is \\( \\frac{s}{\\sqrt{n}} \\). But what about a ratio? Well, that&#8217;s more difficult. Thus the book gives a nice example on how to use simulation to find a confidence interval for a ratio (p. 20). Instead of reproducing it verbatim, I thought I would apply the strategy to finding a confidence interval for a median.<\/p>\n<p>My old stats textbook, <em><a href=\"http:\/\/www.amazon.com\/Probability-Statistical-Inference-7th-Edition\/dp\/0131464132\">Probability and Statistical Inference<\/a><\/em> by Hogg and Tanis (7th edition) has a problem in section 6.10 (#7) that gives you measurements of one of the front legs of 20 different spiders and asks you to find a 95% confidence interval for the median. Now this chapter presents a method for calculating this, which is covered in depth at this <a href=\"https:\/\/onlinecourses.science.psu.edu\/stat414\/node\/316\">very nice Penn State web site<\/a>. Since the problem is odd-numbered, I can flip to the back and see the answer is (15.40, 17.05). Let&#8217;s try finding a 95% confidence interval for this data using simulation.<\/p>\n<p>First off, here is the data:<\/p>\n<pre>\r\nx <- scan(\"Exercise_6_10-07.txt\",skip=1)\r\n> sort(x)\r\n [1] 13.55 13.60 14.05 15.10 15.25 15.40 15.45 15.75 16.25 16.40 16.45 16.65\r\n[13] 16.80 16.95 17.05 17.55 17.75 19.05 19.05 20.00\r\n<\/pre>\n<p>The measurements are in millimeters. The median of this data is 16.425, easily calculated in R with the median() function. Now what about a confidence interval? This is after all a sample of 20 spiders. Our median is but a point estimate of the population median, a value we will never be able to directly measure. Using R we can simulate a large number of samples by re-sampling from the data and taking the median each time, like this:<\/p>\n<pre>\r\nnsims <- 1000 # number of simulations\r\nm <- rep(NA,nsims) # empty vector to store medians\r\nfor (i in 1:nsims){  \r\n\tm[i] <- median(sample(x,replace=TRUE))\r\n\t}\r\n<\/pre>\n<p>When we're done, we can use the quantile() function as follows to find the 2.5 and 97.5 percentiles and thus estimate a confidence interval:<\/p>\n<pre>\r\nquantile(m,c(0.025,0.975))\r\n    2.5%    97.5% \r\n15.42500 17.00125 \r\n<\/pre>\n<p>That's very close to the given answer of (15.40, 17.05). This example would technically be filed under \"bootstrap\", but I think it captures the spirit of using simulation to find a confidence interval.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been working through Gelman and Hill&#8217;s book, Data Analysis Using Regression and Multilevel\/Hierarchical Models. I originally wanted to read&#8230; <a class=\"read-more\" href=\"https:\/\/www.clayford.net\/statistics\/using-simulation-to-compute-confidence-intervals\/\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,13],"tags":[],"class_list":["post-173","post","type-post","status-publish","format-standard","hentry","category-simulation","category-using-r"],"_links":{"self":[{"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/posts\/173","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/comments?post=173"}],"version-history":[{"count":3,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/posts\/173\/revisions"}],"predecessor-version":[{"id":986,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/posts\/173\/revisions\/986"}],"wp:attachment":[{"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/media?parent=173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/categories?post=173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/tags?post=173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}