<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/introduction-to-r-for-data-science-session-7-multiple-linear-regression-model-in-r-categorical-predictors-partial-and-part-correlation/"></div></div><div><a data-url="http://www.r-bloggers.com/introduction-to-r-for-data-science-session-7-multiple-linear-regression-model-in-r-categorical-predictors-partial-and-part-correlation/" data-counturl="http://www.r-bloggers.com/introduction-to-r-for-data-science-session-7-multiple-linear-regression-model-in-r-categorical-predictors-partial-and-part-correlation/" data-count="horizontal" data-text="Introduction to R for Data Science :: Session 7 [Multiple Linear Regression Model in R  + Categorical Predictors, Partial and Part Correlation]" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.exactness.net/post/145682161575"> The Exactness of Mind</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><br></p>
<p>Welcome to Introduction to R for Data Science Session 7: Multiple Regression + Dummy Coding, Partial and Part Correlations [<i>Multiple Linear Regression in R. Dummy coding: various ways to do it in R. Factors. Inspecting the multiple regression model: regression coefficients and their interpretation, confidence intervals, predictions. Introducing {lattice} plots + ggplot2. Assumptions: multicolinearity and testing it from the {car} package. Predictive models with categorical and continuous predictors. Influence plot. Partial and part (semi-partial) correlation in R.<i>]</i></i></p>
<p>The course is co-organized by <a target="_blank" href="http://www.datascience.rs/">Data Science Serbia</a> and <a target="_blank" href="http://en.startit.rs/">Startit</a>. You will find all course material (R scripts, data sets, SlideShare presentations, readings) on these pages.</p>
<figure data-orig-height="354" data-orig-width="500"><img alt="image" src="http://i2.wp.com/66.media.tumblr.com/e8ec86a7dd863cfccb64fd40e55c0e90/tumblr_inline_o8j42kdnl81qa0hyw_540.png?resize=456%2C323" data-orig-height="354" data-recalc-dims="1" data-orig-width="500"></figure>
<p>Check out the <a target="_blank" href="http://www.exactness.net/post/143651799365/introduction-to-r-for-data-science">Course Overview</a> to acess the learning material presented thus far.</p>
<p><a target="_blank" href="http://www.datascience.rs/kurs-r-za-data-science/">Data Science Serbia Course Pages [in Serbian]</a></p>
<p><a target="_blank" href="http://startit.rs/uvod-u-r-za-data-science/">Startit Course Pages [in Serbian]</a></p>
<h2><strong>Lecturers</strong></h2>
<ul>
<li><a target="_blank" href="https://rs.linkedin.com/in/kovacbranko">dipl. ing Branko Kovač</a>, Data Analyst at <a target="_blank" href="http://cube.rs/">CUBE</a>, Data Science Mentor at <a target="_blank" href="https://www.springboard.com/">Springboard</a>, Data Science Serbia</li>
<li><a target="_blank" href="http://www.exactness.net/">Goran S. Milovanović, Phd</a>, <a target="_blank" href="http://www.diplomacy.edu/">DataScientist@DiploFoundation</a>, Data Science Serbia</li>
</ul>
<h2><strong>Summary of Session 7, 09. June 2016 :: Multiple Regression + Dummy Coding, Partial and Part Correlations.</strong></h2>
<p><em>Multiple Regression + Dummy Coding, Partial and Part Correlations. <span>Multiple Linear Regression in R. Dummy coding: various ways to do it in R. Factors. Inspecting the multiple regression model: regression coefficients and their interpretation, confidence intervals, predictions. Introducing {lattice} plots + ggplot2. Assumptions: multicolinearity and testing it from the {car} package. Predictive models with categorical and continuous predictors. Influence plot. Partial and part (semi-partial) correlation in R.</span></em></p>
<ul>
<li><a target="_blank" href="http://www.exactness.net/post/145682161575#slides">Session 7 SlideShare</a></li>
<li><a target="_blank" href="http://www.exactness.net/post/145682161575#script">Session 7 R Script</a></li>
<li><a target="_blank" href="http://www.exactness.net/post/145682161575#readings">Further Readings</a></li>
</ul>
<h2><strong>Intro to R for Data Science SlideShare :: Session 7</strong></h2>
<p><iframe height="485" width="595" allowfullscreen src="http://www.slideshare.net/slideshow/embed_code/key/s9hZebkCHKL5p"> </iframe>
<div> <strong> <a title="Introduction to R for Data Science :: Session 7 [Multiple Linear Regression in R]" target="_blank" href="http://www.slideshare.net/Krule/introduction-to-r-for-data-science-session-7-multiple-linear-regression-in-r">Introduction to R for Data Science :: Session 7 [Multiple Linear Regression in R]</a> </strong> from <strong><a target="_blank" href="http://www.slideshare.net/Krule">Goran S. Milovanovic</a></strong> </div>
<p><br><br>
<h2><strong>R script :: Session 7</strong></h2>
<div>
<div>
<pre><span>########################################################</span>
<span># Introduction to R for Data Science</span>
<span># SESSION 7 :: 9 June, 2016</span>
<span># Multiple Linear Regression in R</span>
<span># Data Science Community Serbia + Startit</span>
<span># :: Goran S. Milovanović and Branko Kovač ::</span>
<span>########################################################</span>
 
<span># clear</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/rm"><span>rm</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/base/list"><span>list</span></a>=<a target="_blank" href="http://inside-r.org/r-doc/base/ls"><span>ls</span></a><span>(</span><span>)</span><span>)</span>
 
