{"id":65398,"date":"2013-03-08T16:35:26","date_gmt":"2013-03-08T22:35:26","guid":{"rendered":"http:\/\/www.win-vector.com\/blog\/?p=2354"},"modified":"2013-03-08T16:35:26","modified_gmt":"2013-03-08T22:35:26","slug":"a-bit-more-on-sample-size","status":"publish","type":"post","link":"https:\/\/www.r-bloggers.com\/2013\/03\/a-bit-more-on-sample-size\/","title":{"rendered":"A bit more on sample size"},"content":{"rendered":"<!-- \r\n<div style=\"min-height: 30px;\">\r\n[social4i size=\"small\" align=\"align-left\"]\r\n<\/div>\r\n-->\r\n\r\n<div style=\"border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 12px;\">\r\n[This article was first published on  <strong><a href=\"http:\/\/www.win-vector.com\/blog\/2013\/03\/a-bit-more-on-sample-size\/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=a-bit-more-on-sample-size\"> Win-Vector Blog \u00bb R<\/a><\/strong>, and kindly contributed to <a href=\"https:\/\/www.r-bloggers.com\/\" rel=\"nofollow\">R-bloggers<\/a>].  (You can report issue about the content on this page <a href=\"https:\/\/www.r-bloggers.com\/contact-us\/\">here<\/a>)\r\n<hr>Want to share your content on R-bloggers?<a href=\"https:\/\/www.r-bloggers.com\/add-your-blog\/\" rel=\"nofollow\"> click here<\/a> if you have a blog, or <a href=\"http:\/\/r-posts.com\/\" rel=\"nofollow\"> here<\/a> if you don't.\r\n<\/div>\n<p>In our article <a href=\"http:\/\/www.win-vector.com\/blog\/2011\/06\/what-is-a-large-enough-random-sample\/\" rel=\"nofollow\" target=\"_blank\">What is a large enough random sample?<\/a> we pointed out that if you wanted to measure a proportion to an accuracy \u201ca\u201d with chance of being wrong of \u201cd\u201d then a idea was to guarantee you had a sample size of at least:<\/p>\n<p><img loading=\"lazy\" style=\"display:block; margin-left:auto; margin-right:auto;\" src=\"https:\/\/i2.wp.com\/www.win-vector.com\/blog\/wp-content\/uploads\/2013\/03\/NewImage.png?resize=159%2C77\" alt=\"NewImage\" title=\"NewImage.png\" border=\"0\" width=\"159\" height=\"77\" data-recalc-dims=\"1\" \/><\/p>\n<p>This is the central question in designing opinion polls or running A\/B tests.  This estimate comes from a quick application of <a href=\"http:\/\/en.wikipedia.org\/wiki\/Hoeffding_inequality\" rel=\"nofollow\" target=\"_blank\">Hoeffding\u2019s inequality<\/a> and <em>because<\/em> it has a simple form it is possible to see that accuracy is very expensive (to halve the size of difference we are trying to measure we have to multiply the sample size by four) and the cheapness of confidence (increases in the required confidence or <a href=\"http:\/\/www.win-vector.com\/blog\/2009\/12\/statistics-to-english-translation-part-2a-significant-doesnt-always-mean-important\/\" rel=\"nofollow\" target=\"_blank\">significance<\/a> of a result cost only moderately in sample size).  <\/p>\n<p>However, for high-accuracy situations (when you are trying to measure two effects that are very close to each other) suggesting a sample size that is larger than is strictly necessary (as we are using an bound, not an exact formula for the required sample size).  As a theorist or a statistician we like to error on the side of too large a sample (guaranteeing reliability), but somebody who is paying for each entry in a poll would want a smaller size.<\/p>\n<p>This article shows a function that computes the exact size needed (using <a href=\"http:\/\/cran.r-project.org\/\" rel=\"nofollow\" target=\"_blank\">R<\/a>).<span id=\"more-2354\"><\/span>\n<p\/>\n<p>The bound we gave in <a href=\"http:\/\/www.win-vector.com\/blog\/2011\/06\/what-is-a-large-enough-random-sample\/\" rel=\"nofollow\" target=\"_blank\">What is a large enough random sample?<\/a>  is correct: a sample of the stated size always at least achieves the desired accuracy and significance goals.  Sample size and significance seem a bit abstract if you forget the underlying points: too small sample size and you can\u2019t state a conclusion with any confidence and too large a sample size and you have spent too much money on your experiments.  This is a central issue in measurement when you are measuring something serious (a clinical trial, an opinion poll or an A\/B test) or even measuring something silly (<a href=\"http:\/\/www.win-vector.com\/blog\/2012\/12\/how-to-test-xcom-dice-rolls-for-fairness\/\" rel=\"nofollow\" target=\"_blank\">the statistics of a game<\/a>).  In addition to knowing how to estimate significance of an experiment after the fact you need to know how to design an experiment to achieve significance; and that is largely picking a big enough sample size, the subject of this article.<\/p>\n<p>We could get a better bound on sample size by using a more detailed version of the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Chernoff_bounds\" rel=\"nofollow\" target=\"_blank\">Chernoff bound<\/a> that better accounts for small sample sizes.  Or, as we will do here, we can say bounds are only useful if they are simple (so they give us usable intuition) and move on to an exact calculation. <\/p>\n<p>The exact sample size needed is determined by a simple use of the binomial theorem (used to calculate how often a distribution of coin flips exhibits a given range of averages).  The R code to find the exact sample size is given in the function binomsize(a,d) below:<\/p>\n<pre>\nlibrary('gtools')\nestimate = function(a,d) { -log(d\/2)\/(2*a^2) }\nsig = function(a,n) { pbinom(floor(n*0.5)-floor(n*a),size=floor(n),prob=0.5) }\nbinomsize = function(a,d) { \n  r=c(1,2*estimate(a,d))\n  v=binsearch(function(n) {sig(a,n) - d},range=r,lower=min(r),upper=max(r))\n  v$where[[length(v$where)]] }\n<\/pre>\n<p>For example:  binomsize(0.1,0.05) = 80 tells us that a sample size of 80 is enough to measure a difference in rates as small as 0.1 with a chance of mis-measurement of no more then 0.05.  That is if you want to measure the popularity of a single candidate to with +=10% with no more than a 0.05 changes of being wrong, we need a sample size of at least 80 respondents.  In a poll of 80 people if your candidate is marked as favorable by more than 60% of the time then with 19 chances out of 20 they are in fact the more popular candidate (also assuming your sample of 80 was truly representative).  On the other hand, estimate(0.1,0.05) is 184.4, and is more than twice the minimum necessary size (though it is safe to use).<\/p>\n<p>Our estimate was designed to always at least the true value (so it is a valid bound), but it is often much larger than the needed value.  Will illustrate this with the command below which yield the plot that follows.<\/p>\n<pre>\nlibrary('ggplot2')\nlibrary('reshape2')\nd = data.frame(accuracy=10^seq(from=-0.9,to=-7,by=-0.1))\nd$estimateSize = estimate(d$accuracy,0.05)\nd$binomialSize = sapply(d$accuracy,function(x){ binomsize(x,0.05)})\ndmelt = melt(d,id.vars=c('accuracy'))\nggplot(data=dmelt,aes(x=1\/accuracy,y=value,color=variable)) + \n    geom_line()\n<\/pre>\n<p><img loading=\"lazy\" style=\"display:block; margin-left:auto; margin-right:auto;\" src=\"https:\/\/i0.wp.com\/www.win-vector.com\/blog\/wp-content\/uploads\/2013\/03\/rate1.png?w=450\" alt=\"Rate1\" title=\"rate1.png\" border=\"0\" data-recalc-dims=\"1\" \/><\/p>\n<p>The difference doesn\u2019t look so bad if we plot on a log-log scale \u201con which anything looks like a straight line\u201d (F. J. Yndurain, 1996):<\/p>\n<pre>\nggplot(data=dmelt,aes(x=1\/accuracy,y=value,color=variable)) + \n   geom_line() + scale_y_log10() + scale_x_log10()\n<\/pre>\n<p><img loading=\"lazy\" style=\"display:block; margin-left:auto; margin-right:auto;\" src=\"https:\/\/i0.wp.com\/www.win-vector.com\/blog\/wp-content\/uploads\/2013\/03\/rate2.png?w=450\" alt=\"Rate2\" title=\"rate2.png\" border=\"0\" data-recalc-dims=\"1\" \/><\/p>\n<p>In fact we can see the ratio of the estimate over the actual needed sample size is approaching e:<\/p>\n<pre>\nggplot(data=d,aes(x=1\/accuracy,y=estimateSize\/binomialSize)) +\n   geom_line() + scale_x_log10()\n<\/pre>\n<p><img loading=\"lazy\" style=\"display:block; margin-left:auto; margin-right:auto;\" src=\"https:\/\/i0.wp.com\/www.win-vector.com\/blog\/wp-content\/uploads\/2013\/03\/rate3.png?w=450\" alt=\"Rate3\" title=\"rate3.png\" border=\"0\" data-recalc-dims=\"1\" \/><\/p>\n<p>So we can use the following formula as a \u201cgood rule of thumb\u201d (but not as a bound, as it is always not quite a large enough sample!): you should always have a sample size larger than:<\/p>\n<p><img loading=\"lazy\" style=\"display:block; margin-left:auto; margin-right:auto;\" src=\"https:\/\/i2.wp.com\/www.win-vector.com\/blog\/wp-content\/uploads\/2013\/03\/NewImage1.png?resize=159%2C77\" alt=\"NewImage\" title=\"NewImage.png\" border=\"0\" width=\"159\" height=\"77\" data-recalc-dims=\"1\" \/><\/p>\n<p>Our advice is: use our bound or the rule of thumb to plan.  But the time comes to run your test, quickly use the binomial formula given above.<\/p>\n<div class='yarpp-related-rss'>\n<p>Related posts:<ol>\n<li><a href='http:\/\/www.win-vector.com\/blog\/2011\/06\/what-is-a-large-enough-random-sample\/' rel=\"nofollow\" title='What is a large enough random sample?' target=\"_blank\">What is a large enough random sample?<\/a><\/li>\n<li><a href='http:\/\/www.win-vector.com\/blog\/2012\/10\/level-fit-summaries-can-be-tricky-in-r\/' rel=\"nofollow\" title='Level fit summaries can be tricky in R' target=\"_blank\">Level fit summaries can be tricky in R<\/a><\/li>\n<li><a href='http:\/\/www.win-vector.com\/blog\/2013\/02\/dont-use-correlation-to-track-prediction-performance\/' rel=\"nofollow\" title='Don\u2019t use correlation to track prediction performance' target=\"_blank\">Don\u2019t use correlation to track prediction performance<\/a><\/li>\n<\/ol><\/p>\n<img src='http:\/\/yarpp.org\/pixels\/b8781af2b90c83bd11d1e98c04b31afb'\/>\n<\/div>\n\n<div style=\"border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 13px;\">\r\n<div style=\"text-align: center;\">To <strong>leave a comment<\/strong> for the author, please follow the link and comment on their blog: <strong><a href=\"http:\/\/www.win-vector.com\/blog\/2013\/03\/a-bit-more-on-sample-size\/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=a-bit-more-on-sample-size\"> Win-Vector Blog \u00bb R<\/a><\/strong>.<\/div>\r\n<hr \/>\r\n<a href=\"https:\/\/www.r-bloggers.com\/\" rel=\"nofollow\">R-bloggers.com<\/a> offers <strong><a href=\"https:\/\/feedburner.google.com\/fb\/a\/mailverify?uri=RBloggers\" rel=\"nofollow\">daily e-mail updates<\/a><\/strong> about <a title=\"The R Project for Statistical Computing\" href=\"https:\/\/www.r-project.org\/\" rel=\"nofollow\">R<\/a> news and tutorials about <a title=\"R tutorials\" href=\"https:\/\/www.r-bloggers.com\/how-to-learn-r-2\/\" rel=\"nofollow\">learning R<\/a> and many other topics. <a title=\"Data science jobs\" href=\"https:\/\/www.r-users.com\/\" rel=\"nofollow\">Click here if you're looking to post or find an R\/data-science job<\/a>.\r\n\r\n<hr>Want to share your content on R-bloggers?<a href=\"https:\/\/www.r-bloggers.com\/add-your-blog\/\" rel=\"nofollow\"> click here<\/a> if you have a blog, or <a href=\"http:\/\/r-posts.com\/\" rel=\"nofollow\"> here<\/a> if you don't.\r\n<\/div>","protected":false},"excerpt":{"rendered":"<p>In our article What is a large enough random sample? we pointed out that if you wanted to measure a proportion to an accuracy \u201ca\u201d with chance of being wrong of \u201cd\u201d then a idea was to guarantee you had a sample size of at least: This is the central &#8230;<\/p>\n","protected":false},"author":293,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"aioseo_notices":[],"jetpack-related-posts":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/posts\/65398"}],"collection":[{"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/users\/293"}],"replies":[{"embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/comments?post=65398"}],"version-history":[{"count":0,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/posts\/65398\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/media?parent=65398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/categories?post=65398"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/tags?post=65398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}