This notebook provides an analysis of the diamonds dataset. We aim to understand the factors affecting the quality of a diamond’s cut using logistic regression.
The diamonds dataset of the ggplot2 package consists of 53940 entries and 11 columns, providing various attributes of diamonds, such as carat, cut, color, clarity, depth, table, price, and dimensions (x, y, z).
#load the dataset
data("diamonds")
# Display the first few rows of the dataset
head(diamonds)
## # A tibble: 6 × 10
## carat cut color clarity depth table price x y z
## <dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43
## 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31
## 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
## 4 0.29 Premium I VS2 62.4 58 334 4.2 4.23 2.63
## 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75
## 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48
Next, we’ll create a binary variable representing whether a diamond has a “High Quality” cut.
diamonds$high_quality_cut <- ifelse(diamonds$cut %in% c("Ideal", "Premium"), 1, 0)
table(diamonds$high_quality_cut)
##
## 0 1
## 18598 35342
We’ll build a logistic regression model using carat,
depth, table, and price as
explanatory variables.
model <- glm(high_quality_cut ~ carat + depth + table + price, data=diamonds, family="binomial")
summary(model)
##
## Call:
## glm(formula = high_quality_cut ~ carat + depth + table + price,
## family = "binomial", data = diamonds)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 5.526e+01 6.983e-01 79.130 <2e-16 ***
## carat -5.513e-01 5.627e-02 -9.797 <2e-16 ***
## depth -5.313e-01 8.307e-03 -63.959 <2e-16 ***
## table -3.758e-01 5.289e-03 -71.060 <2e-16 ***
## price 8.005e-05 6.726e-06 11.901 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 69492 on 53939 degrees of freedom
## Residual deviance: 60529 on 53935 degrees of freedom
## AIC: 60539
##
## Number of Fisher Scoring iterations: 4
Let’s interpret the coefficients of the logistic regression model:
Intercept (Constant): The coefficient for the intercept is 55.2574. This value represents the log-odds of a diamond having a high-quality cut when all the explanatory variables are set to zero. However, interpreting the intercept in isolation is not always meaningful, especially in the context of this dataset.
Carat: The coefficient for carat is -0.5513. This implies that for a one-unit increase in the carat size of the diamond, the log-odds of the diamond having a high-quality cut decrease by 0.5513, keeping all other variables constant. In simpler terms, larger diamonds are less likely to have a high-quality cut.
Depth: The coefficient for depth is -0.5313. This means that for every one percent increase in depth, the log-odds of the diamond having a high-quality cut decrease by 0.5313, holding all other factors constant.
Table: The coefficient for table is -0.3758. This indicates that for every one-unit increase in the table size of the diamond, the log-odds of it having a high-quality cut decrease by 0.3758, all else being equal.
Price: The coefficient for price is 0.00008005. This suggests that for every one-unit increase in the price of the diamond, the log-odds of the diamond having a high-quality cut increase slightly, given that other variables remain the same.
We can calculate the 95% confidence interval for the coefficient of
the carat variable using its standard error.
carat_coefficient <- coef(model)["carat"]
carat_std_err <- summary(model)$coefficients["carat", "Std. Error"]
z_value <- qnorm(0.975) # 1.96 for a 95% CI
lower_bound <- carat_coefficient - z_value * carat_std_err
upper_bound <- carat_coefficient + z_value * carat_std_err
c(lower_bound, upper_bound)
## carat carat
## -0.6616119 -0.4410272
Interpretation: With 95% confidence, we can say that
the true value of the coefficient for carat (representing
its effect on the log-odds of a diamond having a high-quality cut) lies
between the above calculated values, holding all other variables
constant.
Let’s visualize the relationships between the response variable and each explanatory variable using scatter plots.
plot(diamonds$carat, diamonds$high_quality_cut, xlab="Carat", ylab="High Quality Cut", main="Carat vs High Quality Cut")
plot(diamonds$depth, diamonds$high_quality_cut, xlab="Depth", ylab="High Quality Cut", main="Depth vs High Quality Cut")
plot(diamonds$table, diamonds$high_quality_cut, xlab="Table", ylab="High Quality Cut", main="Table vs High Quality Cut")
plot(diamonds$price, diamonds$high_quality_cut, xlab="Price", ylab="High Quality Cut", main="Price vs High Quality Cut")
Given the scatter plots, the carat variable seems to
have a potential non-linear relationship with the high-quality cut.
We’ll visualize the relationship between the log-transformed
carat and the high-quality cut.
diamonds$log_carat <- log(diamonds$carat)
plot(diamonds$log_carat, diamonds$high_quality_cut, xlab="Log(Carat)", ylab="High Quality Cut", main="Log(Carat) vs High Quality Cut")
The analysis has provided insights into the factors affecting the quality of a diamond’s cut. There’s potential for further exploration and refinement of the model, especially by considering variable transformations and interactions.
Some further questions to consider:
- Would
using the log-transformed carat variable improve the
predictive power of the logistic regression model?
- Are there any
other potential transformations or interactions between variables that
could improve the model?
- Would a more complex model, like a
decision tree or a random forest, provide better insights into the
relationships in the data?