Skip to content

Commit

Permalink
Merge pull request #26 from TrevorFrench/regression-example
Browse files Browse the repository at this point in the history
fixing example
  • Loading branch information
TrevorFrench authored May 4, 2023
2 parents df6d95e + 8d32564 commit 2de54cf
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 37 deletions.
Binary file modified R-for-Data-Analysis.pdf
Binary file not shown.
Binary file modified docs/R-for-Data-Analysis.pdf
Binary file not shown.
4 changes: 2 additions & 2 deletions docs/p4c2-regression.html
Original file line number Diff line number Diff line change
Expand Up @@ -531,11 +531,11 @@ <h2 data-number="16.1" class="anchored" data-anchor-id="linear-regression"><span
<h2 data-number="16.2" class="anchored" data-anchor-id="multiple-regression"><span class="header-section-number">16.2</span> Multiple Regression</h2>
<p>If you had more x variables you wanted to add to your linear model, you could add them just like you would in any other math equation. Here’s an example:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="fu">lm</span>(data<span class="sc">$</span>y <span class="sc">~</span> data<span class="sc">$</span>x1 <span class="sc">+</span> data<span class="sc">$</span>x2 <span class="sc">-</span> data<span class="sc">$</span>x3 <span class="sc">*</span> data<span class="sc">$</span>x4)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="fu">lm</span>(data<span class="sc">$</span>y <span class="sc">~</span> data<span class="sc">$</span>x1 <span class="sc">+</span> data<span class="sc">$</span>x2 <span class="sc">+</span> data<span class="sc">$</span>x3 <span class="sc">+</span> data<span class="sc">$</span>x4)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Additionally, you can use the “data” parameter rather than putting the name of the dataset before every variable.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb6"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="fu">lm</span>(y <span class="sc">~</span> x1 <span class="sc">+</span> x2 <span class="sc">-</span> x3 <span class="sc">*</span> x4, <span class="at">data =</span> data)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="sourceCode cell-code" id="cb6"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="fu">lm</span>(y <span class="sc">~</span> x1 <span class="sc">+</span> x2 <span class="sc">+</span> x3 <span class="sc">+</span> x4, <span class="at">data =</span> data)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Let’s try a real example with the mtcars dataset.</p>
<div class="cell">
Expand Down
2 changes: 1 addition & 1 deletion docs/search.json
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@
"href": "p4c2-regression.html#multiple-regression",
"title": "16  Regression",
"section": "16.2 Multiple Regression",
"text": "16.2 Multiple Regression\nIf you had more x variables you wanted to add to your linear model, you could add them just like you would in any other math equation. Here’s an example:\n\nlm(data$y ~ data$x1 + data$x2 - data$x3 * data$x4)\n\nAdditionally, you can use the “data” parameter rather than putting the name of the dataset before every variable.\n\nlm(y ~ x1 + x2 - x3 * x4, data = data)\n\nLet’s try a real example with the mtcars dataset.\n\nhead(mtcars)\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nmpg\ncyl\ndisp\nhp\ndrat\nwt\nqsec\nvs\nam\ngear\ncarb\n\n\n\n\nMazda RX4\n21.0\n6\n160\n110\n3.90\n2.620\n16.46\n0\n1\n4\n4\n\n\nMazda RX4 Wag\n21.0\n6\n160\n110\n3.90\n2.875\n17.02\n0\n1\n4\n4\n\n\nDatsun 710\n22.8\n4\n108\n93\n3.85\n2.320\n18.61\n1\n1\n4\n1\n\n\nHornet 4 Drive\n21.4\n6\n258\n110\n3.08\n3.215\n19.44\n1\n0\n3\n1\n\n\nHornet Sportabout\n18.7\n8\n360\n175\n3.15\n3.440\n17.02\n0\n0\n3\n2\n\n\nValiant\n18.1\n6\n225\n105\n2.76\n3.460\n20.22\n1\n0\n3\n1\n\n\n\n\n\nNow, let’s try to predict mpg and use every other column as a variable then see what the results look like.\n\nlm <- lm(mpg ~ cyl + disp + hp + drat + wt + qsec + vs + am + gear + carb\n , data = mtcars)\nsummary(lm)\n\n\nCall:\nlm(formula = mpg ~ cyl + disp + hp + drat + wt + qsec + vs + \n am + gear + carb, data = mtcars)\n\nResiduals:\n Min 1Q Median 3Q Max \n-3.4506 -1.6044 -0.1196 1.2193 4.6271 \n\nCoefficients:\n Estimate Std. Error t value Pr(>|t|) \n(Intercept) 12.30337 18.71788 0.657 0.5181 \ncyl -0.11144 1.04502 -0.107 0.9161 \ndisp 0.01334 0.01786 0.747 0.4635 \nhp -0.02148 0.02177 -0.987 0.3350 \ndrat 0.78711 1.63537 0.481 0.6353 \nwt -3.71530 1.89441 -1.961 0.0633 .\nqsec 0.82104 0.73084 1.123 0.2739 \nvs 0.31776 2.10451 0.151 0.8814 \nam 2.52023 2.05665 1.225 0.2340 \ngear 0.65541 1.49326 0.439 0.6652 \ncarb -0.19942 0.82875 -0.241 0.8122 \n---\nSignif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1\n\nResidual standard error: 2.65 on 21 degrees of freedom\nMultiple R-squared: 0.869, Adjusted R-squared: 0.8066 \nF-statistic: 13.93 on 10 and 21 DF, p-value: 3.793e-07\n\n\nFrom here, you would likely tweak your model further based on the significance statistics we see here; however, that’s outside the scope of what we’re doing in this book. Take a look in the resources section at the end of this chapter to dive deeper into developing regression models."
"text": "16.2 Multiple Regression\nIf you had more x variables you wanted to add to your linear model, you could add them just like you would in any other math equation. Here’s an example:\n\nlm(data$y ~ data$x1 + data$x2 + data$x3 + data$x4)\n\nAdditionally, you can use the “data” parameter rather than putting the name of the dataset before every variable.\n\nlm(y ~ x1 + x2 + x3 + x4, data = data)\n\nLet’s try a real example with the mtcars dataset.\n\nhead(mtcars)\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nmpg\ncyl\ndisp\nhp\ndrat\nwt\nqsec\nvs\nam\ngear\ncarb\n\n\n\n\nMazda RX4\n21.0\n6\n160\n110\n3.90\n2.620\n16.46\n0\n1\n4\n4\n\n\nMazda RX4 Wag\n21.0\n6\n160\n110\n3.90\n2.875\n17.02\n0\n1\n4\n4\n\n\nDatsun 710\n22.8\n4\n108\n93\n3.85\n2.320\n18.61\n1\n1\n4\n1\n\n\nHornet 4 Drive\n21.4\n6\n258\n110\n3.08\n3.215\n19.44\n1\n0\n3\n1\n\n\nHornet Sportabout\n18.7\n8\n360\n175\n3.15\n3.440\n17.02\n0\n0\n3\n2\n\n\nValiant\n18.1\n6\n225\n105\n2.76\n3.460\n20.22\n1\n0\n3\n1\n\n\n\n\n\nNow, let’s try to predict mpg and use every other column as a variable then see what the results look like.\n\nlm <- lm(mpg ~ cyl + disp + hp + drat + wt + qsec + vs + am + gear + carb\n , data = mtcars)\nsummary(lm)\n\n\nCall:\nlm(formula = mpg ~ cyl + disp + hp + drat + wt + qsec + vs + \n am + gear + carb, data = mtcars)\n\nResiduals:\n Min 1Q Median 3Q Max \n-3.4506 -1.6044 -0.1196 1.2193 4.6271 \n\nCoefficients:\n Estimate Std. Error t value Pr(>|t|) \n(Intercept) 12.30337 18.71788 0.657 0.5181 \ncyl -0.11144 1.04502 -0.107 0.9161 \ndisp 0.01334 0.01786 0.747 0.4635 \nhp -0.02148 0.02177 -0.987 0.3350 \ndrat 0.78711 1.63537 0.481 0.6353 \nwt -3.71530 1.89441 -1.961 0.0633 .\nqsec 0.82104 0.73084 1.123 0.2739 \nvs 0.31776 2.10451 0.151 0.8814 \nam 2.52023 2.05665 1.225 0.2340 \ngear 0.65541 1.49326 0.439 0.6652 \ncarb -0.19942 0.82875 -0.241 0.8122 \n---\nSignif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1\n\nResidual standard error: 2.65 on 21 degrees of freedom\nMultiple R-squared: 0.869, Adjusted R-squared: 0.8066 \nF-statistic: 13.93 on 10 and 21 DF, p-value: 3.793e-07\n\n\nFrom here, you would likely tweak your model further based on the significance statistics we see here; however, that’s outside the scope of what we’re doing in this book. Take a look in the resources section at the end of this chapter to dive deeper into developing regression models."
},
{
"objectID": "p4c2-regression.html#logistic-regression",
Expand Down
64 changes: 32 additions & 32 deletions docs/sitemap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,130 +2,130 @@
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://trevorfrench.github.io/R-for-Data-Analysis/index.html</loc>
<lastmod>2023-05-03T23:11:22.972Z</lastmod>
<lastmod>2023-05-04T13:30:48.587Z</lastmod>
</url>
<url>
<loc>https://trevorfrench.github.io/R-for-Data-Analysis/p0c1-what-is-r.html</loc>
<lastmod>2023-05-03T23:11:31.358Z</lastmod>
<lastmod>2023-05-04T13:30:46.145Z</lastmod>
</url>
<url>
<loc>https://trevorfrench.github.io/R-for-Data-Analysis/p0c2-what-is-data-analysis.html</loc>
<lastmod>2023-05-03T23:11:33.841Z</lastmod>
<lastmod>2023-05-04T13:30:46.146Z</lastmod>
</url>
<url>
<loc>https://trevorfrench.github.io/R-for-Data-Analysis/p0c3-setup.html</loc>
<lastmod>2023-05-03T23:11:37.835Z</lastmod>
<lastmod>2023-05-04T13:30:46.146Z</lastmod>
</url>
<url>
<loc>https://trevorfrench.github.io/R-for-Data-Analysis/p1-fundamentals.html</loc>
<lastmod>2023-05-03T23:11:40.567Z</lastmod>
<lastmod>2023-05-04T13:30:46.147Z</lastmod>
</url>
<url>
<loc>https://trevorfrench.github.io/R-for-Data-Analysis/p1c1-r-studio.html</loc>
<lastmod>2023-05-03T23:11:42.194Z</lastmod>
<lastmod>2023-05-04T13:30:46.148Z</lastmod>
</url>
<url>
<loc>https://trevorfrench.github.io/R-for-Data-Analysis/p1c2-programming-basics.html</loc>
<lastmod>2023-05-03T23:11:44.629Z</lastmod>
<lastmod>2023-05-04T13:30:46.153Z</lastmod>
</url>
<url>
<loc>https://trevorfrench.github.io/R-for-Data-Analysis/p1c3-data-types.html</loc>
<lastmod>2023-05-03T23:11:47.156Z</lastmod>
<lastmod>2023-05-04T13:30:46.154Z</lastmod>
</url>
<url>
<loc>https://trevorfrench.github.io/R-for-Data-Analysis/p1c4-data-structures.html</loc>
<lastmod>2023-05-03T23:11:20.459Z</lastmod>
<lastmod>2023-05-04T13:30:46.156Z</lastmod>
</url>
<url>
<loc>https://trevorfrench.github.io/R-for-Data-Analysis/p1exercises.html</loc>
<lastmod>2023-05-03T23:11:20.460Z</lastmod>
<lastmod>2023-05-04T13:30:46.159Z</lastmod>
</url>
<url>
<loc>https://trevorfrench.github.io/R-for-Data-Analysis/p2-data-acquisition.html</loc>
<lastmod>2023-05-03T23:11:20.462Z</lastmod>
<lastmod>2023-05-04T13:30:46.160Z</lastmod>
</url>
<url>
<loc>https://trevorfrench.github.io/R-for-Data-Analysis/p2c1-included-datasets.html</loc>
<lastmod>2023-05-03T23:11:20.462Z</lastmod>
<lastmod>2023-05-04T13:30:46.161Z</lastmod>
</url>
<url>
<loc>https://trevorfrench.github.io/R-for-Data-Analysis/p2c2-import-from-spreadsheets.html</loc>
<lastmod>2023-05-03T23:11:20.462Z</lastmod>
<lastmod>2023-05-04T13:30:46.163Z</lastmod>
</url>
<url>
<loc>https://trevorfrench.github.io/R-for-Data-Analysis/p2c3-working-with-apis.html</loc>
<lastmod>2023-05-03T23:11:20.462Z</lastmod>
<lastmod>2023-05-04T13:30:46.164Z</lastmod>
</url>
<url>
<loc>https://trevorfrench.github.io/R-for-Data-Analysis/p2exercises.html</loc>
<lastmod>2023-05-03T23:11:20.462Z</lastmod>
<lastmod>2023-05-04T13:30:46.165Z</lastmod>
</url>
<url>
<loc>https://trevorfrench.github.io/R-for-Data-Analysis/p3-data-preparation.html</loc>
<lastmod>2023-05-03T23:11:20.462Z</lastmod>
<lastmod>2023-05-04T13:30:46.166Z</lastmod>
</url>
<url>
<loc>https://trevorfrench.github.io/R-for-Data-Analysis/p3c1-data-cleaning.html</loc>
<lastmod>2023-05-03T23:11:20.462Z</lastmod>
<lastmod>2023-05-04T13:30:46.169Z</lastmod>
</url>
<url>
<loc>https://trevorfrench.github.io/R-for-Data-Analysis/p3c2-handling-missing-data.html</loc>
<lastmod>2023-05-03T23:11:20.477Z</lastmod>
<lastmod>2023-05-04T13:30:46.172Z</lastmod>
</url>
<url>
<loc>https://trevorfrench.github.io/R-for-Data-Analysis/p3c3-outliers.html</loc>
<lastmod>2023-05-03T23:11:20.478Z</lastmod>
<lastmod>2023-05-04T13:30:46.174Z</lastmod>
</url>
<url>
<loc>https://trevorfrench.github.io/R-for-Data-Analysis/p3c4-organizing-data.html</loc>
<lastmod>2023-05-03T23:11:20.479Z</lastmod>
<lastmod>2023-05-04T13:30:46.174Z</lastmod>
</url>
<url>
<loc>https://trevorfrench.github.io/R-for-Data-Analysis/p3exercises.html</loc>
<lastmod>2023-05-03T23:11:20.481Z</lastmod>
<lastmod>2023-05-04T13:30:46.174Z</lastmod>
</url>
<url>
<loc>https://trevorfrench.github.io/R-for-Data-Analysis/p4-developing-insights.html</loc>
<lastmod>2023-05-03T23:11:20.481Z</lastmod>
<lastmod>2023-05-04T13:30:46.174Z</lastmod>
</url>
<url>
<loc>https://trevorfrench.github.io/R-for-Data-Analysis/p4c1-summary-statistics.html</loc>
<lastmod>2023-05-03T23:11:20.482Z</lastmod>
<lastmod>2023-05-04T13:30:46.174Z</lastmod>
</url>
<url>
<loc>https://trevorfrench.github.io/R-for-Data-Analysis/p4c2-regression.html</loc>
<lastmod>2023-05-03T23:11:20.483Z</lastmod>
<lastmod>2023-05-04T13:30:46.174Z</lastmod>
</url>
<url>
<loc>https://trevorfrench.github.io/R-for-Data-Analysis/p4c3-plotting.html</loc>
<lastmod>2023-05-03T23:11:20.485Z</lastmod>
<lastmod>2023-05-04T13:30:46.174Z</lastmod>
</url>
<url>
<loc>https://trevorfrench.github.io/R-for-Data-Analysis/p4exercises.html</loc>
<lastmod>2023-05-03T23:12:10.534Z</lastmod>
<lastmod>2023-05-04T13:30:46.174Z</lastmod>
</url>
<url>
<loc>https://trevorfrench.github.io/R-for-Data-Analysis/p5-reporting.html</loc>
<lastmod>2023-05-03T23:12:07.906Z</lastmod>
<lastmod>2023-05-04T13:30:46.174Z</lastmod>
</url>
<url>
<loc>https://trevorfrench.github.io/R-for-Data-Analysis/p5c1-spreadsheets.html</loc>
<lastmod>2023-05-03T23:12:05.951Z</lastmod>
<lastmod>2023-05-04T13:30:46.190Z</lastmod>
</url>
<url>
<loc>https://trevorfrench.github.io/R-for-Data-Analysis/p5c2-r-markdown.html</loc>
<lastmod>2023-05-03T23:12:00.612Z</lastmod>
<lastmod>2023-05-04T13:30:46.192Z</lastmod>
</url>
<url>
<loc>https://trevorfrench.github.io/R-for-Data-Analysis/p5c3-r-shiny.html</loc>
<lastmod>2023-05-03T23:11:57.712Z</lastmod>
<lastmod>2023-05-04T13:30:46.193Z</lastmod>
</url>
<url>
<loc>https://trevorfrench.github.io/R-for-Data-Analysis/p5exercises.html</loc>
<lastmod>2023-05-03T23:11:55.678Z</lastmod>
<lastmod>2023-05-04T13:30:46.195Z</lastmod>
</url>
<url>
<loc>https://trevorfrench.github.io/R-for-Data-Analysis/references.html</loc>
<lastmod>2023-05-03T23:11:52.267Z</lastmod>
<lastmod>2023-05-04T13:30:46.195Z</lastmod>
</url>
</urlset>
4 changes: 2 additions & 2 deletions p4c2-regression.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ If you had more x variables you wanted to add to your linear model, you could ad

```{r}
#| eval: false
lm(data$y ~ data$x1 + data$x2 - data$x3 * data$x4)
lm(data$y ~ data$x1 + data$x2 + data$x3 + data$x4)
```

Additionally, you can use the "data" parameter rather than putting the name of the dataset before every variable.

```{r}
#| eval: false
lm(y ~ x1 + x2 - x3 * x4, data = data)
lm(y ~ x1 + x2 + x3 + x4, data = data)
```

Let's try a real example with the mtcars dataset.
Expand Down

0 comments on commit 2de54cf

Please sign in to comment.