<span>#### read data</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/library"><span>library</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets"><span>datasets</span></a><span>)</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/library"><span>library</span></a><span>(</span>broom<span>)</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/library"><span>library</span></a><span>(</span><a target="_blank" href="http://inside-r.org/packages/cran/ggplot2"><span>ggplot2</span></a><span>)</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/library"><span>library</span></a><span>(</span><a target="_blank" href="http://inside-r.org/packages/cran/lattice"><span>lattice</span></a><span>)</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/library"><span>library</span></a><span>(</span><a target="_blank" href="http://inside-r.org/packages/cran/QuantPsyc"><span>QuantPsyc</span></a><span>)</span>
 
<span>#### load</span>
<a target="_blank" href="http://inside-r.org/r-doc/utils/data"><span>data</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>)</span>
<a target="_blank" href="http://inside-r.org/r-doc/utils/str"><span>str</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>)</span>
 
<span>#### simple linear regression: Sepal Length vs Petal Lenth</span>
<span># Predictor vs Criterion {ggplot2}</span>
<a target="_blank" href="http://inside-r.org/packages/cran/ggplot"><span>ggplot</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/utils/data"><span>data</span></a> = <a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>,</span>
       aes<span>(</span>x = Sepal.Length<span>,</span> y = Petal.Length<span>)</span><span>)</span> <span>+</span>
  geom_point<span>(</span>size = <span>2</span><span>,</span> colour = <span>&quot;black&quot;</span><span>)</span> <span>+</span>
  geom_point<span>(</span>size = <span>1</span><span>,</span> colour = <span>&quot;white&quot;</span><span>)</span> <span>+</span>
  geom_smooth<span>(</span>aes<span>(</span>colour = <span>&quot;black&quot;</span><span>)</span><span>,</span>
              method=<span>'lm'</span><span>)</span> <span>+</span>
  ggtitle<span>(</span><span>&quot;Sepal Length vs Petal Length&quot;</span><span>)</span> <span>+</span>
  xlab<span>(</span><span>&quot;Sepal Length&quot;</span><span>)</span> <span>+</span> ylab<span>(</span><span>&quot;Petal Length&quot;</span><span>)</span> <span>+</span>
  theme<span>(</span>legend.position = <span>&quot;none&quot;</span><span>)</span></pre>
</div>
</div>
<p><a title="Created by Pretty R at inside-R.org" target="_blank" href="http://www.inside-r.org/pretty-r">Created by Pretty R at inside-R.org</a></p>
<figure data-orig-height="354" data-orig-width="500"><img alt="image" src="http://i1.wp.com/66.media.tumblr.com/e188101b3ed3c13c278c04a7d9911d75/tumblr_inline_o8j3z8l7vC1qa0hyw_540.png?resize=456%2C323" data-orig-height="354" data-recalc-dims="1" data-orig-width="500"></figure>
<div>
<div>
<pre><span># What is wrong here?</span>
<span># let's see...</span>
reg <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/stats/lm"><span>lm</span></a><span>(</span>Petal.Length <span>~</span> Sepal.Length<span>,</span> <a target="_blank" href="http://inside-r.org/r-doc/utils/data"><span>data</span></a>=<a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>)</span> 
<a target="_blank" href="http://inside-r.org/r-doc/base/summary"><span>summary</span></a><span>(</span>reg<span>)</span>
<span># Hm, everything seems fine to me...</span>
 
<span># And now for something completelly different (but in R)...</span>
 
<span>#### Problems with linear regression in iris</span>
<span># Predictor vs Criterion {ggplot2} - group separation</span>
<a target="_blank" href="http://inside-r.org/packages/cran/ggplot"><span>ggplot</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/utils/data"><span>data</span></a> = <a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>,</span> 
       aes<span>(</span>x = Sepal.Length<span>,</span>
           y = Petal.Length<span>,</span>
           color = Species<span>)</span><span>)</span> <span>+</span> 
  geom_point<span>(</span>size = <span>2</span><span>)</span> <span>+</span>
  ggtitle<span>(</span><span>&quot;Sepal Length vs Petal Length&quot;</span><span>)</span> <span>+</span>
  xlab<span>(</span><span>&quot;Sepal Length&quot;</span><span>)</span> <span>+</span> ylab<span>(</span><span>&quot;Petal Length&quot;</span><span>)</span></pre>
</div>
</div>
<p><a title="Created by Pretty R at inside-R.org" target="_blank" href="http://www.inside-r.org/pretty-r">Created by Pretty R at inside-R.org</a></p>
<figure data-orig-height="354" data-orig-width="500"><img alt="image" src="http://i0.wp.com/67.media.tumblr.com/da849bf3a41e90cfd7f525cceeee3e2e/tumblr_inline_o8j3zshAGj1qa0hyw_540.png?resize=456%2C323" data-orig-height="354" data-recalc-dims="1" data-orig-width="500"></figure>
<div>
<div>
<pre><span># Predictor vs Criterion {ggplot2} - separate regression lines</span>
<a target="_blank" href="http://inside-r.org/packages/cran/ggplot"><span>ggplot</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/utils/data"><span>data</span></a> = <a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>,</span> 
       aes<span>(</span>x = Sepal.Length<span>,</span>
           y = Petal.Length<span>,</span>
           colour=Species<span>)</span><span>)</span> <span>+</span> 
  geom_smooth<span>(</span>method=<a target="_blank" href="http://inside-r.org/r-doc/stats/lm"><span>lm</span></a><span>)</span> <span>+</span> 
  geom_point<span>(</span>size = <span>2</span><span>)</span> <span>+</span>
  ggtitle<span>(</span><span>&quot;Sepal Length vs Petal Length&quot;</span><span>)</span> <span>+</span>
  xlab<span>(</span><span>&quot;Sepal Length&quot;</span><span>)</span> <span>+</span> ylab<span>(</span><span>&quot;Petal Length&quot;</span><span>)</span></pre>
