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)
In last week’s data dive, I explored the distribution of price and color. the conclusion was that they have a positive correlation. I wanted to explore deeper into their relationship grouping by their mean price quantiles.
price_cut <- diamonds |>
group_by(cut) |>
summarise(expected_prob=n()/nrow(diamonds),
mean= mean(price),
median=median(price),
Q1= quantile(price,0.25),
Q3= quantile(price,0.75),
IQR = Q3-Q1)
lowest_prob <- price_cut |>
filter(expected_prob == min(expected_prob))
price_cut <- price_cut |>
mutate(tag = ifelse(cut==lowest_prob$cut, "anomaly", ""))
price_cut
## # A tibble: 5 × 8
## cut expected_prob mean median Q1 Q3 IQR tag
## <ord> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>
## 1 Fair 0.0298 4359. 3282 2050. 5206. 3155. "anomaly"
## 2 Good 0.0910 3929. 3050. 1145 5028 3883 ""
## 3 Very Good 0.224 3982. 2648 912 5373. 4461. ""
## 4 Premium 0.256 4584. 3185 1046 6296 5250 ""
## 5 Ideal 0.400 3458. 1810 878 4678. 3800. ""
diamonds |>
ggplot(aes(x=cut,y=price,color=cut))+
geom_boxplot()
From the grouped data, the anomaly group is the ‘fair’ cut which only appears 2.98% in the dataset. It also has the second highest mean, the highest median and Q1. This means that the ‘fair’ cut group has a more densed and higher price on average than the majority of other groups. This is supported by the box plot graph above. Another interesting finding is that the premium group has the highest mean while having the IQR which we could assume that the boxplot should be more dragged out than the other groups. This is also supported by the box plot above.
A potential hypothesis for why the ‘fair’ cut appears the least out of all cut quality is that because since diamonds are rare so people are more careful and more detailed while cutting than other stones. We could test this by gathering more intel on how diamonds are cut using outside resources. We could sample multiple datasets about diamonds with their cut quality and compare the different samples.
Since we explored the relationship between price and cut in the above section, I want to continue with the cut quality of diamonds and compare to another characteristic of diamond which is its color. Somce these two categories are categorical, we can cross-tabulate the count of these two and further express in a relative frequency manner.
freq_table <- diamonds |>
group_by(color,cut) |>
summarize(n=n()) |>
group_by(cut) |>
mutate(freq=n/sum(n)) |>
select(color,cut,freq)
## `summarise()` has grouped output by 'color'. You can override using the
## `.groups` argument.
freq_table |> spread(cut,freq)
## # A tibble: 7 × 6
## color Fair Good `Very Good` Premium Ideal
## <ord> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 D 0.101 0.135 0.125 0.116 0.132
## 2 E 0.139 0.190 0.199 0.169 0.181
## 3 F 0.194 0.185 0.179 0.169 0.178
## 4 G 0.195 0.178 0.190 0.212 0.227
## 5 H 0.188 0.143 0.151 0.171 0.145
## 6 I 0.109 0.106 0.0997 0.104 0.0971
## 7 J 0.0739 0.0626 0.0561 0.0586 0.0416
In the above frequency table, it is hard to find the relationship between diamond cut and color. There’s more diamonds of all quality appear in the colors F,G, and H more than the other colors. To get a better understanding between these two variables, a density graph could be beneficial to see the relationship better.
freq_table |>
ggplot(aes(color,cut)) +
geom_tile(aes(fill=freq*100), color = "white") +
scale_fill_gradient(low="white", high= "purple") +
labs(fill = "Density")
As expected, diamonds of all cut quality appear more in the range of F-H color while there’s fewer count in the color D and I while J has very few diamonds in that color. Something that I missed while looking at the frequency table was that color E also has a similar density to F-H. From the density map, most ideal and premium cuts are from the range E-H while G has the most, most very good and good cut diamonds are from color E, fair cut diamonds appear the most from color F-H, and diamonds are rarely found in color J.
A testable hypothesis for why some groups are rarer than others (group J in this case) would be because color J diamonds are a lot more difficulty to find compare to the other colors. Since I think that diamonds with J rated color are rarer to find than other colors, they should be priced higher due to lower supply so we could use price to find the answer that we need.
Since I explored the relationship between price and cut, cut and color, I wanted to close out by exploring the relationship between diamond’s price and its color. This also potentially helps finding the answer for the hypothesis that we had for the above section.
price_color <- diamonds |>
group_by(color) |>
summarise(expected_prob=n()/nrow(diamonds),
mean= mean(price),
median=median(price),
Q1= quantile(price,0.25),
Q3= quantile(price,0.75),
IQR = Q3-Q1)
lowest_prob <- price_color |>
filter(expected_prob == min(expected_prob))
price_color <- price_color |>
mutate(tag = ifelse(color==lowest_prob$color, "anomaly", ""))
price_color
## # A tibble: 7 × 8
## color expected_prob mean median Q1 Q3 IQR tag
## <ord> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>
## 1 D 0.126 3170. 1838 911 4214. 3302. ""
## 2 E 0.182 3077. 1739 882 4003 3121 ""
## 3 F 0.177 3725. 2344. 982 4868. 3886. ""
## 4 G 0.209 3999. 2242 931 6048 5117 ""
## 5 H 0.154 4487. 3460 984 5980. 4996. ""
## 6 I 0.101 5092. 3730 1120. 7202. 6081. ""
## 7 J 0.0521 5324. 4234 1860. 7695 5834. "anomaly"
diamonds |>
ggplot(aes(x=color,y=price,color=color))+
geom_boxplot()
From the above table, color J appears the least which we already know. However, color I and J have the biggest IQR which means their price distribution will vary more than the other colors. Color H and G have similar IQR values but color H diamonds have higher mean and median values so most diamonds in color G will be lower priced than diamonds with color H. Diamonds with color D, E, and F have similar IQR, mean, and median so their distribution will be similar to each other.
A testable hypothesis is that diamonds become more expensive the higher color quality they are. This can be tested by looking at diamonds grouped by their color and compare their prices.
freq_table <- diamonds |>
group_by(color,cut) |>
summarize(n=n()) |>
group_by(cut) |>
mutate(freq=n/sum(n)) |>
select(color,cut,freq)
## `summarise()` has grouped output by 'color'. You can override using the
## `.groups` argument.
freq_table |> spread(cut,freq)
## # A tibble: 7 × 6
## color Fair Good `Very Good` Premium Ideal
## <ord> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 D 0.101 0.135 0.125 0.116 0.132
## 2 E 0.139 0.190 0.199 0.169 0.181
## 3 F 0.194 0.185 0.179 0.169 0.178
## 4 G 0.195 0.178 0.190 0.212 0.227
## 5 H 0.188 0.143 0.151 0.171 0.145
## 6 I 0.109 0.106 0.0997 0.104 0.0971
## 7 J 0.0739 0.0626 0.0561 0.0586 0.0416
From the above frequency table, there isn’t a combination that never shows up so everything is accounted for. Color J with all cut quality diamonds appear the least common. This could be the case because diamonds with color J are harder to find than other color groups. We find this convincing because their prices are higher than other groups on average from above sections. G-rated diamonds with premium and ideal cut quality appear the most common. This could be because the common diamonds appear the most in the color G and they were cut by experts so their cut quality is usually being premium or ideal.