suzuki fraction demo
authorDan Vanderkam <danvdk@gmail.com>
Tue, 8 Dec 2009 06:45:44 +0000 (22:45 -0800)
committerDan Vanderkam <danvdk@gmail.com>
Tue, 8 Dec 2009 06:45:44 +0000 (22:45 -0800)
docs/index.html
docs/suzuki-mariners.txt [new file with mode: 0644]

index e75c9a4..ea84048 100644 (file)
@@ -56,6 +56,7 @@
   <ul>
   <li><a href="#demo">Demo</a>
   <li><a href="#usage">Usage</a>
+  <li><a href="#baseball">Baseball chart</a>
   <li><a href="#stock">Stock chart</a>
   <li><a href="#options">Options Reference</a>
   <li><a href="#policy">Data Policy</a>
@@ -353,6 +354,53 @@ new Dygraph(
   <li>The error bars are partially transparent. This can be seen when they overlap one another.</li>
 </ul>
 
+<a name="baseball"><h2>Charting Fractions</h2>
+<p>Situations often arise where you want to plot fractions, e.g. the fraction of respondents in a poll who said they'd vote for candidate X or the number of hits divided by at bats (baseball's batting average). Fractions require special treatment for two main reasons:</p>
+
+<ul>
+<li>The average of <code>a1/b1</code> and <code>a2/b2</code> is
+<code>(a1+a2)/(b1+b2)</code>, not <code>(a1/b1 + a2/b2)/2</code>.
+<li>The normal approximation is not always applicable and more sophisticated
+confidence intervals (e.g. the <a
+href="http://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval">Wilson
+confidence interval</a>) must be employed to avoid ratios that exceed 100% or
+go below 0%.
+</ul>
+
+<p>Fortunately, dygraphs handles both of these for you! Here's a chart and the command that generated it:</p>
+
+<div style="width:750px; text-align:center; font-weight: bold; font-size: 125%;">Batting Average for Ichiro Suzuki vs. Mariners (2004)</div>
+<div id="baseballdiv" style="width:750px; height:300px;"></div>
+<script type="text/javascript">
+new Dygraph(document.getElementById("baseballdiv"),
+            "suzuki-mariners.txt", {
+              fractions: true,
+              errorBars: true,
+              showRoller: true,
+              rollPeriod: 15
+            });
+</script>
+
+<p>Command:</p>
+<pre>
+new Dygraph(
+  document.getElementById("baseballdiv"), "suzuki-mariners.txt",
+  {
+    fractions: true, errorBars: true,
+    showRoller: true, rollPeriod: 15
+  });
+</pre>
+
+<p>The <code>fractions</code> option indicates that the values in each column should be parsed as fractions (e.g. "1/2" instead of "0.5"). The <code>errorBars</code> option indicates that we'd like to see a confidence interval around each data point. By default, when <code>fractions</code> is set, you get a Wilson confidence interval. If you look carefully at the chart, you can see that the error bars are asymmetric.</p>
+
+<p>A couple things to notice about this chart:</p>
+<ul>
+<li>The error bars for Ichiro's batting average are larger than for the Mariners', since he has far fewer at bats than his team.
+<li>dygraphs makes it easy to see "batting average over the last 30 games". This is ordinarily quite difficult to compute. It makes it clear where the "hot" and "cold" part of Suzuki's season were.
+<li>If you set the averaging period to something large, like 200, you'll see the team's and player's batting average through that game. The final number is the overall batting average for the season.
+<li>Where the error bars do not overlap, we can say with 95% confidence that the series differ. There is a better than 95% chance that Ichiro was a better hitter than his team as a whole in 2004, the year he won the batting title.
+</ul>
+
 <a name="stock"><h2>One last demo</h2>
 
 <p>This chart shows monthly closes of the Dow Jones Industrial Average, both in nominal and real (i.e. adjusted for inflation) dollars. The shaded areas show its monthly high and low. CPI values with a base from 1982-84 are used to adjust for inflation.</p>