</div>
</div>
<p><a title="Created by Pretty R at inside-R.org" target="_blank" href="http://www.inside-r.org/pretty-r">Created by Pretty R at inside-R.org</a></p>
<figure data-orig-height="354" data-orig-width="500"><img alt="image" src="http://i0.wp.com/67.media.tumblr.com/e8ec86a7dd863cfccb64fd40e55c0e90/tumblr_inline_o8j406qB4V1qa0hyw_540.png?resize=456%2C323" data-orig-height="354" data-recalc-dims="1" data-orig-width="500"></figure>
<div>
<div>
<pre><span>### Ooops...</span>
<span>### overview and considerations</span>
<a target="_blank" href="http://inside-r.org/r-doc/graphics/plot"><span>plot</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>[</span><span>,</span><a target="_blank" href="http://inside-r.org/r-doc/base/c"><span>c</span></a><span>(</span><span>1</span><span>,</span><span>3</span><span>,</span><span>5</span><span>)</span><span>]</span><span>,</span>
     main = <span>&quot;Inspect: Sepal vs. Petal Length <span>\n</span>following the discovery of the Species...&quot;</span><span>,</span>
     cex.main = <span>.75</span><span>,</span>
     cex = <span>.6</span><span>)</span></pre>
</div>
</div>
<p><a title="Created by Pretty R at inside-R.org" target="_blank" href="http://www.inside-r.org/pretty-r">Created by Pretty R at inside-R.org</a></p>
<figure data-orig-height="387" data-orig-width="500"><img alt="image" src="http://i0.wp.com/67.media.tumblr.com/9dfbe396532dd8839761e8010f5fd454/tumblr_inline_o8j40f7whY1qa0hyw_540.png?resize=456%2C353" data-orig-height="387" data-recalc-dims="1" data-orig-width="500"></figure>
<div>
<div>
<pre><span>### better... {lattice}</span>
<a target="_blank" href="http://inside-r.org/r-doc/lattice/xyplot"><span>xyplot</span></a><span>(</span>Petal.Length <span>~</span> Sepal.Length <span>|</span> Species<span>,</span> <span># {latice} xyplot</span>
       <a target="_blank" href="http://inside-r.org/r-doc/utils/data"><span>data</span></a> = <a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>,</span>
       xlab = <span>&quot;Sepal Length&quot;</span><span>,</span> ylab = <span>&quot;Petal Length&quot;</span>
       <span>)</span></pre>
</div>
</div>
<p><a title="Created by Pretty R at inside-R.org" target="_blank" href="http://www.inside-r.org/pretty-r">Created by Pretty R at inside-R.org</a></p>
<figure data-orig-height="387" data-orig-width="500"><img alt="image" src="http://i0.wp.com/67.media.tumblr.com/976ad52a7bcefce527bb1cbf01a2686e/tumblr_inline_o8j40mimgI1qa0hyw_540.png?resize=456%2C353" data-orig-height="387" data-recalc-dims="1" data-orig-width="500"></figure>
<div>
<div>
<pre><span>### Petal.Length and Sepal.Lengt: EDA and distributions</span>
<a target="_blank" href="http://inside-r.org/r-doc/graphics/par"><span>par</span></a><span>(</span>mfcol = <a target="_blank" href="http://inside-r.org/r-doc/base/c"><span>c</span></a><span>(</span><span>2</span><span>,</span><span>2</span><span>)</span><span>)</span>
<span># boxplot Petal.Length</span>
<a target="_blank" href="http://inside-r.org/r-doc/graphics/boxplot"><span>boxplot</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Petal.Length<span>,</span>
        horizontal = <span>TRUE</span><span>,</span> 
        xlab=<span>&quot;Petal Length&quot;</span><span>)</span>
<span># histogram: Petal.Length</span>
<a target="_blank" href="http://inside-r.org/r-doc/graphics/hist"><span>hist</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Petal.Length<span>,</span> 
     main=<span>&quot;&quot;</span><span>,</span> 
     xlab=<span>&quot;Petal Length&quot;</span><span>,</span> 
     <a target="_blank" href="http://inside-r.org/packages/cran/prob"><span>prob</span></a>=T<span>)</span>
<a target="_blank" href="http://inside-r.org/r-doc/graphics/lines"><span>lines</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/stats/density"><span>density</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Petal.Length<span>)</span><span>,</span>
      lty=<span>&quot;dashed&quot;</span><span>,</span> 
      lwd=<span>2.5</span><span>,</span> 
      <a target="_blank" href="http://inside-r.org/r-doc/base/col"><span>col</span></a>=<span>&quot;red&quot;</span><span>)</span>
<span># boxplot Sepal.Length</span>
<a target="_blank" href="http://inside-r.org/r-doc/graphics/boxplot"><span>boxplot</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Sepal.Length<span>,</span>
        horizontal = <span>TRUE</span><span>,</span> 
        xlab=<span>&quot;Sepal Length&quot;</span><span>)</span>
