This report presents a comprehensive statistical analysis of diamonds dataset from the ggplot2 package. The aim is to uncover insights that can be applied in practical settings, specifically within the diamond industry.
The dataset includes the following variables:
data("diamonds")
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
For a comprehensive analysis, we can explore:
We begin by examining the basic statistical measures for each numerical column in the dataset.
summary(diamonds)
## carat cut color clarity depth
## Min. :0.2000 Fair : 1610 D: 6775 SI1 :13065 Min. :43.00
## 1st Qu.:0.4000 Good : 4906 E: 9797 VS2 :12258 1st Qu.:61.00
## Median :0.7000 Very Good:12082 F: 9542 SI2 : 9194 Median :61.80
## Mean :0.7979 Premium :13791 G:11292 VS1 : 8171 Mean :61.75
## 3rd Qu.:1.0400 Ideal :21551 H: 8304 VVS2 : 5066 3rd Qu.:62.50
## Max. :5.0100 I: 5422 VVS1 : 3655 Max. :79.00
## J: 2808 (Other): 2531
## table price x y
## Min. :43.00 Min. : 326 Min. : 0.000 Min. : 0.000
## 1st Qu.:56.00 1st Qu.: 950 1st Qu.: 4.710 1st Qu.: 4.720
## Median :57.00 Median : 2401 Median : 5.700 Median : 5.710
## Mean :57.46 Mean : 3933 Mean : 5.731 Mean : 5.735
## 3rd Qu.:59.00 3rd Qu.: 5324 3rd Qu.: 6.540 3rd Qu.: 6.540
## Max. :95.00 Max. :18823 Max. :10.740 Max. :58.900
##
## z
## Min. : 0.000
## 1st Qu.: 2.910
## Median : 3.530
## Mean : 3.539
## 3rd Qu.: 4.040
## Max. :31.800
##
The descriptive statistics for the diamond dataset provide the following insights:
Next, let’s perform a correlation analysis to understand how different numerical variables, especially carat, depth, table, dimensions (x, y, z), relate to the diamond’s price. This will help in understanding which factors are most influential in determining the price of a diamond.
Understanding the relationships between different numerical variables, especially in relation to the price, is crucial.
numerical_data <- diamonds %>% select_if(is.numeric)
correlation_matrix <- cor(numerical_data)
ggcorrplot(correlation_matrix, lab = TRUE)
The correlation matrix provides insights into how different variables in the diamond dataset are related to each other, particularly in relation to the price:
Now, let’s analyze how categorical variables like cut, color, and clarity affect the price. For this, we will look at average prices within each category of these variables. We will also investigate the distribution of prices and carat weights, which can provide insights into the typical characteristics of diamonds in the dataset and their pricing patterns.
avg_price_by_cut <- diamonds %>%
group_by(cut) %>%
summarize(Avg_Price = mean(price))
avg_price_by_color <- diamonds %>%
group_by(color) %>%
summarize(Avg_Price = mean(price))
avg_price_by_clarity <- diamonds %>%
group_by(clarity) %>%
summarize(Avg_Price = mean(price))
# Plotting
par(mfrow = c(1, 3))
barplot(avg_price_by_cut$Avg_Price, names.arg = avg_price_by_cut$cut,
main = "Average Price by Cut", col = "skyblue", ylab = "Average Price ($)")
barplot(avg_price_by_color$Avg_Price, names.arg = avg_price_by_color$color,
main = "Average Price by Color", col = "lightgreen", ylab = "Average Price ($)")
barplot(avg_price_by_clarity$Avg_Price, names.arg = avg_price_by_clarity$clarity,
main = "Average Price by Clarity", col = "salmon", ylab = "Average Price ($)")
The analysis of how cut, color, and clarity affect the average price of diamonds reveals several insights:
ggplot(diamonds, aes(x = price)) +
geom_histogram(bins = 50, fill = "purple") +
labs(title = "Distribution of Diamond Prices", x = "Price ($)", y = "Frequency")
ggplot(diamonds, aes(x = carat)) +
geom_histogram(bins = 50, fill = "orange") +
labs(title = "Distribution of Diamond Carat Weights", x = "Carat", y = "Frequency")
Additionally, the distribution plots for diamond prices and carat weights provide further insights:
Outliers can significantly affect the results of a statistical analysis. In the context of the diamonds dataset, we can focus on detecting outliers in key variables like price, carat, and the physical dimensions (x, y, z).
# Outlier detection for 'price' and 'carat'
par(mfrow=c(1,2)) # Plotting two plots side by side
boxplot(diamonds$price, main="Price", horizontal=TRUE, col="lightblue")
boxplot(diamonds$carat, main="Carat", horizontal=TRUE, col="lightgreen")
# Outlier detection for dimensions 'x', 'y', 'z'
par(mfrow=c(1,3)) # Plotting three plots side by side
boxplot(diamonds$x, main="Length (x)", horizontal=TRUE, col="lightcoral")
boxplot(diamonds$y, main="Width (y)", horizontal=TRUE, col="lightyellow")
boxplot(diamonds$z, main="Depth (z)", horizontal=TRUE, col="lightskyblue")
This code creates boxplots for the price, carat, and dimensions (x, y, z) which can help in visually identifying outliers (points that fall outside the whiskers of the boxplot).
Visual representations can provide insights into the relationships and patterns within the data. Let’s create some additional visualizations:
ggplot(diamonds, aes(x = carat, y = price)) +
geom_point(alpha = 0.5) +
geom_smooth(method = "lm", color = "red") +
labs(title = "Price vs. Carat", x = "Carat", y = "Price ($)")
## `geom_smooth()` using formula = 'y ~ x'
This scatter plot with a regression line will show how price varies with carat weight.
ggplot(diamonds, aes(x = price, fill = cut)) +
geom_histogram(bins = 30, position = "identity", alpha = 0.6) +
scale_fill_brewer(palette = "Set1") +
labs(title = "Price Distribution by Cut", x = "Price ($)", y = "Count")
This histogram provides insight into the distribution of prices across different cuts.
ggplot(diamonds, aes(x = carat, fill = clarity)) +
geom_histogram(bins = 30, position = "identity", alpha = 0.6) +
scale_fill_brewer(palette = "Pastel1") +
labs(title = "Carat Distribution by Clarity", x = "Carat", y = "Count")
This histogram shows how carat weight is distributed across different clarity grades.
Based on this analysis, the following practical insights can be derived:
This analysis can help different stakeholders in the diamond industry make informed decisions based on the characteristics that most significantly impact diamond prices.
In conclusion, the comprehensive statistical analysis of the diamonds dataset reveals several key insights that can guide decisions for different stakeholders in the diamond industry: