#load the dataset
data("diamonds")
For part 1 of the Data Dive, we’ll use the group_by function to group our dataset into 3 different sets of groups each summarizing a variable. Let us divide our diamonds dataset based on the cut, color and clarity categorial variables, while summarizing the price variable in each of these groups.
We’ll start by dividing our diamonds dataset with the cut variable. According to the diamonds dataset information, the cut variable has 5 levels based on the quality of the cut, namely, Fair, Good, Very Good, Premium and Ideal. Let’s group it based on these levels.
group_cut <- diamonds %>% select(cut, price)
group_cut %>% group_by(cut)
## # A tibble: 53,940 × 2
## # Groups: cut [5]
## cut price
## <ord> <int>
## 1 Ideal 326
## 2 Premium 326
## 3 Good 327
## 4 Premium 334
## 5 Good 335
## 6 Very Good 336
## 7 Very Good 336
## 8 Very Good 337
## 9 Fair 337
## 10 Very Good 338
## # ℹ 53,930 more rows
Now, let’s calculate the expected probability for each group within the cut group.
total_poss <- nrow(group_cut)
group_cut %>% group_by(cut) %>% summarise(n_poss = n(), prob = n_poss/total_poss) %>% arrange(desc(prob))
## # A tibble: 5 × 3
## cut n_poss prob
## <ord> <int> <dbl>
## 1 Ideal 21551 0.400
## 2 Premium 13791 0.256
## 3 Very Good 12082 0.224
## 4 Good 4906 0.0910
## 5 Fair 1610 0.0298
As we can see, the ‘Fair’ cut has the lowest probability of all the cut types, and the ‘Ideal’ cut has the highest probability of all the cut types. This means if we had to randomly choose a diamond based on cut from our diamonds dataset, then we are more likely to get an ‘Ideal’ cut diamond than a ‘Fair’ cut diamond.
Now, let’s divide our diamonds dataset with the color variable. According to the diamonds dataset information, the color variable has 7 levels based on the color of the diamond, from D (best) to J (worst). Let’s group it based on these levels.
group_color <- diamonds %>% select(color, price)
group_color %>% group_by(color)
## # A tibble: 53,940 × 2
## # Groups: color [7]
## color price
## <ord> <int>
## 1 E 326
## 2 E 326
## 3 E 327
## 4 I 334
## 5 J 335
## 6 J 336
## 7 I 336
## 8 H 337
## 9 E 337
## 10 H 338
## # ℹ 53,930 more rows
Now, let’s calculate the expected probability for each group within the color group.
total_poss <- nrow(group_color)
group_color %>% group_by(color) %>% summarise(n_poss = n(), prob = n_poss/total_poss) %>% arrange(desc(prob))
## # A tibble: 7 × 3
## color n_poss prob
## <ord> <int> <dbl>
## 1 G 11292 0.209
## 2 E 9797 0.182
## 3 F 9542 0.177
## 4 H 8304 0.154
## 5 D 6775 0.126
## 6 I 5422 0.101
## 7 J 2808 0.0521
As we can see, the diamond color ‘J’ has the lowest probability of all the color types, and the diamond color ‘G’ has the highest probability of all the color types. This means if we had to randomly choose a diamond based on its color from our diamonds dataset, then we are more likely to get a ‘G’ colored diamond than a ‘J’ colored diamond.
Now, let’s divide our diamonds dataset with the clarity variable. According to the diamonds dataset information, the clarity variable has 8 levels based on the measurement of how clear the diamond is, from I1 (worst), SI2, SI1, VS2, VS1, VVS2, VVS1 to IF (best).
group_clarity <- diamonds %>% select(clarity, price)
group_clarity %>% group_by(clarity)
## # A tibble: 53,940 × 2
## # Groups: clarity [8]
## clarity price
## <ord> <int>
## 1 SI2 326
## 2 SI1 326
## 3 VS1 327
## 4 VS2 334
## 5 SI2 335
## 6 VVS2 336
## 7 VVS1 336
## 8 SI1 337
## 9 VS2 337
## 10 VS1 338
## # ℹ 53,930 more rows
Now, let’s calculate the expected probability for each group within the clarity group.
total_poss <- nrow(group_clarity)
group_clarity %>% group_by(clarity) %>% summarise(n_poss = n(), prob = n_poss/total_poss) %>% arrange(desc(prob))
## # A tibble: 8 × 3
## clarity n_poss prob
## <ord> <int> <dbl>
## 1 SI1 13065 0.242
## 2 VS2 12258 0.227
## 3 SI2 9194 0.170
## 4 VS1 8171 0.151
## 5 VVS2 5066 0.0939
## 6 VVS1 3655 0.0678
## 7 IF 1790 0.0332
## 8 I1 741 0.0137
As we can see, the diamond clarity ‘I1’ has the lowest probability of all the clarity types, and the diamond clarity ‘SI1’ has the highest probability of all the clarity types. This means if we had to randomly choose a diamond based on its clarity from our diamonds dataset, then we are more likely to get a ‘SI1’ calrity diamond than a ‘I1’ calrity diamond.
Now, let’s reflect the anomalies that we found in our diamonds dataset by creating a duplicate dataset.
diamonds2 <- diamonds %>% mutate(prob = case_when(cut == 'Fair' | color == 'J' | clarity == 'I1' ~ 'low', cut == 'Ideal' | color == 'G' | clarity == 'SI1' ~ 'high'))
We could test the hypothesis of why some groups are rare by using the price variable as a possible factor as the price highly influences the sale of a diamond.
Coming to visualizatons, we can visualize each group based on the rarity and the probablities of all the groups within. This would give us a better understanding of how rare these each individual groups are within the three main groups.
Let’s use the same three categorical variables that we have used for part 1 - cut, color and clarity.
diamonds %>% group_by(cut, color, clarity) %>% count() %>% arrange(desc(n))
## # A tibble: 276 × 4
## # Groups: cut, color, clarity [276]
## cut color clarity n
## <ord> <ord> <ord> <int>
## 1 Ideal E VS2 1136
## 2 Ideal G VS1 953
## 3 Ideal D VS2 920
## 4 Ideal G VS2 910
## 5 Ideal F VS2 879
## 6 Ideal G VVS2 774
## 7 Ideal E SI1 766
## 8 Ideal H SI1 763
## 9 Ideal D SI1 738
## 10 Premium G VS2 721
## # ℹ 266 more rows
diamonds %>% group_by(cut, color, clarity) %>% count()
## # A tibble: 276 × 4
## # Groups: cut, color, clarity [276]
## cut color clarity n
## <ord> <ord> <ord> <int>
## 1 Fair D I1 4
## 2 Fair D SI2 56
## 3 Fair D SI1 58
## 4 Fair D VS2 25
## 5 Fair D VS1 5
## 6 Fair D VVS2 9
## 7 Fair D VVS1 3
## 8 Fair D IF 3
## 9 Fair E I1 9
## 10 Fair E SI2 78
## # ℹ 266 more rows
For the categorical variables that I’ve selected in the diamonds dataset, almost all possible combinations show up. From the above data, it seems like the combinations between the cut ‘Fair’, colors ‘I’/‘J’ and the cuts ‘VVS1’/‘VVS2’ seem to be very rare.