<span># histogram: Sepal.Length</span>
<a target="_blank" href="http://inside-r.org/r-doc/graphics/hist"><span>hist</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Sepal.Length<span>,</span> 
     main=<span>&quot;&quot;</span><span>,</span> 
     xlab=<span>&quot;Sepal Length&quot;</span><span>,</span> 
     <a target="_blank" href="http://inside-r.org/packages/cran/prob"><span>prob</span></a>=T<span>)</span>
<a target="_blank" href="http://inside-r.org/r-doc/graphics/lines"><span>lines</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/stats/density"><span>density</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Sepal.Length<span>)</span><span>,</span>
      lty=<span>&quot;dashed&quot;</span><span>,</span> 
      lwd=<span>2.5</span><span>,</span> 
      <a target="_blank" href="http://inside-r.org/r-doc/base/col"><span>col</span></a>=<span>&quot;blue&quot;</span><span>)</span></pre>
</div>
</div>
<p><a title="Created by Pretty R at inside-R.org" target="_blank" href="http://www.inside-r.org/pretty-r">Created by Pretty R at inside-R.org</a></p>
<figure data-orig-height="387" data-orig-width="500"><img alt="image" src="http://i2.wp.com/66.media.tumblr.com/568dad1373f7c3a04a0e628921713939/tumblr_inline_o8j40slXoJ1qa0hyw_540.png?resize=456%2C353" data-orig-height="387" data-recalc-dims="1" data-orig-width="500"></figure>
<div>
<div>
<pre><span># Petal Length and Sepal Length: Conditional Densities</span>
<a target="_blank" href="http://inside-r.org/r-doc/lattice/densityplot"><span>densityplot</span></a><span>(</span><span>~</span> Petal.Length <span>|</span> Species<span>,</span> <span># {latice} xyplot</span>
       <a target="_blank" href="http://inside-r.org/r-doc/utils/data"><span>data</span></a> = <a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>,</span>
       plot.points=<span>FALSE</span><span>,</span>
       xlab = <span>&quot;Petal Length&quot;</span><span>,</span> ylab = <span>&quot;Density&quot;</span><span>,</span>
       main = <span>&quot;P(Petal Length|Species)&quot;</span><span>,</span>
       col.line = <span>'red'</span>
<span>)</span></pre>
</div>
</div>
<p><a title="Created by Pretty R at inside-R.org" target="_blank" href="http://www.inside-r.org/pretty-r">Created by Pretty R at inside-R.org</a></p>
<figure data-orig-height="387" data-orig-width="500"><img alt="image" src="http://i2.wp.com/66.media.tumblr.com/cec44864bb6b64f04ede0c6a44d1555f/tumblr_inline_o8j40xaDTy1qa0hyw_540.png?resize=456%2C353" data-orig-height="387" data-recalc-dims="1" data-orig-width="500"></figure>
<div>
<div>
<pre><a target="_blank" href="http://inside-r.org/r-doc/lattice/densityplot"><span>densityplot</span></a><span>(</span><span>~</span> Sepal.Length <span>|</span> Species<span>,</span> <span># {latice} xyplot</span>
            <a target="_blank" href="http://inside-r.org/r-doc/utils/data"><span>data</span></a> = <a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>,</span>
            plot.points=<span>FALSE</span><span>,</span>
            xlab = <span>&quot;Sepal Length&quot;</span><span>,</span> ylab = <span>&quot;Density&quot;</span><span>,</span>
            main = <span>&quot;P(Sepal Length|Species)&quot;</span><span>,</span>
            col.line = <span>'blue'</span>
<span>)</span></pre>
</div>
</div>
<p><a title="Created by Pretty R at inside-R.org" target="_blank" href="http://www.inside-r.org/pretty-r">Created by Pretty R at inside-R.org</a></p>
<figure data-orig-height="387" data-orig-width="500"><img alt="image" src="http://i0.wp.com/66.media.tumblr.com/e09c24d5dba3755754711f978496fdd7/tumblr_inline_o8j41j27uK1qa0hyw_540.png?resize=456%2C353" data-orig-height="387" data-recalc-dims="1" data-orig-width="500"></figure>
<div>
<div>
<pre><span># Linear regression in subgroups</span>
species <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/unique"><span>unique</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Species<span>)</span>
w1 <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/which"><span>which</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Species <span>==</span> species<span>[</span><span>1</span><span>]</span><span>)</span> <span># setosa</span>
reg <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/stats/lm"><span>lm</span></a><span>(</span>Petal.Length <span>~</span> Sepal.Length<span>,</span> <a target="_blank" href="http://inside-r.org/r-doc/utils/data"><span>data</span></a>=<a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>[</span>w1<span>,</span><span>]</span><span>)</span> 
tidy<span>(</span>reg<span>)</span>
w2 <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/which"><span>which</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Species <span>==</span> species<span>[</span><span>2</span><span>]</span><span>)</span> <span># versicolor</span>
reg <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/stats/lm"><span>lm</span></a><span>(</span>Petal.Length <span>~</span> Sepal.Length<span>,</span> <a target="_blank" href="http://inside-r.org/r-doc/utils/data"><span>data</span></a>=<a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>[</span>w2<span>,</span><span>]</span><span>)</span> 
tidy<span>(</span>reg<span>)</span>
w3 <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/which"><span>which</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Species <span>==</span> species<span>[</span><span>3</span><span>]</span><span>)</span> <span># virginica</span>
reg <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/stats/lm"><span>lm</span></a><span>(</span>Petal.Length <span>~</span> Sepal.Length<span>,</span> <a target="_blank" href="http://inside-r.org/r-doc/utils/data"><span>data</span></a>=<a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>[</span>w3<span>,</span><span>]</span><span>)</span> 
tidy<span>(</span>reg<span>)</span>
 
