library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## âś” dplyr 1.1.0 âś” readr 2.1.4
## âś” forcats 1.0.0 âś” stringr 1.5.0
## âś” ggplot2 3.4.1 âś” tibble 3.1.8
## âś” lubridate 1.9.2 âś” tidyr 1.3.0
## âś” purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## âś– dplyr::filter() masks stats::filter()
## âś– dplyr::lag() masks stats::lag()
## ℹ Use the ]8;;http://conflicted.r-lib.org/conflicted package]8;; to force all conflicts to become errors
library(ggplot2)
library(dplyr)
library(ggrepel)
## Warning: package 'ggrepel' was built under R version 4.2.3
library(effsize)
## Warning: package 'effsize' was built under R version 4.2.3
library(pwrss)
## Warning: package 'pwrss' was built under R version 4.2.3
##
## Attaching package: 'pwrss'
##
## The following object is masked from 'package:stats':
##
## power.t.test
library(boot)
library(broom)
library(lindia)
## Warning: package 'lindia' was built under R version 4.2.3
library(vcd)
## Warning: package 'vcd' was built under R version 4.2.3
## Loading required package: grid
# Build a linear model
linear_model <- lm(price ~ carat + cut + color, data = diamonds)
summary(linear_model)
##
## Call:
## lm(formula = price ~ carat + cut + color, data = diamonds)
##
## Residuals:
## Min 1Q Median 3Q Max
## -17313.9 -751.2 -83.9 543.6 12273.0
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -3149.82 15.76 -199.905 < 2e-16 ***
## carat 8183.74 13.90 588.885 < 2e-16 ***
## cut.L 1243.35 24.74 50.260 < 2e-16 ***
## cut.Q -531.75 21.93 -24.252 < 2e-16 ***
## cut.C 372.06 19.16 19.417 < 2e-16 ***
## cut^4 76.15 15.39 4.949 7.49e-07 ***
## color.L -1579.17 21.72 -72.699 < 2e-16 ***
## color.Q -732.85 19.86 -36.902 < 2e-16 ***
## color.C -107.41 18.64 -5.763 8.32e-09 ***
## color^4 81.63 17.12 4.769 1.85e-06 ***
## color^5 -138.64 16.18 -8.568 < 2e-16 ***
## color^6 -161.09 14.68 -10.973 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1432 on 53928 degrees of freedom
## Multiple R-squared: 0.8711, Adjusted R-squared: 0.8711
## F-statistic: 3.315e+04 on 11 and 53928 DF, p-value: < 2.2e-16
So for this week’s data dive, I’ve decided to do a simple linear model with the response variable being price and explanatory variables being cut, color, and carat because they are the three main variables that directly influence the price of diamonds. Something that stand out for me right away is the residual ranges from -17313.9 to 12273.0 which is a really big range for price.
# Get the predicted values and residuals from the model
predicted_values <- predict(linear_model, type = "response")
residuals <- residuals(linear_model)
# Create a scatter plot of residuals vs. predicted values
plot(predicted_values, residuals,
xlab = "Predicted Values", ylab = "Residuals",
main = "Residuals vs. Predicted Values",
col = "blue", pch = 16)
# Add a horizontal line at y = 0 for reference
abline(h = 0, col = "red", lwd = 2)
The graph above shows the scatterplot of the residual of the predicted values vs the actual values. The residuals show a relatively random pattern around the 0 line so it’s safe to assume the relationship between the variables is linear.
qqnorm(residuals)
qqline(residuals, col = 2)
Just to confirm the linearity relationship, I also performed a Q-Q plot and the distribution falls approximately along the diagonal line which indicates that the residuals follow a normal distribution.
Overall, the model explains a significant portion of the variance in diamond prices, but there might be room for improvement, especially concerning the wide range of residuals. Further exploration of the data and potential inclusion of additional relevant variables could enhance the model’s performance. Additionally, checking for potential outliers and considering transformations for predictors might be beneficial in improving the model fit.
# Log-transform the predictor variable (carat)
diamonds$log_carat <- log(diamonds$carat)
# Build a new linear regression model with the transformed predictor
model_transformed <- lm(price ~ log_carat + cut + color, data = diamonds)
# Check the summary of the new model
summary(model_transformed)
##
## Call:
## lm(formula = price ~ log_carat + cut + color, data = diamonds)
##
## Residuals:
## Min 1Q Median 3Q Max
## -6206.2 -1476.5 -364.8 1102.8 11849.0
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5836.047 14.105 413.750 < 2e-16 ***
## log_carat 6046.018 15.722 384.546 < 2e-16 ***
## cut.L 1302.921 34.913 37.319 < 2e-16 ***
## cut.Q -530.642 30.898 -17.174 < 2e-16 ***
## cut.C 224.722 26.986 8.327 < 2e-16 ***
## cut^4 56.199 21.685 2.592 0.00956 **
## color.L -862.177 30.312 -28.443 < 2e-16 ***
## color.Q -479.320 27.952 -17.148 < 2e-16 ***
## color.C -150.396 26.263 -5.727 1.03e-08 ***
## color^4 2.771 24.118 0.115 0.90855
## color^5 -108.715 22.804 -4.767 1.87e-06 ***
## color^6 -162.683 20.687 -7.864 3.79e-15 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2018 on 53928 degrees of freedom
## Multiple R-squared: 0.7441, Adjusted R-squared: 0.7441
## F-statistic: 1.426e+04 on 11 and 53928 DF, p-value: < 2.2e-16
# Check the diagnostic plots for the transformed model
par(mfrow = c(2, 2))
plot(model_transformed)
The log transformation on the carat variable helps lowering the range of the residual compared to the original linear model that we have. However, the adjusted \(R^2\) value for the new model is 0.7441, compared to the adjusted \(R^2\) value of 0.8711 for the original model which indicates that the new model might not be a better fit for the model to the data.
Intercept: The intercept of -3149.82 represents the estimated price of a diamond when all other predictors (carat, cut, and color) are zero. However, since carat, cut, and color are all categorical variables, having a value of zero for them doesn’t have a meaningful interpretation in this context.
Carat: The coefficient for carat is 8183.74. This means that for every one unit increase in carat, the price of the diamond is estimated to increase by $8183.74.