new file: Files/flashplayer_32_sa.exe new file: favicon.ico new file: globe.gif new file: imgs/download.png new file: imgs/zuck.jpg new file: index.html new file: other.ico new file: script.js new file: site.webmanifest new file: sitemap.html new file: styles/backround.css new file: styles/border.css new file: styles/fonts/Titillium_Web/OFL.txt new file: styles/fonts/Titillium_Web/TitilliumWeb-Black.ttf new file: styles/fonts/Titillium_Web/TitilliumWeb-Bold.ttf new file: styles/fonts/Titillium_Web/TitilliumWeb-BoldItalic.ttf new file: styles/fonts/Titillium_Web/TitilliumWeb-ExtraLight.ttf new file: styles/fonts/Titillium_Web/TitilliumWeb-ExtraLightItalic.ttf new file: styles/fonts/Titillium_Web/TitilliumWeb-Italic.ttf new file: styles/fonts/Titillium_Web/TitilliumWeb-Light.ttf new file: styles/fonts/Titillium_Web/TitilliumWeb-LightItalic.ttf new file: styles/fonts/Titillium_Web/TitilliumWeb-Regular.ttf new file: styles/fonts/Titillium_Web/TitilliumWeb-SemiBold.ttf new file: styles/fonts/Titillium_Web/TitilliumWeb-SemiBoldItalic.ttf new file: styles/fonts/webfontkit-20221027-163353/generator_config.txt new file: styles/fonts/webfontkit-20221027-163353/specimen_files/grid_12-825-55-15.css new file: styles/fonts/webfontkit-20221027-163353/specimen_files/specimen_stylesheet.css new file: styles/fonts/webfontkit-20221027-163353/stylesheet.css new file: styles/fonts/webfontkit-20221027-163353/titilliumweb-extralight-demo.html new file: styles/fonts/webfontkit-20221027-163353/titilliumweb-extralight-webfont.woff new file: styles/fonts/webfontkit-20221027-163353/titilliumweb-extralight-webfont.woff2 new file: styles/fonts/webfontkit-20221027-165950/generator_config.txt new file: styles/fonts/webfontkit-20221027-165950/specimen_files/grid_12-825-55-15.css new file: styles/fonts/webfontkit-20221027-165950/specimen_files/specimen_stylesheet.css new file: styles/fonts/webfontkit-20221027-165950/stylesheet.css new file: styles/fonts/webfontkit-20221027-165950/titilliumweb-bold-demo.html new file: styles/fonts/webfontkit-20221027-165950/titilliumweb-bold-webfont.woff new file: styles/fonts/webfontkit-20221027-165950/titilliumweb-bold-webfont.woff2 new file: styles/style.css new file: tools/2048/.gitignore new file: tools/2048/.jshintrc new file: tools/2048/CONTRIBUTING.md new file: tools/2048/LICENSE.txt new file: tools/2048/README.md new file: tools/2048/Rakefile new file: tools/2048/favicon.ico new file: tools/2048/index.html new file: tools/2048/js/animframe_polyfill.js new file: tools/2048/js/application.js new file: tools/2048/js/bind_polyfill.js new file: tools/2048/js/classlist_polyfill.js new file: tools/2048/js/game_manager.js new file: tools/2048/js/grid.js new file: tools/2048/js/html_actuator.js new file: tools/2048/js/keyboard_input_manager.js new file: tools/2048/js/local_storage_manager.js new file: tools/2048/js/tile.js new file: tools/2048/meta/apple-touch-icon.png new file: tools/webretro/cores/neocd_libretro.js new file: tools/webretro/cores/neocd_libretro.wasm new file: tools/webretro/cores/nestopia_libretro.js new file: tools/webretro/cores/nestopia_libretro.wasm new file: tools/webretro/cores/o2em_libretro.js new file: tools/webretro/cores/o2em_libretro.wasm new file: tools/webretro/cores/opera_libretro.js new file: tools/webretro/cores/opera_libretro.wasm
326 lines
13 KiB
HTML
326 lines
13 KiB
HTML
<!doctype html>
|
|
<html lang="en"><!-- #BeginTemplate "/Templates/Advanced.dwt" --><!-- DW6 -->
|
|
|
|
<!-- Mirrored from www.mathsisfun.com/data/least-squares-regression.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 29 Oct 2022 00:31:59 GMT -->
|
|
<head>
|
|
<!-- #BeginEditable "doctitle" -->
|
|
<title>Least Squares Regression</title>
|
|
<meta name="description" content="Math explained in easy language, plus puzzles, games, quizzes, videos and worksheets. For K-12 kids, teachers and parents." />
|
|
<!-- #EndEditable -->
|
|
<meta name="keywords" content="math, maths, mathematics, school, homework, education" />
|
|
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
|
|
<meta name="viewport" content="width=device-width; initial-scale=1.0; user-scalable=true;" />
|
|
<meta name="HandheldFriendly" content="true"/>
|
|
<meta http-equiv="pics-label" content='(PICS-1.1 "http://www.classify.org/safesurf/" L gen true for "http://www.mathsisfun.com" r (SS~~000 1))' />
|
|
<link rel="stylesheet" type="text/css" href="../style3.css" />
|
|
<script src="../main3.js" type="text/javascript"></script>
|
|
</head>
|
|
|
|
<body id="bodybg" class="adv">
|
|
<div class="bg">
|
|
<div id="stt"></div>
|
|
<div id="hdr"></div>
|
|
<div id="logo"><a href="../index.html"><img src="../images/style/logo.svg" alt="Math is Fun" /></a></div>
|
|
<div id="advText">Advanced</div>
|
|
<div id="gtran"><script type="text/javascript">document.write(getTrans());</script></div>
|
|
<div id="gplus"><script type="text/javascript">document.write(getGPlus());</script></div>
|
|
<div id="adTopOuter" class="centerfull noprint">
|
|
<div id="adTop">
|
|
<script type="text/javascript">document.write(getAdTop());</script>
|
|
</div>
|
|
</div>
|
|
<div id="adHide">
|
|
<div id="showAds1"><a href="javascript:showAds()">Show Ads</a></div>
|
|
<div id="hideAds1"><a href="javascript:hideAds()">Hide Ads</a><br>
|
|
<a href="../about-ads.html">About Ads</a></div>
|
|
</div>
|
|
<div id="menuWide" class="menu">
|
|
<script type="text/javascript">document.write(getMenu(0));</script>
|
|
</div>
|
|
<div id="linkto">
|
|
<div id="linktort"><script type="text/javascript">document.write(getLinks());</script></div>
|
|
</div>
|
|
<div id="search" role="search"><script type="text/javascript">document.write(getSearch());</script></div>
|
|
<div id="menuSlim" class="menu">
|
|
<script type="text/javascript">document.write(getMenu(1));</script>
|
|
</div>
|
|
<div id="menuTiny" class="menu">
|
|
<script type="text/javascript">document.write(getMenu(2));</script>
|
|
</div>
|
|
<div id="extra"></div>
|
|
</div>
|
|
<div id="content" role="main"><!-- #BeginEditable "Body" -->
|
|
|
|
<h1 class="center">Least Squares Regression</h1>
|
|
<h2>Line of Best Fit</h2>
|
|
<p>Imagine you have some points, and want to have a <b>line</b> that best fits them like this:</p>
|
|
<p class="center"><img src="images/scatter-ice-cream1a.svg" alt="scatter plot ice cream vs temp with line" style="max-width:100%" /><br>
|
|
</p>
|
|
<p>We can place the line "by eye": try to have the line as close as possible to all points, and a similar number of points above and below the line.</p>
|
|
<p>But for better accuracy let's see how to calculate the line using <b>Least Squares Regression</b>.</p>
|
|
<h2>The Line</h2>
|
|
<p>Our aim is to calculate the values <b>m</b> (slope) and <b>b</b> (y-intercept) in the <a href="../equation_of_line.html">equation of a line</a> :</p>
|
|
|
|
<div class="center80">
|
|
<div align="center"><span class="largest">y = mx + b</span></div>
|
|
</div>
|
|
<p>Where: </p>
|
|
<ul>
|
|
<li><b class="larger">y</b> = how far up</li>
|
|
<li><b class="larger">x</b> = how far along</li>
|
|
<li><b class="larger">m</b> = <a href="../geometry/slope.html">Slope</a> or <a href="../gradient.html">Gradient</a> (how steep the line is)</li>
|
|
<li><b class="larger">b</b> = the <a href="../y_intercept.html">Y Intercept</a> (where the line crosses the Y axis)</li>
|
|
</ul>
|
|
<h2>Steps</h2>
|
|
<p>To find the line of best fit for <b>N</b> points:</p>
|
|
<div class="def">
|
|
<p><b>Step 1</b>: For each (x,y) point calculate x<sup>2</sup> and xy</p>
|
|
<p><b>Step 2</b>: Sum all x, y, x<sup>2</sup> and xy, which gives us Σx, Σy, Σx<sup>2</sup> and Σxy (<a href="../algebra/sigma-notation.html">Σ means "sum up"</a>)</p>
|
|
<p><b>Step 3</b>: Calculate Slope <b>m</b>:</p>
|
|
<p class="center larger"><b>m</b> = <span class="intbl"> <em>N Σ(xy) − Σx Σy</em> <strong>N Σ(x<sup>2</sup>) − (Σx)<sup>2</sup></strong></span></p>
|
|
<p>(N is the number of points.)</p>
|
|
<p><b>Step 4</b>: Calculate Intercept <b>b</b>:</p>
|
|
<p class="center larger"><b>b</b> = <span class="intbl"> <em>Σy − m Σx</em> <strong>N</strong></span></p>
|
|
<p><b>Step 5</b>: Assemble the equation of a line</p>
|
|
<p class="center large">y = mx + b</p>
|
|
<p>Done! </p>
|
|
</div>
|
|
|
|
<h2>Example </h2>
|
|
<p>Let's have an example to see how to do it!</p>
|
|
<div class="example">
|
|
<p style="float:right; margin: 0 0 5px 10px;"><img src="../combinatorics/images/ice-cream.jpg" width="100" height="133" alt="ice cream"></p>
|
|
<h3>Example: Sam found how many <b>hours of sunshine</b> vs how many <b>ice creams</b> were sold at the shop from Monday to Friday:</h3>
|
|
<div class="simple">
|
|
<table border="0" align="center">
|
|
<tr align="right">
|
|
<th width="100">"x"<br>
|
|
Hours of Sunshine</th>
|
|
<th width="100">"y"<br>
|
|
Ice Creams Sold</th>
|
|
</tr>
|
|
<tr>
|
|
<td align="right">2</td>
|
|
<td align="right">4</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="right">3</td>
|
|
<td align="right">5</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="right">5</td>
|
|
<td align="right">7</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="right">7</td>
|
|
<td align="right">10</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="right">9</td>
|
|
<td align="right">15</td>
|
|
</tr>
|
|
</table></div>
|
|
|
|
<p>Let us find the best <b>m</b> (slope) and <b>b</b> (y-intercept) that suits that data</p>
|
|
<p class="center larger">y = mx + b</p>
|
|
|
|
<p> </p>
|
|
|
|
<p><b>Step 1</b>: For each (x,y) calculate x<sup>2</sup> and xy:</p>
|
|
<div class="simple">
|
|
<table border="0" align="center">
|
|
<tr align="right">
|
|
<th width="70">x</th>
|
|
<th width="70">y</th>
|
|
<th width="70">x<sup>2</sup></th>
|
|
<th width="70">xy</th>
|
|
</tr>
|
|
<tr>
|
|
<td width="70" align="right">2</td>
|
|
<td width="70" align="right">4</td>
|
|
<td width="70" align="right">4</td>
|
|
<td width="70" align="right">8</td>
|
|
</tr>
|
|
<tr>
|
|
<td width="70" align="right">3</td>
|
|
<td width="70" align="right">5</td>
|
|
<td width="70" align="right">9</td>
|
|
<td width="70" align="right">15</td>
|
|
</tr>
|
|
<tr>
|
|
<td width="70" align="right">5</td>
|
|
<td width="70" align="right">7</td>
|
|
<td width="70" align="right">25</td>
|
|
<td width="70" align="right">35</td>
|
|
</tr>
|
|
<tr>
|
|
<td width="70" align="right">7</td>
|
|
<td width="70" align="right">10</td>
|
|
<td width="70" align="right">49</td>
|
|
<td width="70" align="right">70</td>
|
|
</tr>
|
|
<tr>
|
|
<td width="70" align="right">9</td>
|
|
<td width="70" align="right">15</td>
|
|
<td width="70" align="right">81</td>
|
|
<td width="70" align="right">135</td>
|
|
</tr>
|
|
</table>
|
|
</div>
|
|
<p><b>Step 2</b>: Sum x, y, x<sup>2</sup> and xy (gives us Σx, Σy, Σx<sup>2</sup> and Σxy):</p>
|
|
<div class="simple">
|
|
<table border="0" align="center">
|
|
<tr align="right">
|
|
<th width="90">x</th>
|
|
<th width="90">y</th>
|
|
<th width="90">x<sup>2</sup></th>
|
|
<th width="90">xy</th>
|
|
</tr>
|
|
<tr>
|
|
<td align="right">2</td>
|
|
<td align="right">4</td>
|
|
<td align="right">4</td>
|
|
<td align="right">8</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="right">3</td>
|
|
<td align="right">5</td>
|
|
<td align="right">9</td>
|
|
<td align="right">15</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="right">5</td>
|
|
<td align="right">7</td>
|
|
<td align="right">25</td>
|
|
<td align="right">35</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="right">7</td>
|
|
<td align="right">10</td>
|
|
<td align="right">49</td>
|
|
<td align="right">70</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="right">9</td>
|
|
<td align="right">15</td>
|
|
<td align="right">81</td>
|
|
<td align="right">135</td>
|
|
</tr>
|
|
<tr align="right">
|
|
<td><b>Σx: 26</b></td>
|
|
<td><b>Σy: 41</b></td>
|
|
<td><b>Σx<sup>2</sup>: 168</b></td>
|
|
<td><b>Σxy: 263</b></td>
|
|
</tr>
|
|
</table>
|
|
</div>
|
|
<p class="center">Also <b>N</b> (number of data values) = <b>5 </b></p>
|
|
<p><b>Step 3</b>: Calculate Slope <b>m</b>:</p>
|
|
<p class="center larger"><b>m</b> = <span class="intbl"><em>N Σ(xy) − Σx Σy</em>
|
|
<strong>N Σ(x<sup>2</sup>) − (Σx)<sup>2</sup></strong></span></p>
|
|
<p class="center larger"> = <span class="intbl"> <em>5 x 263 − 26 x 41</em> <strong>5 x 168 − 26<sup>2</sup></strong></span></p>
|
|
<p class="center larger"> = <span class="intbl"> <em>1315 − 1066</em> <strong>840 − 676</strong></span></p>
|
|
<p class="center larger"> = <span class="intbl"> <em>249</em> <strong>164</strong></span> = 1.5183...</p>
|
|
<p><b>Step 4</b>: Calculate Intercept <b>b</b>:</p>
|
|
<p class="center larger"><b>b</b> = <span class="intbl">
|
|
<em>Σy − m Σx</em>
|
|
<strong>N</strong>
|
|
</span></p>
|
|
<p class="center larger">= <span class="intbl">
|
|
<em>41 − 1.5183 x 26</em>
|
|
<strong>5</strong>
|
|
</span></p>
|
|
|
|
<p class="center larger">= 0.3049...</p>
|
|
<p><b>Step 5</b>: Assemble the equation of a line:</p>
|
|
<p class="center large">y = mx + b</p>
|
|
<p class="center large">y = 1.518x + 0.305</p>
|
|
<p>Let's see how it works out:</p>
|
|
<div class="simple">
|
|
<table border="0" align="center">
|
|
<tr align="right">
|
|
<th width="70">x</th>
|
|
<th width="70">y</th>
|
|
<th>y = 1.518x + 0.305</th>
|
|
<th width="70">error</th>
|
|
</tr>
|
|
<tr>
|
|
<td width="70" align="right">2</td>
|
|
<td width="70" align="right">4</td>
|
|
<td align="right">3.34</td>
|
|
<td align="right">−0.66</td>
|
|
</tr>
|
|
<tr>
|
|
<td width="70" align="right">3</td>
|
|
<td width="70" align="right">5</td>
|
|
<td align="right">4.86</td>
|
|
<td align="right">−0.14</td>
|
|
</tr>
|
|
<tr>
|
|
<td width="70" align="right">5</td>
|
|
<td width="70" align="right">7</td>
|
|
<td align="right">7.89</td>
|
|
<td align="right">0.89</td>
|
|
</tr>
|
|
<tr>
|
|
<td width="70" align="right">7</td>
|
|
<td width="70" align="right">10</td>
|
|
<td align="right">10.93</td>
|
|
<td align="right">0.93</td>
|
|
</tr>
|
|
<tr>
|
|
<td width="70" align="right">9</td>
|
|
<td width="70" align="right">15</td>
|
|
<td align="right">13.97</td>
|
|
<td align="right">−1.03</td>
|
|
</tr>
|
|
</table>
|
|
</div>
|
|
<p>Here are the (x,y) points and the line <b>y = 1.518x + 0.305</b> on a graph:</p>
|
|
<p class="center"><img src="images/least-squares-example.gif" width="235" height="330" alt="least squares example graph"></p>
|
|
<p class="center">Nice fit!</p>
|
|
<p>Sam hears the weather forecast which says "we expect 8 hours of sun tomorrow", so he uses the above equation to estimate that he will sell</p>
|
|
<p class="center large">y = 1.518 x 8 + 0.305 = 12.45 Ice Creams</p>
|
|
<p>Sam makes fresh waffle cone mixture for 14 ice creams just in case. Yum.</p>
|
|
</div>
|
|
|
|
<h2>How does it work?</h2>
|
|
<p>It works by making the total of the <b>square of the errors</b> as small as possible (that is why it is called "least squares"):</p>
|
|
<p class="center"><img src="images/least-squares2.svg" alt="least squares example graph" ><br>
|
|
The straight line minimizes the sum of squared errors</p>
|
|
<p>So, when we square each of those errors and add them all up, the total is as small as possible. </p>
|
|
<p>You can <b>imagine</b> (but not accurately) each data point connected to a straight bar by springs:</p>
|
|
<p class="center"><img src="images/least-squares-springs.svg" alt="least squares example graph" ><br>
|
|
Boing! </p>
|
|
<h2>Outliers</h2>
|
|
<p>Be careful! Least squares is sensitive to <a href="outliers.html">outliers</a>. A strange value will pull the line towards it.</p>
|
|
<h2>Use the App</h2>
|
|
<p>Have a play with the <a href="least-squares-calculator.html">Least Squares Calculator</a></p>
|
|
<h2>Not Just For Lines</h2>
|
|
<p>This <b>idea</b> can be used in many other areas, not just lines.</p>
|
|
<p class="center"><img src="images/least-squares-circle.svg" alt="least squares circle graph" ><br>
|
|
A "circle of best fit"</p>
|
|
<p>But the formulas (and the steps taken) will be very different!</p>
|
|
<p> </p>
|
|
|
|
<p> </p>
|
|
<div class="related">
|
|
<a href="scatter-xy-plots.html">Scatter (x,y) Graphs</a> <a href="../equation_of_line.html">Equation of a Line</a> <a href="least-squares-calculator.html">Least Squares Calculator</a> <a href="index.html">Data Index</a>
|
|
</div>
|
|
<!-- #EndEditable --></div>
|
|
<div id="adend" class="centerfull noprint">
|
|
<script type="text/javascript">document.write(getAdEnd());</script>
|
|
</div>
|
|
<div id="footer" class="centerfull noprint">
|
|
<script type="text/javascript">document.write(getFooter());</script>
|
|
</div>
|
|
<div id="copyrt">
|
|
Copyright © 2017 MathsIsFun.com
|
|
</div>
|
|
|
|
<script type="text/javascript">document.write(getBodyEnd());</script>
|
|
</body>
|
|
<!-- #EndTemplate -->
|
|
<!-- Mirrored from www.mathsisfun.com/data/least-squares-regression.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 29 Oct 2022 00:32:01 GMT -->
|
|
</html>
|