Skip to content

Commit

Permalink
new post
Browse files Browse the repository at this point in the history
  • Loading branch information
spsanderson committed Nov 15, 2023
1 parent 2f4e71c commit 7677480
Show file tree
Hide file tree
Showing 12 changed files with 1,191 additions and 662 deletions.
16 changes: 16 additions & 0 deletions _freeze/posts/2023-11-15/index/execute-results/html.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"hash": "86e1e9e2def2f85730e4ffc2e24f4f13",
"result": {
"markdown": "---\ntitle: \"How to Perform Multiple Linear Regression in R\"\nauthor: \"Steven P. Sanderson II, MPH\"\ndate: \"2023-11-15\"\ncategories: [rtip]\n---\n\n\n# Introduction\n\nMultiple linear regression is a powerful statistical method that allows us to examine the relationship between a dependent variable and multiple independent variables.\n\n# Example\n\n## Step 1: Load the dataset\n\n```R\n# Load the mtcars dataset\ndata(mtcars)\n```\n\n## Step 2: Build the model\n\nNow, let's create the multiple linear regression model using the specified variables: disp, hp, and drat.\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# Build the multiple linear regression model\nmodel <- lm(mpg ~ disp + hp + drat, data = mtcars)\n```\n:::\n\n\n## Step 3: Examine the data\n\nIt's always a good idea to take a look at the relationships between variables before diving into the model. The `pairs()` function helps us with that.\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# Examine relationships between variables\npairs(mtcars[,c(\"mpg\",\"disp\",\"hp\",\"drat\")])\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-2-1.png){width=672}\n:::\n:::\n\n\n## Step 4: Check for multicollinearity\n\nMulticollinearity is when independent variables in a regression model are highly correlated. It can affect the stability and reliability of our model. Keep an eye on the scatterplots in the pairs plot to get a sense of this.\n\n## Step 5: Plot the residuals\n\nNow, let's check the model's residuals using a scatterplot. Residuals are the differences between observed and predicted values. They should ideally show no pattern.\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# Plot the residuals\nplot(\n model$residuals, \n main = \"Residuals vs Fitted Values\", \n xlab = \"Fitted Values\", \n ylab = \"Residuals\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-3-1.png){width=672}\n:::\n:::\n\n\n## Step 6: Evaluate the model\n\nBy examining the residuals vs. fitted values plot, we can identify patterns that may suggest non-linearity or heteroscedasticity. Ideally, residuals should be randomly scattered.\n\n## Step 7: Encourage readers to try it themselves\n\nI'd encourage readers to take the code snippets, run them in their R environment, and explore. Maybe try different variables, tweak the model, or even use another dataset. Hands-on experience is the best teacher!\n\nRemember, understanding the data and interpreting the results is as important as running the code. It's a fascinating journey into uncovering patterns and relationships within your data.\n\nFeel free to reach out if you have any questions or if there's anything specific you'd like to explore further. Happy coding!",
"supporting": [
"index_files"
],
"filters": [
"rmarkdown/pagebreak.lua"
],
"includes": {},
"engineDependencies": {},
"preserve": {},
"postProcess": true
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
505 changes: 270 additions & 235 deletions docs/index.html

Large diffs are not rendered by default.

554 changes: 129 additions & 425 deletions docs/index.xml

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/listings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
"listing": "/index.html",
"items": [
"/posts/2023-11-15/index.html",
"/posts/2023-11-14/index.html",
"/posts/2023-11-13/index.html",
"/posts/2023-11-07/index.html",
Expand Down
646 changes: 646 additions & 0 deletions docs/posts/2023-11-15/index.html

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
58 changes: 57 additions & 1 deletion docs/search.json

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion docs/sitemap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</url>
<url>
<loc>https://www.spsanderson.com/steveondata/index.html</loc>
<lastmod>2023-11-14T12:52:55.864Z</lastmod>
<lastmod>2023-11-15T13:40:04.468Z</lastmod>
</url>
<url>
<loc>https://www.spsanderson.com/steveondata/posts/rtip-2023-04-06/index.html</loc>
Expand Down Expand Up @@ -564,4 +564,8 @@
<loc>https://www.spsanderson.com/steveondata/posts/2023-11-14/index.html</loc>
<lastmod>2023-11-14T12:53:03.604Z</lastmod>
</url>
<url>
<loc>https://www.spsanderson.com/steveondata/posts/2023-11-15/index.html</loc>
<lastmod>2023-11-15T13:40:12.254Z</lastmod>
</url>
</urlset>
67 changes: 67 additions & 0 deletions posts/2023-11-15/index.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
title: "How to Perform Multiple Linear Regression in R"
author: "Steven P. Sanderson II, MPH"
date: "2023-11-15"
categories: [rtip]
---

# Introduction

Multiple linear regression is a powerful statistical method that allows us to examine the relationship between a dependent variable and multiple independent variables.

# Example

## Step 1: Load the dataset

```R
# Load the mtcars dataset
data(mtcars)
```

## Step 2: Build the model

Now, let's create the multiple linear regression model using the specified variables: disp, hp, and drat.

```{r}
# Build the multiple linear regression model
model <- lm(mpg ~ disp + hp + drat, data = mtcars)
```

## Step 3: Examine the data

It's always a good idea to take a look at the relationships between variables before diving into the model. The `pairs()` function helps us with that.

```{r}
# Examine relationships between variables
pairs(mtcars[,c("mpg","disp","hp","drat")])
```

## Step 4: Check for multicollinearity

Multicollinearity is when independent variables in a regression model are highly correlated. It can affect the stability and reliability of our model. Keep an eye on the scatterplots in the pairs plot to get a sense of this.

## Step 5: Plot the residuals

Now, let's check the model's residuals using a scatterplot. Residuals are the differences between observed and predicted values. They should ideally show no pattern.

```{r}
# Plot the residuals
plot(
model$residuals,
main = "Residuals vs Fitted Values",
xlab = "Fitted Values",
ylab = "Residuals"
)
```

## Step 6: Evaluate the model

By examining the residuals vs. fitted values plot, we can identify patterns that may suggest non-linearity or heteroscedasticity. Ideally, residuals should be randomly scattered.

## Step 7: Encourage readers to try it themselves

I'd encourage readers to take the code snippets, run them in their R environment, and explore. Maybe try different variables, tweak the model, or even use another dataset. Hands-on experience is the best teacher!

Remember, understanding the data and interpreting the results is as important as running the code. It's a fascinating journey into uncovering patterns and relationships within your data.

Feel free to reach out if you have any questions or if there's anything specific you'd like to explore further. Happy coding!

0 comments on commit 7677480

Please sign in to comment.