<span>#### Dummy Coding: Species in the iris dataset</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/is.factor"><span>is.factor</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Species<span>)</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/levels"><span>levels</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Species<span>)</span>
reg <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/stats/lm"><span>lm</span></a><span>(</span>Petal.Length <span>~</span> Species<span>,</span> <a target="_blank" href="http://inside-r.org/r-doc/utils/data"><span>data</span></a>=<a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>)</span> 
tidy<span>(</span>reg<span>)</span>
glance<span>(</span>reg<span>)</span>
<span># Never forget what the regression coefficient for a dummy variable means:</span>
<span># It tells us about the effect of moving from the baseline towards the respective reference level!</span>
<span># Here: baseline = setosa (cmp. levels(iris$Species) vs. the output of tidy(reg))</span>
<span># NOTE: watch for the order of levels!</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/levels"><span>levels</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Species<span>)</span> <span># Levels: setosa versicolor virginica</span>
<a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Species <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/factor"><span>factor</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Species<span>,</span> 
                       <a target="_blank" href="http://inside-r.org/r-doc/base/levels"><span>levels</span></a> = <a target="_blank" href="http://inside-r.org/r-doc/base/c"><span>c</span></a><span>(</span><span>&quot;versicolor&quot;</span><span>,</span> 
                                  <span>&quot;virginica&quot;</span><span>,</span>
                                  <span>&quot;setosa&quot;</span><span>)</span><span>)</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/levels"><span>levels</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Species<span>)</span>
<span># baseline is now: versicolor</span>
reg <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/stats/lm"><span>lm</span></a><span>(</span>Petal.Length <span>~</span> Species<span>,</span> <a target="_blank" href="http://inside-r.org/r-doc/utils/data"><span>data</span></a>=<a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>)</span> 
tidy<span>(</span>reg<span>)</span> <span># The regression coefficents (!): figure out what has happened!</span>
 
<span>### another way to do dummy coding</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/rm"><span>rm</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>)</span><span>;</span> <a target="_blank" href="http://inside-r.org/r-doc/utils/data"><span>data</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>)</span> <span># ...just to fix the order of Species back to default</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/levels"><span>levels</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Species<span>)</span>
<a target="_blank" href="http://inside-r.org/r-doc/stats/contrasts"><span>contrasts</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Species<span>)</span> = <a target="_blank" href="http://inside-r.org/r-doc/stats/contr.treatment"><span>contr.treatment</span></a><span>(</span><span>3</span><span>,</span> <a target="_blank" href="http://inside-r.org/r-doc/base"><span>base</span></a> = <span>1</span><span>)</span>
<a target="_blank" href="http://inside-r.org/r-doc/stats/contrasts"><span>contrasts</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Species<span>)</span> <span># this probably what you remember from your stats class...</span>
<a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Species <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/factor"><span>factor</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Species<span>,</span> 
                       <a target="_blank" href="http://inside-r.org/r-doc/base/levels"><span>levels</span></a> = <a target="_blank" href="http://inside-r.org/r-doc/base/c"><span>c</span></a> <span>(</span><span>&quot;virginica&quot;</span><span>,</span><span>&quot;versicolor&quot;</span><span>,</span><span>&quot;setosa&quot;</span><span>)</span><span>)</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/levels"><span>levels</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Species<span>)</span>
<a target="_blank" href="http://inside-r.org/r-doc/stats/contrasts"><span>contrasts</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Species<span>)</span> = <a target="_blank" href="http://inside-r.org/r-doc/stats/contr.treatment"><span>contr.treatment</span></a><span>(</span><span>3</span><span>,</span> <a target="_blank" href="http://inside-r.org/r-doc/base"><span>base</span></a> = <span>1</span><span>)</span>
<span># baseline is now: virginica</span>
<a target="_blank" href="http://inside-r.org/r-doc/stats/contrasts"><span>contrasts</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Species<span>)</span> <span># consider carefully what you need to do</span>
 
<span>### Petal.Length ~ Species (Dummy Coding) + Sepal.Length </span>
<a target="_blank" href="http://inside-r.org/r-doc/base/rm"><span>rm</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>)</span><span>;</span> <a target="_blank" href="http://inside-r.org/r-doc/utils/data"><span>data</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>)</span> <span># ...just to fix the order of Species back to default</span>
reg <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/stats/lm"><span>lm</span></a><span>(</span>Petal.Length <span>~</span> Species <span>+</span> Sepal.Length<span>,</span> <a target="_blank" href="http://inside-r.org/r-doc/utils/data"><span>data</span></a>=<a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>)</span>
<span># BTW: since is.factor(iris$Species)==T, R does the dummy coding in lm() for you</span>
regSum <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/summary"><span>summary</span></a><span>(</span>reg<span>)</span>
regSum<span>$</span>r.squared
regSum<span>$</span>coefficients
<span># compare w. Simple Linear Regression</span>
reg <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/stats/lm"><span>lm</span></a><span>(</span>Petal.Length <span>~</span> Sepal.Length<span>,</span> <a target="_blank" href="http://inside-r.org/r-doc/utils/data"><span>data</span></a>=<a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>)</span> 
regSum <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/summary"><span>summary</span></a><span>(</span>reg<span>)</span>
regSum<span>$</span>r.squared
regSum<span>$</span>coefficients
 
