title: Week2|Data Dive
output: html_document
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.0 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.1 ✔ tibble 3.1.8
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the ]8;;http://conflicted.r-lib.org/conflicted package]8;; to force all conflicts to become errors
data(diamonds)
Summary statistics of every column in the data
dim(diamonds)
## [1] 53940 10
str(diamonds)
## tibble [53,940 × 10] (S3: tbl_df/tbl/data.frame)
## $ carat : num [1:53940] 0.23 0.21 0.23 0.29 0.31 0.24 0.24 0.26 0.22 0.23 ...
## $ cut : Ord.factor w/ 5 levels "Fair"<"Good"<..: 5 4 2 4 2 3 3 3 1 3 ...
## $ color : Ord.factor w/ 7 levels "D"<"E"<"F"<"G"<..: 2 2 2 6 7 7 6 5 2 5 ...
## $ clarity: Ord.factor w/ 8 levels "I1"<"SI2"<"SI1"<..: 2 3 5 4 2 6 7 3 4 5 ...
## $ depth : num [1:53940] 61.5 59.8 56.9 62.4 63.3 62.8 62.3 61.9 65.1 59.4 ...
## $ table : num [1:53940] 55 61 65 58 58 57 57 55 61 61 ...
## $ price : int [1:53940] 326 326 327 334 335 336 336 337 337 338 ...
## $ x : num [1:53940] 3.95 3.89 4.05 4.2 4.34 3.94 3.95 4.07 3.87 4 ...
## $ y : num [1:53940] 3.98 3.84 4.07 4.23 4.35 3.96 3.98 4.11 3.78 4.05 ...
## $ z : num [1:53940] 2.43 2.31 2.31 2.63 2.75 2.48 2.47 2.53 2.49 2.39 ...
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
##
There are 53940 values and 10 variables in this dataset.
The descriptions of the variables in this dataset are:
Carat: weight of the diamond
Cut: quality of the cut
Color: color of the diamond
Clarity: measurement of how clear the diamond is (I1 worst, SI2, SI1,
VS2, VS1, VVS2, VVS1, IF best)
Depth: total depth percentage
Table: width of top of diamond relative to widest point
Price: Price of the diamond in US dollars
X: length in mm
Y: width in mm
Z: depth in mm
The documentation of the variables and their summary statistics are pretty important to get started to perform numerical analysis on the data. The code “str(diamonds)” displays the internal structure of each variable in the dataset.
Initially while looking at the numerical summary of the variables in this dataset and the data, the questions I have are:
These questions could be used as the basis to dive further into the data to explore the relationships between variables and I think it would be interesting to see the visualizations of these variables to see if there’s any trends or correlations between them.
diamonds |>
group_by(cut) |>
summarise(mean_depth = mean(depth))
## # A tibble: 5 × 2
## cut mean_depth
## <ord> <dbl>
## 1 Fair 64.0
## 2 Good 62.4
## 3 Very Good 61.8
## 4 Premium 61.3
## 5 Ideal 61.7
diamonds |>
group_by(carat) |>
summarise(mean_price = mean(price))
## # A tibble: 273 × 2
## carat mean_price
## <dbl> <dbl>
## 1 0.2 365.
## 2 0.21 380.
## 3 0.22 391.
## 4 0.23 486.
## 5 0.24 505.
## 6 0.25 551.
## 7 0.26 551.
## 8 0.27 575.
## 9 0.28 580.
## 10 0.29 601.
## # … with 263 more rows
diamonds |>
group_by(color) |>
summarise(mean_price = mean(price))
## # A tibble: 7 × 2
## color mean_price
## <ord> <dbl>
## 1 D 3170.
## 2 E 3077.
## 3 F 3725.
## 4 G 3999.
## 5 H 4487.
## 6 I 5092.
## 7 J 5324.
diamonds |>
group_by(cut) |>
summarise(mean_price = mean(price))
## # A tibble: 5 × 2
## cut mean_price
## <ord> <dbl>
## 1 Fair 4359.
## 2 Good 3929.
## 3 Very Good 3982.
## 4 Premium 4584.
## 5 Ideal 3458.
In the above aggregation functions, I attempted to dive deeper to explore more about the data to try to answer some of the questions I had above. Typical cut with “fair” quality has the highest average depth. On average, the depth quality decreases as the cut grade increases.
The second aggregation function I wanted to try to answer the question of “What would be the relationship between price and carat of diamonds if any?”. It’s pretty self-explanatory that the price of diamonds would increase as the carat of diamonds increase. The numeric summary of price of diamonds and carat further strengthen that claim since clearly the average price of diamond increases the higher its average carat is.
The last two aggregation functions focused on the question of “What would be the relationship of price of diamonds and color or cut?”. It is easy to tell the relationship of price relative to color and cut without visualizations but I decided to see what I can get from the numerical summaries. Just from a quick look at the average price of diamonds based on its color, the price increases as the color quality of the diamond increases on average. The one interesting that I found is that the price of diamond increase based on the cut quality but there isn’t really a correlation between the two just from the summary (There could still be a correlation/relationship between the two, I would need to explore further using different methods or visualizations). This could also be affected by the carat of the diamond because we found that the higher carat of the diamond is the higher its price so some diamonds with lower quality cut could have high carats which would affect their prices.
diamonds |>
ggplot(aes(x = carat, fill = cut)) +
geom_histogram(binwidth = 0.1, position = "dodge") +
labs(title = "Distribution of Carat by Cut", x = "Carat", y = "Frequency") +
scale_fill_discrete(name = "Cut") +
theme_minimal()
Most of the diamonds are between 0 to 2 carat with ideal cut being the most common cut follow by premium.
diamonds |>
ggplot(aes(color = cut)) +
geom_histogram(binwidth = 100, aes(x=price)) +
labs(title = ("Diamond Price Distribution by Cut"), x = "Diamond Price in $", y = "Frequency") +
theme_minimal() +
facet_wrap(~cut, scales = "free_y")
The distribution of price by cut is as expected since we just saw the visualization of carat by cut and most of the diamonds are between 0 and 2 carat. We also stated previously that price of diamond has a strong positive correlation with its carat so I’m not surprised the distribution of price is heavily right-skewed.
diamonds |>
ggplot(aes(x = cut, y = price, fill = cut)) +
geom_boxplot() +
labs(title = "Diamond Price By Cut", x = "Cut Type", y = "Diamond Price in $") +
theme_minimal() +
coord_cartesian(ylim=c(0,7500))
I just wanted to do a boxplot of the diamond price by cut type to see if it provides better insight than the histogram. I think the boxplot is better to compare diamond price (continuous variable) to its cut quality (categorical variable). Also if I increase the limit on the diamond price there will be a lot more outliers, I chose 7500 in this case to eliminate outliers just for better visualization.
diamonds |>
ggplot(aes(x = clarity, y = price, fill = clarity)) +
geom_boxplot() +
labs(title = "Diamond Price by Clarity", x = "Clarity", y = "Diamond Price in $") +
coord_cartesian(ylim=c(0,7500))
Since I think that boxplot is better for visualization. I decided to compare the diamond price using clarity this time. I decided to keep the same price limit and there’s a wider differences between the clarity types compared to cut types. The mean diamond price by cut type varies a lot less compared to clarity type.