Does the clarity of a diamond have a significant impact on its price?
library(ggplot2)
mydata <- force(diamonds)
head(mydata)
## # 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
Unit of observation: One diamond
Sample Size: 53940
Variables:
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))
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)
Price: Price in US dollars ($326–$18,823)
x: Length in mm (0–10.74)
y: Width in mm (0–58.9)
z: Depth in mm (0–31.8)
Source: Built in R Studio ggplot2 dataset
Since the sample size is very large, we will take 250 random diamonds for our analysis
set.seed(1)
mydata <- mydata[sample(nrow(mydata), 250), ]
mydata$clarityF <- factor(mydata$clarity,
levels = c("I1", "SI2", "SI1", "VS2", "VS1", "VVS2", "VVS1", "IF"),
labels = c("I1", "SI2", "SI1", "VS2", "VS1", "VVS2", "VVS1", "IF"))
library(psych)
##
## Attaching package: 'psych'
## The following objects are masked from 'package:ggplot2':
##
## %+%, alpha
describeBy(x = mydata$price, group = mydata$clarityF)
##
## Descriptive statistics by group
## group: I1
## vars n mean sd median trimmed mad min max range skew kurtosis
## X1 1 4 7881.75 6903.47 5319.5 7881.75 2553.78 2870 18018 15148 0.66 -1.75
## se
## X1 3451.73
## ------------------------------------------------------------
## group: SI2
## vars n mean sd median trimmed mad min max range skew kurtosis
## X1 1 41 6264.61 5303.33 4583 5615 3328.44 447 18296 17849 1.03 -0.27
## se
## X1 828.24
## ------------------------------------------------------------
## group: SI1
## vars n mean sd median trimmed mad min max range skew kurtosis
## X1 1 62 4453.05 4006.66 4054 3668.7 2549.33 363 18757 18394 1.8 3.14
## se
## X1 508.85
## ------------------------------------------------------------
## group: VS2
## vars n mean sd median trimmed mad min max range skew kurtosis
## X1 1 62 4222.81 4461.77 1992 3394.18 2120.12 449 18020 17571 1.39 1.05
## se
## X1 566.64
## ------------------------------------------------------------
## group: VS1
## vars n mean sd median trimmed mad min max range skew kurtosis
## X1 1 38 4279.71 4380.87 1926 3712.19 1951.1 470 17197 16727 1.13 0.35
## se
## X1 710.67
## ------------------------------------------------------------
## group: VVS2
## vars n mean sd median trimmed mad min max range skew kurtosis
## X1 1 22 3369 4031.13 1386.5 2752.17 1024.48 505 13588 13083 1.26 0
## se
## X1 859.44
## ------------------------------------------------------------
## group: VVS1
## vars n mean sd median trimmed mad min max range skew kurtosis
## X1 1 13 2732.85 4501.67 939 1755 553.01 536 15686 15150 1.98 2.59
## se
## X1 1248.54
## ------------------------------------------------------------
## group: IF
## vars n mean sd median trimmed mad min max range skew kurtosis
## X1 1 8 2244.12 3169.34 1109.5 2244.12 354.34 826 10053 9227 1.82 1.63
## se
## X1 1120.53
summary(mydata)
## carat cut color clarity depth
## Min. :0.2300 Fair : 9 D:32 SI1 :62 Min. :56.90
## 1st Qu.:0.4100 Good :25 E:35 VS2 :62 1st Qu.:61.23
## Median :0.7500 Very Good:60 F:38 SI2 :41 Median :62.00
## Mean :0.8722 Premium :65 G:62 VS1 :38 Mean :62.01
## 3rd Qu.:1.1200 Ideal :91 H:44 VVS2 :22 3rd Qu.:62.70
## Max. :5.0100 I:19 VVS1 :13 Max. :70.20
## J:20 (Other):12
## table price x y
## Min. :52.0 Min. : 363.0 Min. : 0.000 Min. : 0.000
## 1st Qu.:56.0 1st Qu.: 940.5 1st Qu.: 4.725 1st Qu.: 4.750
## Median :57.0 Median : 2757.5 Median : 5.825 Median : 5.830
## Mean :57.3 Mean : 4466.0 Mean : 5.830 Mean : 5.839
## 3rd Qu.:58.0 3rd Qu.: 5615.0 3rd Qu.: 6.705 3rd Qu.: 6.737
## Max. :64.0 Max. :18757.0 Max. :10.740 Max. :10.540
##
## z clarityF
## Min. :0.000 SI1 :62
## 1st Qu.:2.908 VS2 :62
## Median :3.580 SI2 :41
## Mean :3.603 VS1 :38
## 3rd Qu.:4.117 VVS2 :22
## Max. :6.980 VVS1 :13
## (Other):12
Mean: Based on this sample of 250 diamonds, for the clarity SI2, the average price of a diamond is $6264.61.
Median: Based on this sample of 250 diamonds, for the clarity VVS1, the median is 939, which means that half of the diamonds are more expensive, while the other half are cheaper than 939.
Range: Based on this sample of 250 diamonds, for the clarity VS1, the price range is 16727 dollars. This means that the price goes from $470, for the cheapest, to the 17197 dollars for the most expensive diamond.
Since this data is an independent sample with three or more arithmetic means, we do the one way analysis of variance- ANOVA as a parametric test, and if the assumption of normality is not met, we do the Kruskal-Wallis Rank Sum Test as a non parametric test
Checking the assumptions:
Analysed variable is numeric: This assumption is met, because price is a numeric variable
Variable in the population is normally distributed within each group: We will be doing the Shapiro WIlk Test to check this
H0: The data within the group is normally distributed
H1: The data within the group is not normally distributed
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(rstatix)
## Warning: package 'rstatix' was built under R version 4.3.2
##
## Attaching package: 'rstatix'
## The following object is masked from 'package:stats':
##
## filter
mydata %>%
group_by(clarityF) %>%
shapiro_test(price)
## # A tibble: 8 × 4
## clarityF variable statistic p
## <ord> <chr> <dbl> <dbl>
## 1 I1 price 0.807 0.115
## 2 SI2 price 0.830 0.0000250
## 3 SI1 price 0.784 0.0000000371
## 4 VS2 price 0.794 0.0000000686
## 5 VS1 price 0.815 0.0000213
## 6 VVS2 price 0.703 0.0000216
## 7 VVS1 price 0.544 0.0000224
## 8 IF price 0.502 0.0000108
Based on the above values, we reject H0 for all the groups(at p< 0.001) except I1(as p>0.05), which means all the groups dont have a normal distribution of the variable price except I1- which has a small sample size so we will not look at this finding as significant
Homoscedasticity: the variance of analyzed variable is the same within all groups: We will do the Levene test to check this
H0: The variance of price within the clarity groups I1, SI2, SI1, VS2, VS1, VVS2, VVS1 and IF is the same
H1: At least one group has a different variance
library(car)
## Warning: package 'car' was built under R version 4.3.2
## Loading required package: carData
## Warning: package 'carData' was built under R version 4.3.2
##
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
##
## recode
## The following object is masked from 'package:psych':
##
## logit
leveneTest(mydata$price, group = mydata$clarityF)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 7 0.8005 0.5876
## 242
As p = 0.5876(more than 5%), we cannot reject H0, which implies we cannot reject
that Homoscedasticity is met
library(ggplot2)
ggplot(mydata, aes(x = price)) +
geom_histogram(binwidth = 5000, colour="gray") +
facet_wrap(~clarityF, ncol = 3) +
ylab("Frequency")
There are no significant outliers in this data. The groups that are showing that there could be ones, are having a very small sample size and we will not take them in to account.
Conclusion: As the assumption of normality is not fulfilled, opting for a non-parametric test is appropriate for this sample. However, we will conduct both parametric and non-parametric tests.
Parametric Test (this test will be done, but it is not the best option because the assumption of normality is not met)
H0: Mean(I1) = Mean(SI2) = Mean(SI1) = Mean(VS2) = Mean(VS1) = Mean(VVS2) = Mean(VVS1) = Mean(IF)
H1: At least one mean is different
ANOVA_results <- aov(price ~ clarityF,
data = mydata)
summary(ANOVA_results)
## Df Sum Sq Mean Sq F value Pr(>F)
## clarityF 7 2.893e+08 41331257 2.072 0.0472 *
## Residuals 242 4.826e+09 19943949
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Based on the above values, we can reject H0(p<5%), which implies at least one of the means is statistically different than the others.
library(effectsize)
## Warning: package 'effectsize' was built under R version 4.3.2
##
## Attaching package: 'effectsize'
## The following objects are masked from 'package:rstatix':
##
## cohens_d, eta_squared
## The following object is masked from 'package:psych':
##
## phi
eta_squared(ANOVA_results)
## For one-way between subjects designs, partial eta squared is equivalent
## to eta squared. Returning eta squared.
## # Effect Size for ANOVA
##
## Parameter | Eta2 | 95% CI
## -------------------------------
## clarityF | 0.06 | [0.00, 1.00]
##
## - One-sided CIs: upper bound fixed at [1.00].
interpret_eta_squared(0.02, rules = "cohen1992")
## [1] "small"
## (Rules: cohen1992)
pwc <- mydata %>%
pairwise_t_test(price ~ clarityF,
paired = FALSE,
p.adjust.method = "bonferroni")
ANOVA_results <- anova_test(price ~ clarityF,
data = mydata)
library(rstatix)
pwc <- pwc %>%
add_y_position(fun = "median", step.increase = 0.35)
library(ggpubr)
## Warning: package 'ggpubr' was built under R version 4.3.2
ggboxplot(mydata, x = "clarityF", y = "price", add = "point", ylim=c(0, 70000)) +
stat_pvalue_manual(pwc, hide.ns = FALSE) +
stat_summary(fun = mean, geom = "point", shape = 16, size = 4,
aes(group = clarityF), color = "darkred",
position = position_dodge(width = 0.5)) +
stat_summary(fun = mean, colour = "darkred",
position = position_dodge(width = 0.5),
geom = "text", vjust = -0.2, hjust = -0.2,
aes(label = round(after_stat(y), digits = 2), group = clarityF)) +
labs(subtitle = get_test_label(ANOVA_results, detailed = TRUE),
caption = get_pwc_label(pwc))
Based on the ANOVA results at least one mean is significantly different then the others, so we reject H0 and do the Post-Hoc test in order to see which ones are different.
pairwise.t.test(x = mydata$price, g = mydata$clarityF,
p.adj = "none")
##
## Pairwise comparisons using t tests with pooled SD
##
## data: mydata$price and mydata$clarityF
##
## I1 SI2 SI1 VS2 VS1 VVS2 VVS1
## SI2 0.490 - - - - - -
## SI1 0.138 0.045 - - - - -
## VS2 0.114 0.024 0.774 - - - -
## VS1 0.126 0.050 0.851 0.951 - - -
## VVS2 0.064 0.015 0.329 0.442 0.447 - -
## VVS1 0.045 0.014 0.208 0.275 0.282 0.684 -
## IF 0.040 0.021 0.189 0.239 0.242 0.542 0.808
##
## P value adjustment method: none
pairwise.t.test(x = mydata$price, g = mydata$clarityF,
p.adj = "bonf")
##
## Pairwise comparisons using t tests with pooled SD
##
## data: mydata$price and mydata$clarityF
##
## I1 SI2 SI1 VS2 VS1 VVS2 VVS1
## SI2 1.00 - - - - - -
## SI1 1.00 1.00 - - - - -
## VS2 1.00 0.67 1.00 - - - -
## VS1 1.00 1.00 1.00 1.00 - - -
## VVS2 1.00 0.42 1.00 1.00 1.00 - -
## VVS1 1.00 0.38 1.00 1.00 1.00 1.00 -
## IF 1.00 0.58 1.00 1.00 1.00 1.00 1.00
##
## P value adjustment method: bonferroni
As the assumption of normality is not met, the Kruskal-Wallis test is suitable for this sample
H0: The location distribution of price is the same for all clarity categories
H1: At least one is different
kruskal.test(price ~ clarityF,
data = mydata)
##
## Kruskal-Wallis rank sum test
##
## data: price by clarityF
## Kruskal-Wallis chi-squared = 21.251, df = 7, p-value = 0.003416
As p<0.005, we can reject H0, which means we can reject the statement that the location distribution of price is the same for all the clarity groups
pwc <- mydata %>%
wilcox_test(price ~ clarityF,
paired = FALSE,
p.adjust.method = "bonferroni")
Kruskal_results <- kruskal_test(price ~ clarityF,
data = mydata)
library(rstatix)
pwc <- pwc %>%
add_y_position(fun = "median", step.increase = 0.35)
library(ggpubr)
ggboxplot(mydata, x = "clarityF", y = "price", add = "point", ylim=c(0, 20000)) +
stat_pvalue_manual(pwc, hide.ns = FALSE) +
stat_summary(fun = median, geom = "point", shape = 16, size = 4,
aes(group = clarityF), color = "darkred",
position = position_dodge(width = 0.8)) +
stat_summary(fun = median, colour = "darkred",
position = position_dodge(width = 0.8),
geom = "text", vjust = -0.5, hjust = -1,
aes(label = round(after_stat(y), digits = 2), group = clarityF)) +
labs(subtitle = get_test_label(Kruskal_results, detailed = TRUE),
caption = get_pwc_label(pwc))
kruskal_effsize(price ~ clarityF,
data = mydata)
## # A tibble: 1 × 5
## .y. n effsize method magnitude
## * <chr> <int> <dbl> <chr> <ord>
## 1 price 250 0.0589 eta2[H] small
library(rstatix)
groups_nonpar <- wilcox_test(price ~ clarityF,
paired = FALSE,
p.adjust.method = "bonferroni",
data = mydata)
groups_nonpar
## # A tibble: 28 × 9
## .y. group1 group2 n1 n2 statistic p p.adj p.adj.signif
## * <chr> <chr> <chr> <int> <int> <dbl> <dbl> <dbl> <chr>
## 1 price I1 SI2 4 41 98 0.551 1 ns
## 2 price I1 SI1 4 62 173 0.192 1 ns
## 3 price I1 VS2 4 62 182 0.122 1 ns
## 4 price I1 VS1 4 38 110 0.157 1 ns
## 5 price I1 VVS2 4 22 72 0.048 1 ns
## 6 price I1 VVS1 4 13 46 0.023 0.636 ns
## 7 price I1 IF 4 8 29 0.028 0.792 ns
## 8 price SI2 SI1 41 62 1504 0.117 1 ns
## 9 price SI2 VS2 41 62 1624 0.018 0.493 ns
## 10 price SI2 VS1 41 38 990 0.038 1 ns
## # ℹ 18 more rows
Based on the Kruskal-Wallis test we rejected H0 which means we rejected the statement that the location distribution of price is the same for all the clarity groups but based on the adjusted p values in the post hoc test(p>5%), we can say there is no statistically significant difference between the location distribution of any of the groups with each other.
Based on the non parametric test(suitable because the assumption of normality is not met), we can say there is no statistically significant impact of a diamond’s clarity on its price on the basis of the post hoc test.