<span>### Comparing nested models</span>
reg1 <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/stats/lm"><span>lm</span></a><span>(</span>Petal.Length <span>~</span> Sepal.Length<span>,</span> <a target="_blank" href="http://inside-r.org/r-doc/utils/data"><span>data</span></a>=<a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>)</span>
reg2 <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/stats/lm"><span>lm</span></a><span>(</span>Petal.Length <span>~</span> Species <span>+</span> Sepal.Length<span>,</span> <a target="_blank" href="http://inside-r.org/r-doc/utils/data"><span>data</span></a>=<a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>)</span> <span># reg1 is nested under reg2</span>
<span># terminology: reg2 is a &quot;full model&quot;</span>
<span># this terminology will be used quite often in Logistic Regression</span>
 
<span># NOTE: Nested models</span>
<span># There is a set of coefficients for the nested model (reg1) such that it</span>
<span># can be expressed in terms of the full model (reg2); in our case it is simple </span>
<span># HOME: - figure it out.</span>
 
<a target="_blank" href="http://inside-r.org/r-doc/stats/anova"><span>anova</span></a><span>(</span>reg1<span>,</span> reg2<span>)</span> <span># partial F-test; Species certainly has an effect beyond Sepal.Length</span>
<span># NOTE: for partial F-test, see:</span>
<span># <a target="_blank" href="http://pages.stern.nyu.edu/~gsimon/B902301Page/CLASS02_24FEB10/PartialFtest.pdf">http://pages.stern.nyu.edu/~gsimon/B902301Page/CLASS02_24FEB10/PartialFtest.pdf</a></span>
 
<span># Influence Plot</span>
regFrame <span>&lt;-</span> augment<span>(</span>reg2<span>)</span>
<span>## Influence plot</span>
<span># influnce data</span>
infReg <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/as.data.frame"><span>as.data.frame</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/stats/influence.measures"><span>influence.measures</span></a><span>(</span>reg<span>)</span><span>$</span>infmat<span>)</span>
<span># data.frame for ggplot2</span>
plotFrame <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/data.frame"><span>data.frame</span></a><span>(</span>residual = regFrame<span>$</span>.std.resid<span>,</span>
                        leverage = regFrame<span>$</span>.hat<span>,</span>
                        cookD = regFrame<span>$</span>.cooksd<span>)</span>
 
<a target="_blank" href="http://inside-r.org/packages/cran/ggplot"><span>ggplot</span></a><span>(</span>plotFrame<span>,</span>
       aes<span>(</span>y = residual<span>,</span>
           x = leverage<span>)</span><span>)</span> <span>+</span>
  geom_point<span>(</span>size = plotFrame<span>$</span>cookD<span>*</span><span>100</span><span>,</span> <a target="_blank" href="http://inside-r.org/packages/cran/shape"><span>shape</span></a> = <span>1</span><span>)</span> <span>+</span>
  ggtitle<span>(</span><span>&quot;Influence Plot<span>\n</span>Size of the circle corresponds to Cook's distance&quot;</span><span>)</span> <span>+</span>
  theme<span>(</span>plot.title = element_text<span>(</span>size=<span>8</span><span>,</span> face=<span>&quot;bold&quot;</span><span>)</span><span>)</span> <span>+</span>
  ylab<span>(</span><span>&quot;Standardized Residual&quot;</span><span>)</span> <span>+</span> xlab<span>(</span><span>&quot;Leverage&quot;</span><span>)</span></pre>
</div>
</div>
<p><a title="Created by Pretty R at inside-R.org" target="_blank" href="http://www.inside-r.org/pretty-r">Created by Pretty R at inside-R.org</a></p>
<figure data-orig-height="387" data-orig-width="500"><img alt="image" src="http://i1.wp.com/67.media.tumblr.com/91345c0c2541bffde5186bcd3fe4d58b/tumblr_inline_o8j41ukUaY1qa0hyw_540.png?resize=456%2C353" data-orig-height="387" data-recalc-dims="1" data-orig-width="500"></figure>
<div>
<div>
<pre><span>#### Multiple Regression - by the book</span>
<span># Following: <a target="_blank" href="http://www.r-tutor.com/elementary-statistics/multiple-linear-regression">http://www.r-tutor.com/elementary-statistics/multiple-linear-regression</a></span>
<span># (that's from your reading list, to remind you...)</span>
<a target="_blank" href="http://inside-r.org/r-doc/utils/data"><span>data</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/stackloss"><span>stackloss</span></a><span>)</span>
<a target="_blank" href="http://inside-r.org/r-doc/utils/str"><span>str</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/stackloss"><span>stackloss</span></a><span>)</span>
<span># Data set description</span>
<span># URL: <a target="_blank" href="https://stat.ethz.ch/R-manual/R-devel/library/datasets/html/stackloss.html">https://stat.ethz.ch/R-manual/R-devel/library/datasets/html/stackloss.html</a></span>
<span># Air Flow represents the rate of operation of the plant. </span>
<span># Water Temp is the temperature of cooling water circulated through coils in the absorption tower. </span>
<span># Acid Conc. is the concentration of the acid circulating.</span>
<span># stack.loss (the dependent variable) is 10 times the percentage of the ingoing ammonia to </span>
<span># the plant that escapes from the absorption column unabsorbed;</span>
<span># that is, an (inverse) measure of the over-all efficiency of the plant.</span>
stacklossModel = <a target="_blank" href="http://inside-r.org/r-doc/stats/lm"><span>lm</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/stack.loss"><span>stack.loss</span></a> <span>~</span> Air.Flow <span>+</span> Water.Temp <span>+</span> Acid.Conc.<span>,</span> 
                    <a target="_blank" href="http://inside-r.org/r-doc/utils/data"><span>data</span></a>=<a target="_blank" href="http://inside-r.org/r-doc/datasets/stackloss"><span>stackloss</span></a><span>)</span>
 
