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
# Subsetting data for Ideal and Premium cuts only
sub_data <- diamonds %>% filter(cut %in% c("Ideal", "Premium"))
# Descriptive statistics
sub_data %>% group_by(cut) %>% summarise(
mean_price = mean(price, na.rm = TRUE),
sd_price = sd(price, na.rm = TRUE),
n = n()
)
## # A tibble: 2 × 4
## cut mean_price sd_price n
## <ord> <dbl> <dbl> <int>
## 1 Premium 4584. 4349. 13791
## 2 Ideal 3458. 3808. 21551
# T-test
t_test_result <- t.test(price ~ cut, data = sub_data)
# ANOVA
anova_result <- aov(price ~ cut, data = sub_data)
# Results
t_test_result
##
## Welch Two Sample t-test
##
## data: price by cut
## t = 24.918, df = 26552, p-value < 2.2e-16
## alternative hypothesis: true difference in means between group Premium and group Ideal is not equal to 0
## 95 percent confidence interval:
## 1038.088 1215.344
## sample estimates:
## mean in group Premium mean in group Ideal
## 4584.258 3457.542
summary(anova_result)
## Df Sum Sq Mean Sq F value Pr(>F)
## cut 1 1.068e+10 1.068e+10 658 <2e-16 ***
## Residuals 35340 5.734e+11 1.623e+07
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# Descriptive statistics
diamonds %>% group_by(clarity) %>% summarise(
mean_carat = mean(carat, na.rm = TRUE),
sd_carat = sd(carat, na.rm = TRUE),
n = n()
)
## # A tibble: 8 × 4
## clarity mean_carat sd_carat n
## <ord> <dbl> <dbl> <int>
## 1 I1 1.28 0.632 741
## 2 SI2 1.08 0.517 9194
## 3 SI1 0.850 0.450 13065
## 4 VS2 0.764 0.446 12258
## 5 VS1 0.727 0.424 8171
## 6 VVS2 0.596 0.360 5066
## 7 VVS1 0.503 0.300 3655
## 8 IF 0.505 0.313 1790
# ANOVA
anova_result <- aov(carat ~ clarity, data = diamonds)
# Results
summary(anova_result)
## Df Sum Sq Mean Sq F value Pr(>F)
## clarity 7 1662 237.48 1225 <2e-16 ***
## Residuals 53932 10457 0.19
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# Boxplot for price distribution across Ideal and Premium cuts
ggplot(sub_data, aes(x = cut, y = price)) +
geom_boxplot() +
labs(title = "Price Distribution Across Ideal and Premium Cuts",
x = "Cut",
y = "Price") +
theme_minimal()
# Boxplot for carat size distribution across different clarity levels
ggplot(diamonds, aes(x = clarity, y = carat)) +
geom_boxplot() +
labs(title = "Carat Size Distribution Across Different Clarity Levels",
x = "Clarity",
y = "Carat Size") +
theme_minimal()