library(ggplot2)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(tidyr)
# Load the diamonds dataset
data("diamonds")
# Create a new column "binary_cut" where "Ideal" and "Premium" are 1, and the rest are 0
diamonds <- diamonds %>%
mutate(binary_cut = ifelse(cut %in% c("Ideal", "Premium"), 1, 0))
# View the first few rows of the dataset to see the changes
head(diamonds)
## # A tibble: 6 × 11
## carat cut color clarity depth table price x y z binary_cut
## <dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl> <dbl>
## 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43 1
## 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31 1
## 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31 0
## 4 0.29 Premium I VS2 62.4 58 334 4.2 4.23 2.63 1
## 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75 0
## 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48 0
Task: Create a binary variable from the “cut” column in the diamonds dataset.
Insight: By creating a binary variable from the “cut” column, we can group the diamond cuts into two categories: “Ideal” and “Premium” as one group (binary 1) and all other cuts as another group (binary 0). This binary variable allows for a simplified way to analyze the influence of cut quality on other attributes of the diamonds.
Significance: The cut of a diamond is an important factor that can influence its price and overall quality. By converting this categorical variable into a binary variable, we can easily conduct binary classification or regression analysis to understand how diamonds with “Ideal” and “Premium” cuts differ from the rest in terms of carat, price, clarity, color, etc.
Further Questions:
How does this binary variable (cut quality) correlate with other attributes of diamonds such as carat, price, clarity, and color? Are diamonds with “Ideal” or “Premium” cuts significantly more expensive than other cuts? Does the binary cut variable influence customer preferences when buying diamonds? Is there a significant difference in the average carat size between “Ideal” and “Premium” cuts and other cuts?
library(stats)
library(broom)
# Create a binary variable "binary_cut" (1 for "Ideal" and "Premium," 0 for others)
diamonds <- diamonds %>%
mutate(binary_cut = ifelse(cut %in% c("Ideal", "Premium"), 1, 0))
# Build a logistic regression model
model <- glm(binary_cut ~ carat + price + clarity + color, data = diamonds, family = binomial)
# Print the model summary
summary(model)
##
## Call:
## glm(formula = binary_cut ~ carat + price + clarity + color, family = binomial,
## data = diamonds)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.348e+00 3.735e-02 36.085 < 2e-16 ***
## carat -1.597e+00 7.655e-02 -20.866 < 2e-16 ***
## price 1.670e-04 8.416e-06 19.847 < 2e-16 ***
## clarity.L 3.094e-01 6.683e-02 4.629 3.67e-06 ***
## clarity.Q 2.611e-01 5.646e-02 4.625 3.74e-06 ***
## clarity.C 1.702e-01 4.713e-02 3.612 0.000304 ***
## clarity^4 1.172e-01 3.750e-02 3.126 0.001773 **
## clarity^5 4.443e-02 3.045e-02 1.459 0.144549
## clarity^6 -1.757e-01 2.616e-02 -6.718 1.84e-11 ***
## clarity^7 8.772e-02 2.307e-02 3.803 0.000143 ***
## color.L 2.869e-01 3.581e-02 8.011 1.14e-15 ***
## color.Q -4.441e-03 2.977e-02 -0.149 0.881435
## color.C -1.277e-01 2.746e-02 -4.650 3.32e-06 ***
## color^4 6.498e-02 2.535e-02 2.563 0.010383 *
## color^5 1.263e-02 2.396e-02 0.527 0.598177
## color^6 -5.911e-02 2.207e-02 -2.678 0.007411 **
## ---
## 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: 68190 on 53924 degrees of freedom
## AIC: 68222
##
## Number of Fisher Scoring iterations: 4
# Calculate the confidence intervals for coefficients
conf_intervals <- tidy(model, conf.int = TRUE)
# View the confidence intervals for coefficients
print(conf_intervals)
## # A tibble: 16 × 7
## term estimate std.error statistic p.value conf.low conf.high
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 1.35 0.0374 36.1 3.91e-285 1.27 1.42
## 2 carat -1.60 0.0766 -20.9 1.09e- 96 -1.75 -1.45
## 3 price 0.000167 0.00000842 19.8 1.16e- 87 0.000151 0.000184
## 4 clarity.L 0.309 0.0668 4.63 3.67e- 6 0.178 0.440
## 5 clarity.Q 0.261 0.0565 4.63 3.74e- 6 0.151 0.372
## 6 clarity.C 0.170 0.0471 3.61 3.04e- 4 0.0780 0.263
## 7 clarity^4 0.117 0.0375 3.13 1.77e- 3 0.0437 0.191
## 8 clarity^5 0.0444 0.0305 1.46 1.45e- 1 -0.0153 0.104
## 9 clarity^6 -0.176 0.0262 -6.72 1.84e- 11 -0.227 -0.124
## 10 clarity^7 0.0877 0.0231 3.80 1.43e- 4 0.0425 0.133
## 11 color.L 0.287 0.0358 8.01 1.14e- 15 0.217 0.357
## 12 color.Q -0.00444 0.0298 -0.149 8.81e- 1 -0.0627 0.0540
## 13 color.C -0.128 0.0275 -4.65 3.32e- 6 -0.181 -0.0739
## 14 color^4 0.0650 0.0254 2.56 1.04e- 2 0.0153 0.115
## 15 color^5 0.0126 0.0240 0.527 5.98e- 1 -0.0343 0.0596
## 16 color^6 -0.0591 0.0221 -2.68 7.41e- 3 -0.102 -0.0158
Insight:
The logistic regression model helps us understand the relationship between the binary “cut” variable and other explanatory variables like carat, price, clarity, and color. It quantifies how these factors influence the likelihood of a diamond having a cut of “Ideal” or “Premium” (binary 1). Significance:
The coefficients in the logistic regression model represent the change in the log-odds of a diamond having an “Ideal” or “Premium” cut for a one-unit change in the corresponding explanatory variable. For example, a positive coefficient for “carat” would suggest that as carat size increases, the odds of a diamond having an “Ideal” or “Premium” cut increase. The model significance can help in pricing diamonds, predicting customer preferences, or guiding diamond cutting processes. Further Questions:
What is the magnitude of the coefficient for “cut” quality? Does it significantly influence the likelihood of an “Ideal” or “Premium” cut? Are there interactions between the explanatory variables (e.g., does the effect of “carat” on cut quality depend on the “clarity” of the diamond)? How well does the logistic regression model predict the binary “cut” variable? Are there any issues with model fit or overfitting? Can we incorporate more explanatory variables, such as dimensions and symmetry, to improve the model’s accuracy in predicting cut quality?
# Create a scatter plot of carat vs. price
ggplot(diamonds, aes(x = carat, y = price)) +
geom_point() +
labs(x = "Carat", y = "Price") +
ggtitle("Scatter Plot of Carat vs. Price")
diamonds$log_carat <- log(diamonds$carat)
diamonds$sqrt_carat <- sqrt(diamonds$carat)
model <- lm(price ~ poly(carat, degree=2), data=diamonds)
Non-Linear Relationship: The scatter plot of carat versus price suggests a non-linear relationship. The points on the plot do not follow a straight line, indicating that a simple linear model may not adequately capture the relationship between carat and price.
Heteroscedasticity: There appears to be heteroscedasticity in the data, meaning that the variability of price increases with carat size. This can be seen as the spread of data points widening as carat size increases, suggesting that the assumption of constant variance in a linear regression model may not hold.
Significance:
The significance of these insights is as follows:
Transformation Need: The non-linear relationship between carat and price indicates that a transformation of the carat variable may be necessary to better capture the underlying relationship in a linear regression model. Without a transformation, a linear model may lead to a poor fit and inaccurate predictions.
Model Improvement: By applying an appropriate transformation, it is possible to build a more accurate regression model that better describes how price is influenced by carat size, accounting for the non-linear nature of the relationship.
Further Questions:
The insights gathered from the scatter plot and the decision to apply a transformation raise some further questions and avenues for investigation:
Choice of Transformation: What type of transformation should be applied to carat (e.g., logarithmic, square root, or polynomial) to best linearize the relationship? It is important to select the transformation that optimally fits the data.
Model Performance: After applying the transformation, how does the transformed model’s performance compare to the initial linear model in terms of predictive accuracy and goodness of fit?