<span># let's see:</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/summary"><span>summary</span></a><span>(</span>stacklossModel<span>)</span>
glance<span>(</span>stacklossModel<span>)</span> <span># {broom}</span>
tidy<span>(</span>stacklossModel<span>)</span> <span># {broom}</span>
 
<span># predict new data</span>
obs = <a target="_blank" href="http://inside-r.org/r-doc/base/data.frame"><span>data.frame</span></a><span>(</span>Air.Flow=<span>72</span><span>,</span> Water.Temp=<span>20</span><span>,</span> Acid.Conc.=<span>85</span><span>)</span>
<a target="_blank" href="http://inside-r.org/r-doc/stats/predict"><span>predict</span></a><span>(</span>stacklossModel<span>,</span> obs<span>)</span>
 
<span># confidence intervals</span>
<a target="_blank" href="http://inside-r.org/r-doc/stats/confint"><span>confint</span></a><span>(</span>stacklossModel<span>,</span> level=<span>.95</span><span>)</span> <span># 95% CI</span>
<a target="_blank" href="http://inside-r.org/r-doc/stats/confint"><span>confint</span></a><span>(</span>stacklossModel<span>,</span> level=<span>.99</span><span>)</span> <span># 99% CI</span>
<span># 95% CI for Acid.Conc. only</span>
<a target="_blank" href="http://inside-r.org/r-doc/stats/confint"><span>confint</span></a><span>(</span>stacklossModel<span>,</span> <span>&quot;Acid.Conc.&quot;</span><span>,</span> level=<span>.95</span><span>)</span>
 
<span># default regression plots in R</span>
<a target="_blank" href="http://inside-r.org/r-doc/graphics/plot"><span>plot</span></a><span>(</span>stacklossModel<span>)</span></pre>
</div>
</div>
<p><a title="Created by Pretty R at inside-R.org" target="_blank" href="http://www.inside-r.org/pretty-r">Created by Pretty R at inside-R.org</a></p>
<figure data-orig-height="387" data-orig-width="500"><img alt="image" src="http://i0.wp.com/65.media.tumblr.com/07e15507385a4120caa11b35b0ed10b1/tumblr_inline_o8j4259ErA1qa0hyw_540.png?resize=456%2C353" data-orig-height="387" data-recalc-dims="1" data-orig-width="500"></figure>
<div>
<div>
<pre><span># multicolinearity</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/library"><span>library</span></a><span>(</span><a target="_blank" href="http://inside-r.org/packages/cran/car"><span>car</span></a><span>)</span> <span># John Fox's car package</span>
<a target="_blank" href="http://inside-r.org/packages/cran/VIF"><span>VIF</span></a> <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/packages/cran/VIF"><span>vif</span></a><span>(</span>stacklossModel<span>)</span>
<a target="_blank" href="http://inside-r.org/packages/cran/VIF"><span>VIF</span></a>
<a target="_blank" href="http://inside-r.org/r-doc/base/sqrt"><span>sqrt</span></a><span>(</span><a target="_blank" href="http://inside-r.org/packages/cran/VIF"><span>VIF</span></a><span>)</span>
<span># Variance Inflation Factor (VIF)</span>
<span># The increase in the ***variance*** of an regression ceoff. due to colinearity</span>
<span># NOTE: sqrt(VIF) = how much larger the ***SE*** of a reg.coeff. vs. what it would be</span>
<span># if there were no correlations with the other predictors in the model</span>
<span># NOTE: lower_bound(VIF) = 1; no upper bound; VIF > 2 --> (Concerned == TRUE)</span>
<a target="_blank" href="http://inside-r.org/packages/cran/tolerance"><span>Tolerance</span></a> <span>&lt;-</span> <span>1</span><span>/</span>VIF <span># obviously, tolerance and VIF are redundant</span>
<a target="_blank" href="http://inside-r.org/packages/cran/tolerance"><span>Tolerance</span></a>
<span># NOTE: you can inspect multicolinearity in the multiple regression mode</span>
<span># by conducting a Principal Component Analysis over the predictors;</span>
<span># when the time is right.</span>
 
<span>#### R for partial and part (semi-partial) correlations</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/library"><span>library</span></a><span>(</span>ppcor<span>)</span> <span># a good one; there are many ways to do this in R</span>
 
<span>#### partial correlation in R</span>
dataSet <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a>
<a target="_blank" href="http://inside-r.org/r-doc/utils/str"><span>str</span></a><span>(</span>dataSet<span>)</span>
dataSet<span>$</span>Species <span>&lt;-</span> <span>NULL</span>
irisPCor <span>&lt;-</span> pcor<span>(</span>dataSet<span>,</span> method=<span>&quot;pearson&quot;</span><span>)</span>
irisPCor<span>$</span>estimate <span># partial correlations</span>
irisPCor<span>$</span>p.value <span># results of significance tests</span>
irisPCor<span>$</span>statistic <span># t-test on n-2-k degrees of freedom ; k = num. of variables conditioned</span>
<span># partial correlation between x and y while controlling for z</span>
partialCor <span>&lt;-</span> pcor.test<span>(</span>dataSet<span>$</span>Sepal.Length<span>,</span> dataSet<span>$</span>Petal.Length<span>,</span>
                     dataSet<span>$</span>Sepal.Width<span>,</span>
                     method = <span>&quot;pearson&quot;</span><span>)</span>
