<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/linear-regression-in-the-man-who-counted/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://tomaztsql.wordpress.com/2017/03/25/linear-regression-in-the-man-who-counted/"> R – TomazTsql</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p lang="en">Recently, I got a book by Brasilian writer  <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/J%C3%BAlio_C%C3%A9sar_de_Mello_e_Souza">Júlio César de Mello e Souza</a> (published under pen name Malba Tahan), titled <a target="_blank" rel="nofollow" href="https://www.amazon.com/Man-Who-Counted-Collection-Mathematical/dp/0393351475">The Man who counted</a>. Book is a collection of mathematical stories very similar to Scheherazada’s 1001 Nights, where mathematical story-telling is the center of book.</p>
<p lang="en">                                               <img sizes="(max-width: 396px) 100vw, 396px" data-image-description="" data-orig-file="https://tomaztsql.files.wordpress.com/2017/03/2017-03-25-19_38_40-the-man-who-counted-google-search.png?w=456" data-orig-size="396,399" data-image-title="2017-03-25 19_38_40-the man who counted – Google Search" class="wp-image-4086" data-comments-opened="1" data-recalc-dims="1" data-attachment-id="4086" data-permalink="https://tomaztsql.wordpress.com/2017/03/25/linear-regression-in-the-man-who-counted/2017-03-25-19_38_40-the-man-who-counted-google-search/#main" alt="2017-03-25 19_38_40-the man who counted - Google Search" data-medium-file="https://tomaztsql.files.wordpress.com/2017/03/2017-03-25-19_38_40-the-man-who-counted-google-search.png?w=456?w=298" data-large-file="https://tomaztsql.files.wordpress.com/2017/03/2017-03-25-19_38_40-the-man-who-counted-google-search.png?w=456?w=396" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://tomaztsql.files.wordpress.com/2017/03/2017-03-25-19_38_40-the-man-who-counted-google-search.png?w=456"></p>
<p lang="en">In <a target="_blank" rel="nofollow" href="http://freebies2download.com/Files/Books/en/The%20man%20who%20counted.pdf">story 5</a>“In so many words”, Malba describes a simple algebraic problem of proportion between price for lodging offered and price of jewel sold.</p>
<pre>This man,” old Salim said pointing to the jeweler “came from Syria to sell 
precious stones in Baghdad. He promised he would pay 20 dinars for his 
lodgings if he sold all of his jewels for 100 dinars and 35 dinars if he 
sold them for 200. After several days of wandering about, he ended up selling 
all of them for 140 dinars. How much does he owe me according to our agreement?”</pre>
<p lang="en">Both, jeweler and lodge owner,  calculate the result each using percent proportion problem, both ending with wrong results, that was each to favor each:</p>
<ol>
<li>Jeweler:</li>
</ol>
<pre>200 : 35 :: 140 : x

x = (35 x 140) / 200 = <strong>24.5</strong></pre>
<p lang="en">2. Lodge Owner:</p>
<pre>100 : 20 :: 140 : x

x = (20 x 140) / 100 = <strong>28</strong></pre>
<p> </p>
<p>With two different results, both would end up in argument, so the third needed to be calculated:</p>
<pre>Sale Price        Price of Lodgings
200               35
-100             -20
-----            -------
100               15</pre>
<p> </p>
<p>So the difference between both calculations forms a proportion to calculate the new case, when Sale price for jewel is <strong>140</strong>, the price of lodging would be <strong>26</strong>.</p>
<pre>100: 15:: 40: x

x = (15 x 40) / 100 = 6</pre>
<p> </p>
<p>Mathematically speaking, problem is very interesting to be solved also using Linear Regression, since the two pair of points [200, 35] and [100, 20] form a linear prediction function and we would need to predict what would be the price of lodging, when sale price for jewel is 140.</p>
<pre>diamond &lt;- c(100, 200)
sleep   &lt;- c(20, 35)

# regression
sleep_model &lt;- lm(sleep ~ diamond)

plot(x=diamond, y=sleep)
abline(lm(sleep ~ diamond))</pre>
<p><img sizes="(max-width: 436px) 100vw, 436px" data-image-description="" data-orig-file="https://tomaztsql.files.wordpress.com/2017/03/2017-03-25-21_07_29-plot-zoom.png?w=436&amp;resize=436%2C250#038;h=250" data-orig-size="500,287" data-image-title="2017-03-25 21_07_29-Plot Zoom" data-comments-opened="1" class="wp-image-4140" data-recalc-dims="1" data-attachment-id="4140" data-permalink="https://tomaztsql.wordpress.com/2017/03/25/linear-regression-in-the-man-who-counted/2017-03-25-21_07_29-plot-zoom/#main" alt="2017-03-25 21_07_29-Plot Zoom" data-medium-file="https://tomaztsql.files.wordpress.com/2017/03/2017-03-25-21_07_29-plot-zoom.png?w=436&amp;resize=436%2C250#038;h=250?w=300" data-large-file="https://tomaztsql.files.wordpress.com/2017/03/2017-03-25-21_07_29-plot-zoom.png?w=436&amp;resize=436%2C250#038;h=250?w=500" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://tomaztsql.files.wordpress.com/2017/03/2017-03-25-21_07_29-plot-zoom.png?w=436&resize=436%2C250#038;h=250"></p>
<p>Now, we can call this a prediction, what actually Beremiz does by heart.</p>
<pre>predict_data &lt;- data.frame(diamond=140)
fit &lt;- predict(sleep_model, predict_data, interval = &quot;predict&quot;)

#new value for diamond=140
fit[1]</pre>
<p>Result is <strong>26</strong>, which is strictly algebraic and R-prediction speaking correct result.</p>
<p>In this case, linear regression does same as proportion calculation, but what strikes me is which calculation – not mathematically speaking – does make more sense? 26 or 24,5 or 28 ? And which method for calculating next price lodge should satisfy both jeweler and lodge owner.</p>
<p>Happy reading!</p>
<p> </p>
<p> </p>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/tomaztsql.wordpress.com/4080/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/tomaztsql.wordpress.com/4080/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://tomaztsql.wordpress.com/2017/03/25/linear-regression-in-the-man-who-counted/"> R – TomazTsql</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/10r5xbuE6Z0"></a></div>