1 + 1[1] 2
Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.
When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:
1 + 1[1] 2
You can add options to executable code like this
[1] 4
The echo: false option disables the printing of code (only output is displayed).
library(tidyverse)── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.2.1 ✔ readr 2.2.0
✔ forcats 1.0.1 ✔ stringr 1.6.0
✔ ggplot2 4.0.3 ✔ tibble 3.3.1
✔ lubridate 1.9.5 ✔ tidyr 1.3.2
✔ purrr 1.2.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
cars <- read_csv("cars.csv")Rows: 5076 Columns: 18
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (8): Engine Information.Driveline, Engine Information.Engine Type, Engin...
dbl (9): Dimensions.Height, Dimensions.Length, Dimensions.Width, Engine Info...
lgl (1): Engine Information.Hybrid
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
head(cars)# A tibble: 6 × 18
Dimensions.Height Dimensions.Length Dimensions.Width Engine Information.Driv…¹
<dbl> <dbl> <dbl> <chr>
1 140 143 202 All-wheel drive
2 140 143 202 Front-wheel drive
3 140 143 202 Front-wheel drive
4 140 143 202 All-wheel drive
5 140 143 202 All-wheel drive
6 91 17 62 All-wheel drive
# ℹ abbreviated name: ¹`Engine Information.Driveline`
# ℹ 14 more variables: `Engine Information.Engine Type` <chr>,
# `Engine Information.Hybrid` <lgl>,
# `Engine Information.Number of Forward Gears` <dbl>,
# `Engine Information.Transmission` <chr>, `Fuel Information.City mpg` <dbl>,
# `Fuel Information.Fuel Type` <chr>, `Fuel Information.Highway mpg` <dbl>,
# Identification.Classification <chr>, Identification.ID <chr>, …
dim(cars)[1] 5076 18
head(cars)# A tibble: 6 × 18
Dimensions.Height Dimensions.Length Dimensions.Width Engine Information.Driv…¹
<dbl> <dbl> <dbl> <chr>
1 140 143 202 All-wheel drive
2 140 143 202 Front-wheel drive
3 140 143 202 Front-wheel drive
4 140 143 202 All-wheel drive
5 140 143 202 All-wheel drive
6 91 17 62 All-wheel drive
# ℹ abbreviated name: ¹`Engine Information.Driveline`
# ℹ 14 more variables: `Engine Information.Engine Type` <chr>,
# `Engine Information.Hybrid` <lgl>,
# `Engine Information.Number of Forward Gears` <dbl>,
# `Engine Information.Transmission` <chr>, `Fuel Information.City mpg` <dbl>,
# `Fuel Information.Fuel Type` <chr>, `Fuel Information.Highway mpg` <dbl>,
# Identification.Classification <chr>, Identification.ID <chr>, …
summary(cars$`Engine Information.Engine Statistics.Horsepower`) Min. 1st Qu. Median Mean 3rd Qu. Max.
100.0 190.0 266.0 270.5 317.0 638.0
summary(cars$`Fuel Information.Highway mpg`) Min. 1st Qu. Median Mean 3rd Qu. Max.
11.00 20.00 24.00 24.13 28.00 223.00
cars_clean <- cars |>
filter(
!is.na(`Engine Information.Engine Statistics.Horsepower`) &
!is.na(`Fuel Information.Highway mpg`)
)
dim(cars_clean)[1] 5076 18
head(cars_clean)# A tibble: 6 × 18
Dimensions.Height Dimensions.Length Dimensions.Width Engine Information.Driv…¹
<dbl> <dbl> <dbl> <chr>
1 140 143 202 All-wheel drive
2 140 143 202 Front-wheel drive
3 140 143 202 Front-wheel drive
4 140 143 202 All-wheel drive
5 140 143 202 All-wheel drive
6 91 17 62 All-wheel drive
# ℹ abbreviated name: ¹`Engine Information.Driveline`
# ℹ 14 more variables: `Engine Information.Engine Type` <chr>,
# `Engine Information.Hybrid` <lgl>,
# `Engine Information.Number of Forward Gears` <dbl>,
# `Engine Information.Transmission` <chr>, `Fuel Information.City mpg` <dbl>,
# `Fuel Information.Fuel Type` <chr>, `Fuel Information.Highway mpg` <dbl>,
# Identification.Classification <chr>, Identification.ID <chr>, …
Here, I’m using library(tidyverse) to load the packages I need and read_csv() to import my cars dataset. Then, I use head() to see the first few rows and dim() to check how many rows and columns there are. Next, I use summary() to look at the horsepower and highway MPG numbers, including their averages, minimums, and maximums. Finally, I use filter() and is.na() to remove rows with missing values in those two columns, so I can work with complete data for my analysis.
cars_clean <- cars |>
filter(
!is.na(`Engine Information.Engine Statistics.Horsepower`) &
!is.na(`Fuel Information.Highway mpg`)
)
head(cars_clean)# A tibble: 6 × 18
Dimensions.Height Dimensions.Length Dimensions.Width Engine Information.Driv…¹
<dbl> <dbl> <dbl> <chr>
1 140 143 202 All-wheel drive
2 140 143 202 Front-wheel drive
3 140 143 202 Front-wheel drive
4 140 143 202 All-wheel drive
5 140 143 202 All-wheel drive
6 91 17 62 All-wheel drive
# ℹ abbreviated name: ¹`Engine Information.Driveline`
# ℹ 14 more variables: `Engine Information.Engine Type` <chr>,
# `Engine Information.Hybrid` <lgl>,
# `Engine Information.Number of Forward Gears` <dbl>,
# `Engine Information.Transmission` <chr>, `Fuel Information.City mpg` <dbl>,
# `Fuel Information.Fuel Type` <chr>, `Fuel Information.Highway mpg` <dbl>,
# Identification.Classification <chr>, Identification.ID <chr>, …
library(tidyverse)
cars <- read_csv("cars.csv")Rows: 5076 Columns: 18
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (8): Engine Information.Driveline, Engine Information.Engine Type, Engin...
dbl (9): Dimensions.Height, Dimensions.Length, Dimensions.Width, Engine Info...
lgl (1): Engine Information.Hybrid
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Here, I’m using filter() and is.na() to remove rows with missing horsepower or highway MPG values. Then, I use head() to check the first few rows of the cleaned dataset and make sure it’s ready for my analysis.
cars_graph <- cars_clean |>
filter(`Identification.Make` %in% c("BMW", "Audi", "Mercedes-Benz"))
ggplot(cars_graph,
aes(x = `Engine Information.Engine Statistics.Horsepower`,
y = `Fuel Information.Highway mpg`,
color = `Identification.Make`)) +
geom_point(alpha = 0.6) +
scale_color_manual(values = c("blue", "red", "darkgreen")) +
labs(
title = "Horsepower vs. Highway MPG by Manufacturer",
x = "Horsepower",
y = "Highway MPG",
color = "Manufacturer",
caption = "Source: cars.csv (original source to be added)"
) +
theme_minimal()Here, I’m using filter() to select three car manufacturers and ggplot() to compare horsepower and highway MPG. Then, I use geom_point() to create the scatterplot and scale_color_manual() to give each manufacturer a different color. Finally, I use labs() and theme_minimal() to add labels and improve the appearance of the graph.
Here, I’m using lm() to see how horsepower relates to highway MPG. Then, I use summary() to look at the regression results.
Here, I’m using geom_smooth() to add a red regression line to my scatterplot. This helps me see the overall relationship between horsepower and highway MPG.
Here, I’m using autoplot() to create four diagnostic plots. These help me check for unusual patterns and see how well the regression model fits the data.
library(tidyverse)
cars <- read_csv("cars.csv")Rows: 5076 Columns: 18
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (8): Engine Information.Driveline, Engine Information.Engine Type, Engin...
dbl (9): Dimensions.Height, Dimensions.Length, Dimensions.Width, Engine Info...
lgl (1): Engine Information.Hybrid
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
cars_clean <- cars |>
filter(
!is.na(`Engine Information.Engine Statistics.Horsepower`) &
!is.na(`Fuel Information.Highway mpg`)
)
# Create a regression model
model <- lm(
`Fuel Information.Highway mpg` ~
`Engine Information.Engine Statistics.Horsepower`,
data = cars_clean
)
summary(model)
Call:
lm(formula = `Fuel Information.Highway mpg` ~ `Engine Information.Engine Statistics.Horsepower`,
data = cars_clean)
Residuals:
Min 1Q Median 3Q Max
-13.824 -3.026 0.198 3.243 201.170
Coefficients:
Estimate Std. Error t value
(Intercept) 34.2220194 0.2292682 149.27
`Engine Information.Engine Statistics.Horsepower` -0.0373255 0.0007994 -46.69
Pr(>|t|)
(Intercept) <2e-16 ***
`Engine Information.Engine Statistics.Horsepower` <2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 5.427 on 5074 degrees of freedom
Multiple R-squared: 0.3005, Adjusted R-squared: 0.3004
F-statistic: 2180 on 1 and 5074 DF, p-value: < 2.2e-16
ggplot(cars_clean,
aes(x = `Engine Information.Engine Statistics.Horsepower`,
y = `Fuel Information.Highway mpg`)) +
geom_point() +
geom_smooth(method = "lm", formula = y ~ x, color = "red") +
labs(
title = "Horsepower vs. Highway MPG",
x = "Horsepower",
y = "Highway MPG"
) +
theme_minimal()library(ggfortify)
autoplot(model, 1:4, nrow = 2, ncol = 2)Warning: `fortify(<lm>)` was deprecated in ggplot2 4.0.0.
ℹ Please use `broom::augment(<lm>)` instead.
ℹ The deprecated feature was likely used in the ggfortify package.
Please report the issue at <https://github.com/sinhrks/ggfortify/issues>.
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
ℹ The deprecated feature was likely used in the ggfortify package.
Please report the issue at <https://github.com/sinhrks/ggfortify/issues>.