partialCor<span>$</span>estimate
partialCor<span>$</span>p.value
partialCor<span>$</span>statistic
 
<span># NOTE:</span>
<span># Formally, the partial correlation between X and Y given a set of n </span>
<span># controlling variables Z = {Z1, Z2, ..., Zn}, written ρXY·Z, is the </span>
<span># correlation between the residuals RX and RY resulting from the </span>
<span># linear regression of X with Z and of Y with Z, respectively. </span>
<span># The first-order partial correlation (i.e. when n=1) is the difference </span>
<span># between a correlation and the product of the removable correlations </span>
<span># divided by the product of the coefficients of alienation of the </span>
<span># removable correlations. [source: <a target="_blank" href="https://en.wikipedia.org/wiki/Partial_correlation%5D">https://en.wikipedia.org/wiki/Partial_correlation]</a></span>
<span># NOTE: coefficient of alienation = 1 - R2 (R2 = &quot;r-squared&quot;)</span>
<span># coefficient of alienation = the proportion of variance &quot;unaccounted for&quot;</span>
 
<span>#### semi-partial correlation in R</span>
<span># NOTE: ... Semi-partial correlation is the correlation of two variables </span>
<span># with variation from a third or more other variables removed only </span>
<span># from the ***second variable***</span>
<span># NOTE: The first variable &lt;- rows, the second variable &lt;-columns</span>
<span># cf. ppcor: An R Package for a Fast Calculation to Semi-partial Correlation Coefficients (2015)</span>
<span># Seongho Kim, Biostatistics Core, Karmanos Cancer Institute, Wayne State University</span>
<span># URL: <a target="_blank" href="http://www.ncbi.nlm.nih.gov/pmc/articles/PMC4681537/">http://www.ncbi.nlm.nih.gov/pmc/articles/PMC4681537/</a></span>
irisSPCor <span>&lt;-</span> spcor<span>(</span>dataSet<span>,</span> method = <span>&quot;pearson&quot;</span><span>)</span>
irisSPCor<span>$</span>estimate
irisSPCor<span>$</span>p.value
irisSPCor<span>$</span>statistic
 
<span># NOTE: ... Semi-partial correlation is the correlation of two variables </span>
<span># with variation from a third or more other variables removed only </span>
<span># from the ***second variable***</span>
partCor <span>&lt;-</span> spcor.test<span>(</span>dataSet<span>$</span>Sepal.Length<span>,</span> dataSet<span>$</span>Petal.Length<span>,</span>
                       dataSet<span>$</span>Sepal.Width<span>,</span>
                       method = <span>&quot;pearson&quot;</span><span>)</span>
<span># NOTE: this is a correlation of dataSet$Sepal.Length w. dataSet$Petal.Length</span>
<span># when the variance of dataSet$Petal.Length (2nd variable) due to dataSet$Sepal.Width</span>
<span># is removed!</span>
partCor<span>$</span>estimate
partCor<span>$</span>p.value
partCor<span>$</span>statistic
 
<span># NOTE: In multiple regression, this is the semi-partial (or part) correlation</span>
<span># that you need to inspect:</span>
<span># assume a model with X1, X2, X3 as predictors, and Y as a criterion</span>
<span># You need a semi-partial of X1 and Y following the removal of X2 and X3 from Y</span>
<span># It goes like this: in Step 1, you perform a multiple regression Y ~ X2 + X3;</span>
<span># In Step 2, you take the residuals of Y, call them RY; in Step 3, you regress (correlate)</span>
<span># RY ~ X1: the correlation coefficient that you get from Step 3 is the part correlation</span>
<span># that you're looking for.</span>
 
<span># Give a thought to the following discussion on categorical predictors:</span>
<span># <a target="_blank" href="http://stats.stackexchange.com/questions/133203/partial-correlation-and-multiple-regression-controlling-for-categorical-variable">http://stats.stackexchange.com/questions/133203/partial-correlation-and-multiple-regression-controlling-for-categorical-variable</a></span>
<span># What's your take on this?</span></pre>
</div>
</div>
<p><a title="Created by Pretty R at inside-R.org" target="_blank" href="http://www.inside-r.org/pretty-r">Created by Pretty R at inside-R.org</a></p>
<p><br></p>
<h2><strong>Readings :: Session 8: Logistic Regression [16. June, 2016, @Startit.rs, 19h CET]</strong></h2>
<ul>
<li>To be announced. In the meantime: a beatiful, concise <a target="_blank" href="https://en.wikipedia.org/wiki/Logistic_regression">theoretical background</a> is available from Wikipedia.
</li>
</ul>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/introduction-to-r-for-data-science-session-7-multiple-linear-regression-model-in-r-categorical-predictors-partial-and-part-correlation/"></div></div><div><a data-url="http://www.r-bloggers.com/introduction-to-r-for-data-science-session-7-multiple-linear-regression-model-in-r-categorical-predictors-partial-and-part-correlation/" data-counturl="http://www.r-bloggers.com/introduction-to-r-for-data-science-session-7-multiple-linear-regression-model-in-r-categorical-predictors-partial-and-part-correlation/" data-count="vertical" data-text="Introduction to R for Data Science :: Session 7 [Multiple Linear Regression Model in R  + Categorical Predictors, Partial and Part Correlation]" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.exactness.net/post/145682161575"> The Exactness of Mind</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://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="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/X0dElanUtRY"></a></div>