This is a simple analysis of the mtcars dataset. The dataset contains information about 32 cars of different brands and models. The data is available from data/mtcars/mtcars.rds. The dataset contains the following variables:
mpg: Miles/(US) galloncyl: Number of cylindersdisp: Displacement (cu.in.)hp: Gross horsepowerdrat: Rear axle ratiowt: Weight (1000 lbs)qsec: 1/4 mile timevs: Engine (0 = V-shaped, 1 = straight)am: Transmission (0 = automatic, 1 = manual)gear: Number of forward gearscarb: Number of carburetorsThe analysis goals are:
We first load the data from data/mtcars/mtcars.rds and view it as a table.
We calculate the correlation of weight and miles per gallon.
corr <- cor.test(mtcars$wt, mtcars$mpg)
corr
##
## Pearson's product-moment correlation
##
## data: mtcars$wt and mtcars$mpg
## t = -9.559, df = 30, p-value = 1.294e-10
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.9338264 -0.7440872
## sample estimates:
## cor
## -0.8676594
We plot the relationship between weight and miles per gallon with the correlation coefficient.
mtcars %>%
ggplot(aes(x = wt, y = mpg)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
labs(
title = "Relationship between weight and miles per gallon",
x = "Weight (1000 lbs)",
y = "Miles/(US) gallon",
caption = "Source: mtcars dataset"
) +
theme_minimal(base_size = 15) +
# Add the correlation coefficient
annotate(
"text", x = 4.75, y = 25,
label = paste0("Correlation coefficient: ", round(corr$estimate, 3))
)
The correlation between weight and miles per gallon is -0.8677. The relationship between weight and miles per gallon is negative and strong. The significance is 1.294e-10 which is less than 0.05. Therefore, we can reject the null hypothesis and conclude that there is a significant correlation between weight and miles per gallon.