This document provides an analysis of the diamonds dataset. We’ll explore relationships between various variables, visualize these relationships, calculate correlation coefficients, and determine confidence intervals for the response variables.
#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
We will consider three sets of variable combinations and create new variables for the analysis.
diamonds$volume <- (4/3) * pi * diamonds$x * diamonds$y * diamonds$z
diamonds$price_per_carat <- diamonds$price / diamonds$carat
diamonds$aspect_ratio <- diamonds$x / diamonds$y
# Plotting for Set 1
p1 <- ggplot(diamonds, aes(x = carat, y = price)) +
geom_point(aes(color = depth), alpha = 0.5) +
labs(title = "Price vs Carat", x = "Carat", y = "Price") +
theme_minimal()
p2 <- ggplot(diamonds, aes(x = depth, y = price)) +
geom_point(aes(color = carat), alpha = 0.5) +
labs(title = "Price vs Depth", x = "Depth", y = "Price") +
theme_minimal()
p3 <- ggplot(diamonds, aes(x = table, y = price)) +
geom_point(aes(color = carat), alpha = 0.5) +
labs(title = "Price vs Table", x = "Table", y = "Price") +
theme_minimal()
p4 <- ggplot(diamonds, aes(x = volume, y = price)) +
geom_point(aes(color = carat), alpha = 0.5) +
labs(title = "Price vs Volume", x = "Volume", y = "Price") +
theme_minimal()
grid.arrange(p1, p2, p3, p4, ncol = 2)
# Plot 1: Carat vs Depth
p5 <- ggplot(diamonds, aes(x = depth, y = carat)) +
geom_point(aes(color = price_per_carat), alpha = 0.5) +
labs(title = "Carat vs Depth", x = "Depth", y = "Carat") +
theme_minimal()
# Plot 2: Carat vs Table
p6 <- ggplot(diamonds, aes(x = table, y = carat)) +
geom_point(aes(color = price_per_carat), alpha = 0.5) +
labs(title = "Carat vs Table", x = "Table", y = "Carat") +
theme_minimal()
# Plot 3: Carat vs Price per Carat
p7 <- ggplot(diamonds, aes(x = price_per_carat, y = carat)) +
geom_point(aes(color = depth), alpha = 0.5) +
labs(title = "Carat vs Price per Carat", x = "Price per Carat", y = "Carat") +
theme_minimal()
grid.arrange(p5, p6, p7, ncol = 2)
# Plotting for Set 3
# Plot 1: Depth vs Table
p8 <- ggplot(diamonds, aes(x = table, y = depth)) +
geom_point(aes(color = carat), alpha = 0.5) +
labs(title = "Depth vs Table", x = "Table", y = "Depth") +
theme_minimal()
# Plot 2: Depth vs Carat
p9 <- ggplot(diamonds, aes(x = carat, y = depth)) +
geom_point(aes(color = aspect_ratio), alpha = 0.5) +
labs(title = "Depth vs Carat", x = "Carat", y = "Depth") +
theme_minimal()
# Plot 3: Depth vs Aspect Ratio
p10 <- ggplot(diamonds, aes(x = aspect_ratio, y = depth)) +
geom_point(aes(color = carat), alpha = 0.5) +
labs(title = "Depth vs Aspect Ratio", x = "Aspect Ratio", y = "Depth") +
theme_minimal()
grid.arrange(p8, p9, p10, ncol = 2)
## Warning: Removed 7 rows containing missing values (`geom_point()`).
# Calculate correlation coefficients for each set
cor_set1 <- cor(diamonds[, c("price", "carat", "depth", "table", "volume")], method = "pearson")
cor_set2 <- cor(diamonds[, c("carat", "depth", "table", "price_per_carat")], method = "pearson")
cor_set3 <- cor(diamonds[, c("depth", "table", "carat", "aspect_ratio")], method = "pearson")
cor_set1
## price carat depth table volume
## price 1.0000000 0.92159130 -0.010647405 0.1271339 0.902384536
## carat 0.9215913 1.00000000 0.028224314 0.1816175 0.976308385
## depth -0.0106474 0.02822431 1.000000000 -0.2957785 0.009157248
## table 0.1271339 0.18161755 -0.295778522 1.0000000 0.167400017
## volume 0.9023845 0.97630838 0.009157248 0.1674000 1.000000000
cor_set2
## carat depth table price_per_carat
## carat 1.00000000 0.02822431 0.18161755 0.76974386
## depth 0.02822431 1.00000000 -0.29577852 -0.03566946
## table 0.18161755 -0.29577852 1.00000000 0.08470043
## price_per_carat 0.76974386 -0.03566946 0.08470043 1.00000000
cor_set3
## depth table carat aspect_ratio
## depth 1.00000000 -0.2957785 0.02822431 NA
## table -0.29577852 1.0000000 0.18161755 NA
## carat 0.02822431 0.1816175 1.00000000 NA
## aspect_ratio NA NA NA 1
# Calculate 95% confidence intervals for the mean of the response variables
conf_interval_price <- t.test(diamonds$price)$conf.int
conf_interval_carat <- t.test(diamonds$carat)$conf.int
conf_interval_depth <- t.test(diamonds$depth)$conf.int
conf_interval_price
## [1] 3899.132 3966.467
## attr(,"conf.level")
## [1] 0.95
conf_interval_carat
## [1] 0.7939395 0.8019400
## attr(,"conf.level")
## [1] 0.95
conf_interval_depth
## [1] 61.73731 61.76150
## attr(,"conf.level")
## [1] 0.95
The analysis provides insights into the relationships between various attributes of diamonds. The visualizations help in understanding the trends and patterns, while the correlation coefficients quantify these relationships. The confidence intervals provide an estimate of the population mean for the response variables.