2026-02-04

Data Introduction

  • For this assignment, I used the embedded data set “Diamonds” found in R.
  • Using this data set, we analyze how price is impacted by cut, color, and carat.
  • Analysis was conducted through plotting utilizing ggplot2, plotly, and knitr.

Data Used For This Assignment

Diamond Data Set Embedded in R (First 5 Entries)
carat cut color clarity depth table price x y z
0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43
0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31
0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
0.29 Premium I VS2 62.4 58 334 4.20 4.23 2.63
0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75
  • Data Set based on 53,940 individual diamonds.

Residual, R2, and P-Value

\[R^2 = 1 - \frac{\sum (y_i - \hat{y}_i)^2}{\sum (y_i - \bar{y})^2}\]

## 
## Call:
## lm(formula = price ~ carat, data = diamonds)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -18585.3   -804.8    -18.9    537.4  12731.7 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -2256.36      13.06  -172.8   <2e-16 ***
## carat        7756.43      14.07   551.4   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1549 on 53938 degrees of freedom
## Multiple R-squared:  0.8493, Adjusted R-squared:  0.8493 
## F-statistic: 3.041e+05 on 1 and 53938 DF,  p-value: < 2.2e-16

Price vs. Carat by Cut

Diamond Price vs Carat with Regression

\[Price = \hat{\beta}_0 + \hat{\beta}_1(Carat)\]

## `geom_smooth()` using formula = 'y ~ x'

Color vs Price 3-D

Code for 3-D Plot

plot_ly(
  diamonds,
  x = ~x,
  y = ~color,
  z = ~price,
  type = "scatter3d",
  mode = "markers",
  marker = list(
    size = 2,
    color = ~price,
    colorscale = "Viridis",
    showscale = TRUE)
)

Conclusion

  • Price is directly impacted by color, cut, carat, and clarity.
  • Regression for every carat by cut validates that relationship.
  • The tools provided by ggplot2 and plotly are exquisite in showing these relationships through distinct visual plots.