Dataset

For this assignment and all the assigments in the future, I’ll be using the diamonds dataset present in the ggplot2 pacakge. Since our dataset is part of the ggplot2 package, we can use the help() function to know more about the dataset.

help("diamonds")

According to the documentation, diamonds dataset contains the prices and other attributes of almost 54,000 diamonds. The variables are as follows:

price price in US dollars ($326–$18,823)

carat weight of the diamond (0.2–5.01)

cut quality of the cut (Fair, Good, Very Good, Premium, Ideal)

color diamond colour, from D (best) to J (worst)

clarity a measurement of how clear the diamond is (I1 (worst), SI2, SI1, VS2, VS1, VVS2, VVS1, IF (best))

x length in mm (0–10.74)

y width in mm (0–58.9)

z depth in mm (0–31.8)

depth total depth percentage = z / mean(x, y) = 2 * z / (x + y) (43–79)

table width of top of diamond relative to widest point (43–95)

#load the dataset
data("diamonds")

Now that we have succesfully loaded our data, let’s take a look at the dataset. We can use the glimpse() function to get a brief idea of the data that we are dealing with.

Numeric Summary

glimpse(diamonds)
## Rows: 53,940
## Columns: 10
## $ carat   <dbl> 0.23, 0.21, 0.23, 0.29, 0.31, 0.24, 0.24, 0.26, 0.22, 0.23, 0.…
## $ cut     <ord> Ideal, Premium, Good, Premium, Good, Very Good, Very Good, Ver…
## $ color   <ord> E, E, E, I, J, J, I, H, E, H, J, J, F, J, E, E, I, J, J, J, I,…
## $ clarity <ord> SI2, SI1, VS1, VS2, SI2, VVS2, VVS1, SI1, VS2, VS1, SI1, VS1, …
## $ depth   <dbl> 61.5, 59.8, 56.9, 62.4, 63.3, 62.8, 62.3, 61.9, 65.1, 59.4, 64…
## $ table   <dbl> 55, 61, 65, 58, 58, 57, 57, 55, 61, 61, 55, 56, 61, 54, 62, 58…
## $ price   <int> 326, 326, 327, 334, 335, 336, 336, 337, 337, 338, 339, 340, 34…
## $ x       <dbl> 3.95, 3.89, 4.05, 4.20, 4.34, 3.94, 3.95, 4.07, 3.87, 4.00, 4.…
## $ y       <dbl> 3.98, 3.84, 4.07, 4.23, 4.35, 3.96, 3.98, 4.11, 3.78, 4.05, 4.…
## $ z       <dbl> 2.43, 2.31, 2.31, 2.63, 2.75, 2.48, 2.47, 2.53, 2.49, 2.39, 2.…

As we can see, the dataset diamonds has 53,940 rows and 10 columns. We can also see the type of data present in each column. We have 3 categorical variables - cut, color and clarity, and 7 numerical variables - carat, depth, table, price, x, y and z.

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  
## 

Here, we have a some summary statistics of every column in the diamonds dataset. For each numerical column, we have the Minimum, Maximum, 1st Quartile, Median, 3rd Quartile and Mean. And for each categorical variable, we have the different features and the count of each of there features in that category.

For example, if you look at the price variable, we can see that the Min. price is 326 and the Max. is 18823, whereas the mean and median prices are 3933 and 2401 respectively. Similarly, if you look at the cut variable, we can see that there are 5 unique cut types and the total obseravtions for each cut. Looks like the “Ideal” cut has the most number of obseravtions.

For this dataset, I’m interested in finding the relationship between the weight of the diamond (carat), quality of the cut (cut), color, clarity and price of the diamond. I’d also like analyze how those different factors individually and together influence the price of the diamond. We can also extend this to include the dimensions such as the length, width, and depth of the diamond. This would be an interesting analysis as we get to understand the factors that primarily influence the price of diamonds.

Now, let’s use some aggregate functions on our dataset to find some unique and intersting insights.

Let’s see which quality of the cut has the highest avg price for a diamond.

diamonds |> group_by(cut) |> summarise(avg_price = mean(price)) |> arrange(desc(avg_price))
## # A tibble: 5 × 2
##   cut       avg_price
##   <ord>         <dbl>
## 1 Premium       4584.
## 2 Fair          4359.
## 3 Very Good     3982.
## 4 Good          3929.
## 5 Ideal         3458.

We can see that the Premium cut diamonds have the highest price, followed by Fair and Very Good. This tells us that the Premium cut diamonds are more expensive compared to the other cuts. But, for some reason, Fair cut has an higher average price than Very Good. Let’s run some more function to find other factors.

diamonds |> group_by(cut, clarity) |> summarise(avg_price = mean(price)) |> arrange(desc(avg_price))
## `summarise()` has grouped output by 'cut'. You can override using the `.groups`
## argument.
## # A tibble: 40 × 3
## # Groups:   cut [5]
##    cut       clarity avg_price
##    <ord>     <ord>       <dbl>
##  1 Premium   SI2         5546.
##  2 Fair      SI2         5174.
##  3 Very Good SI2         4989.
##  4 Ideal     SI2         4756.
##  5 Good      SI2         4580.
##  6 Premium   VS2         4550.
##  7 Premium   VS1         4485.
##  8 Premium   SI1         4455.
##  9 Very Good IF          4396.
## 10 Ideal     I1          4336.
## # ℹ 30 more rows

We can see that, even though the clarity is the same (SI2) for Fair and Very Good cuts, the average price of Fair cut diamonds is slightly higher than that of Very Good. We’ll find out why in the future assigments as we run more statistics through our dataset.

Visual Summary

Now, let’s find the correlation between the weight of the diamond (carat), quality of the cut (cut), color, clarity and price of the diamond. To do this, we’ll visualize these variables using a scatterplot, with carat and price on the x and y axis, and cut, color and clarity as aesthetics and facets.

ggplot(
  data = diamonds, mapping = aes(x = carat, y = price)
) + geom_point(aes(color = color, shape = cut)) + geom_smooth(method = "lm") + facet_wrap(~clarity)
## `geom_smooth()` using formula = 'y ~ x'

From a quick galnce at the plot, we can notice there is a positive correlation between the price and weight of the diamonds in all the graphs. Also, D, the best diamond color has a higher price than j, the worst diamond color. Similarly, the correlation between the price and weight is more steep in diamonds with best clarity, such as IF, than others, such as I1.

These visualizations provide a brief idea about the relationships between different variables in the data. In the coming data dives, we will do more in-depth aggregrations and visual summaries on the diamonds dataset.