-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2f4e71c
commit 7677480
Showing
12 changed files
with
1,191 additions
and
662 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
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.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! |