diff --git a/docs/suzuki-mariners.txt b/docs/suzuki-mariners.txt
new file mode 100644 (file)
index 0000000..0b8dfa4
--- /dev/null
@@ -0,0 +1,163 @@
+Game,Suzuki,Mariners
+1,1/4,7/35
+2,1/5,13/39
+3,0/4,7/31
+4,3/5,13/39
+5,2/4,4/30
+6,2/5,14/44
+7,2/5,11/36
+8,2/5,12/36
+9,0/5,8/36
+10,0/5,8/32
+11,1/4,5/29
+12,3/4,10/33
+13,2/6,10/47
+14,0/3,4/28
+15,1/3,9/31
+16,0/4,7/31
+17,2/4,12/37
+18,1/4,7/32
+19,1/5,15/40
+20,1/4,13/40
+21,0/5,4/31
+22,0/4,5/32
+23,1/5,9/39
+24,2/4,6/31
+25,2/5,14/39
+26,3/8,13/55
+27,2/4,5/32
+28,1/4,5/28
+29,2/4,12/36
+30,2/4,4/31
+31,1/4,8/34
+32,1/6,18/50
+33,2/4,10/34
+34,2/4,8/31
+35,2/5,12/34
+36,3/5,15/49
+37,2/4,9/34
+38,2/4,6/33
+39,0/3,7/34
+40,3/5,13/36
+41,3/5,8/35
+42,0/3,7/34
+43,1/4,9/29
+44,1/6,12/48
+45,4/5,13/40
+46,1/5,8/35
+47,2/5,11/36
+48,2/5,11/37
+49,1/6,8/40
+50,3/4,10/31
+51,2/4,9/37
+52,2/5,10/38
+53,1/4,5/31
+54,1/2,7/28
+55,2/5,11/36
+56,2/5,11/33
+57,0/4,6/33
+58,1/4,7/32
+59,1/3,10/30
+60,1/3,5/26
+61,1/4,14/36
+62,0/3,2/27
+63,1/3,6/32
+64,3/5,11/35
+65,0/4,9/32
+66,2/4,8/35
+67,1/4,6/30
+68,1/4,5/32
+69,0/3,8/34
+70,1/7,12/61
+71,0/4,8/33
+72,3/5,13/35
+73,0/4,3/30
+74,1/4,9/32
+75,0/4,8/30
+76,2/5,9/35
+77,3/5,12/36
+78,2/4,6/32
+79,0/3,6/33
+80,1/4,6/32
+81,2/5,10/35
+82,1/5,13/38
+83,2/4,14/36
+84,1/4,7/33
+85,0/0,9/32
+86,2/4,7/32
+87,1/4,4/28
+88,1/3,7/33
+89,1/5,13/38
+90,2/4,10/36
+91,2/5,10/42
+92,4/5,18/43
+93,4/5,12/36
+94,1/4,7/29
+95,1/5,9/32
+96,3/5,9/35
+97,1/4,8/30
+98,3/5,12/35
+99,2/5,9/32
+100,3/4,8/35
+101,5/7,18/56
+102,0/4,7/33
+103,3/6,14/46
+104,2/5,10/34
+105,5/5,16/41
+106,1/1,15/42
+107,3/5,9/35
+108,3/6,10/41
+109,0/4,2/34
+110,2/5,12/36
+111,2/5,9/36
+112,3/4,10/32
+113,2/4,7/32
+114,2/5,9/35
+115,0/3,7/32
+116,1/4,11/36
+117,3/4,12/34
+118,4/4,20/47
+119,0/1,5/29
+120,3/4,9/34
+121,4/7,21/52
+122,1/5,11/35
+123,1/4,5/32
+124,1/4,7/35
+125,0/4,7/33
+126,1/5,8/33
+127,2/4,14/35
+128,1/4,12/33
+129,3/6,12/44
+130,3/4,8/33
+131,3/5,16/39
+132,2/3,6/33
+133,3/5,11/37
+134,1/4,11/37
+135,5/5,15/37
+136,1/4,9/35
+137,2/4,5/30
+138,1/5,7/34
+139,2/4,9/34
+140,0/4,6/33
+141,0/4,5/33
+142,2/4,6/28
+143,0/4,7/34
+144,1/4,7/27
+145,1/4,6/30
+146,0/2,6/33
+147,2/4,10/32
+148,1/5,9/36
+149,0/4,8/33
+150,2/4,12/39
+151,5/5,18/41
+152,4/6,24/47
+153,2/4,14/39
+154,1/5,8/36
+155,1/4,13/37
+156,1/4,8/33
+157,2/5,13/37
+158,1/3,10/35
+159,1/5,9/35
+160,3/5,18/41
+161,1/5,10/37
+162,2/4,4/30