Research question: Does the average price of diamonds differ between the Fair and Ideal cut quality?
Hypothesis: The average price of diamonds differs between the Fair and Ideal cut quality.
If the assumptions are met:
H0: There is no difference in the average price of diamonds between the Fair and Ideal cut quality.
H1: There is a difference in the average price of diamonds between the Fair and Ideal cut quality.
If the assumptions are not met:
H0: The locations of distributions are the same.
H1: The location of distribution of Fair cut quality and Ideal cut quality diamonds is not the same.
Importing the dataset, which is sourced from the ‘ggplot2’ package in R.
library(ggplot2)
data(diamonds)
head(diamonds)
## # 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
The unit of observation is one diamond. The size of the data set is 53940 units with 10 variables. For this analysis only 100 units will be used as a sample size. Regarding the variables, I will only use cut (categorical) and price (numerical). These two variables, will be renamed into CutQuality and Price.
Description of the variables:
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)
Next step is cleaning my data by selecting only the relevant variables for further analysis and renaming them for easier understanding, as well as reducing the amount of units to 100.
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
mydata <- diamonds[, c(-1,-3,-4,-5,-6,-8,-9,-10)]
colnames(mydata) <- c("CutQuality", "Price")
head(mydata)
## # A tibble: 6 × 2
## CutQuality Price
## <ord> <int>
## 1 Ideal 326
## 2 Premium 326
## 3 Good 327
## 4 Premium 334
## 5 Good 335
## 6 Very Good 336
set.seed(1)
mydata1 <- sample_n(mydata, 100)
head(mydata)
## # A tibble: 6 × 2
## CutQuality Price
## <ord> <int>
## 1 Ideal 326
## 2 Premium 326
## 3 Good 327
## 4 Premium 334
## 5 Good 335
## 6 Very Good 336
Converting Cut Quality variable to a factor, since the two groups from the Cut Quality - Fair and Ideal, will be later on compared by the average price.
mydata1$CutQuality <- as.factor(mydata1$CutQuality)
Subsetting the data for only Fair and Ideal cuts.
mydata2 <- mydata1[mydata1$CutQuality %in% c("Fair", "Ideal"), ]
Descriptive statistics for mydata2 and Price by Cut Quality.
library(psych)
## Warning: package 'psych' was built under R version 4.3.2
##
## Attaching package: 'psych'
## The following objects are masked from 'package:ggplot2':
##
## %+%, alpha
summary(mydata2)
## CutQuality Price
## Fair : 5 Min. : 457
## Good : 0 1st Qu.: 847
## Very Good: 0 Median : 1662
## Premium : 0 Mean : 3340
## Ideal :35 3rd Qu.: 5241
## Max. :14388
describeBy(mydata2$Price, mydata2$CutQuality)
##
## Descriptive statistics by group
## group: Fair
## vars n mean sd median trimmed mad min max range skew kurtosis
## X1 1 5 3497.4 1281.01 3530 3497.4 1673.86 2338 5488 3150 0.48 -1.59
## se
## X1 572.89
## ------------------------------------------------------------
## group: Good
## NULL
## ------------------------------------------------------------
## group: Very Good
## NULL
## ------------------------------------------------------------
## group: Premium
## NULL
## ------------------------------------------------------------
## group: Ideal
## vars n mean sd median trimmed mad min max range skew kurtosis
## X1 1 35 3316.97 3573.05 1221 2746.97 763.54 457 14388 13931 1.35 1
## se
## X1 603.96
The maximum price for Fair cut diamonds is 5488 USD and for Ideal cut diamonds 14388 USD.
Price range for Fair cut diamonds is 3150 USD and for Ideal cut diamonds is 13931 USD.
Mean of Fair cut diamonds is 3497,4 USD and the median is 3530 USD.
Mean of Ideal cut diamonds is 3316,97 USD and the median is 1221 USD.
In the sample of 100 units there is 5 diamonds that are Fair cut and 35 that are Ideal cut.
Before chosing the testing method, the assumptions for the parametric test have to be checked. In case of usage of parametric test the following assumptions need to be met:
The variables are numeric. In this case this assumption is met - price is numeric variable.
The distribution of the variable must be normal in both populations - Fair and Ideal. This assumption needs to be verified. It will be checked through the histograms and the Shapiro-Wilk Test.
The data must come from two independent population. This is valid, we have different populations that are independent - Fair and Ideal cut quality.
Variable has the same variance in both populations – since this assumption is often violated, the Welch correction will be applied.
Firstly, the normality will be checked by drawing a histograms of the distribution of price for Fair and Ideal cut diamonds.
library(ggplot2)
ggplot(mydata2[mydata2$CutQuality == "Fair",], aes(x = Price)) +
geom_histogram(bins = 30, fill = "blue", alpha = 0.7) +
labs(title = "Histogram of Prices for Fair Cut Diamonds",
x = "Price (USD)",
y = "Count") +
theme_minimal()
ggplot(mydata2[mydata2$CutQuality == "Ideal",], aes(x = Price)) +
geom_histogram(bins = 30, fill = "green", alpha = 0.7) +
labs(title = "Histogram of Prices for Ideal Cut Diamonds",
x = "Price (USD)",
y = "Count") +
theme_minimal()
Based on histograms we can see that the assumption of normality is violated, but to make sure, the Shapiro-Wilk test will be performed.
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
mydata2 %>%
group_by(CutQuality) %>%
shapiro_test(Price)
## # A tibble: 2 × 4
## CutQuality variable statistic p
## <ord> <chr> <dbl> <dbl>
## 1 Fair Price 0.888 0.347
## 2 Ideal Price 0.766 0.00000477
H0: The variable (price) is normally distributed.
H1: The variable (price) is not normally distributed.
Based on data we can reject normality assumption for both cut quality groups of diamonds - Fair and Ideal. The normality is violated in both groups, corresponding p-values are in both p<0.001.
Due to the violation of the assumption of normality the non-parametric test need to be made. But due to the educational reasons I will still perform the parametric test.
t.test(mydata2$Price ~ mydata2$CutQuality,
paired = FALSE,
var.equal = FALSE,
alternative = "two.sided")
##
## Welch Two Sample t-test
##
## data: mydata2$Price by mydata2$CutQuality
## t = 0.21675, df = 15.57, p-value = 0.8312
## alternative hypothesis: true difference in means between group Fair and group Ideal is not equal to 0
## 95 percent confidence interval:
## -1588.243 1949.101
## sample estimates:
## mean in group Fair mean in group Ideal
## 3497.400 3316.971
H0: There is no difference in the average price of diamonds between the Fair and Ideal cut quality.
H1: There is a difference in the average price of diamonds between the Fair and Ideal cut quality.
Based on the sample data we cannot reject the null hypothesis. We cannot claim that there are differences in the average price of diamonds between the FFair and Ideal cut quality. .
library(effectsize)
##
## Attaching package: 'effectsize'
## The following objects are masked from 'package:rstatix':
##
## cohens_d, eta_squared
## The following object is masked from 'package:psych':
##
## phi
effectsize::cohens_d(mydata2$Price ~ mydata2$CutQuality,
pooled_sd = FALSE)
## Cohen's d | 95% CI
## -------------------------
## 0.07 | [-0.54, 0.67]
##
## - Estimated using un-pooled SD.
interpret_cohens_d(0.07, rules = "sawilowsky2009")
## [1] "tiny"
## (Rules: sawilowsky2009)
Conclusion: Using the sample data, we can not demonstrate that the average price of Fair and Ideal cut quality diamonds differed (the effect size is tiny, d = 0.07).
The next test is non-parametric, which is also the correct one to use in this case, due to validation of normality.
wilcox.test(mydata2$Price ~ mydata2$CutQuality,
paired = FALSE,
correct = FALSE,
exact = FALSE,
alternative = "two.sided")
##
## Wilcoxon rank sum test
##
## data: mydata2$Price by mydata2$CutQuality
## W = 115, p-value = 0.2607
## alternative hypothesis: true location shift is not equal to 0
H0: The locations of distributions are the same.
H1: The location of distribution of Fair cut quality and Ideal cut quality diamonds is not the same.
Based on the data we cannot reject the null hypothesis, because p-value is more than 5%. We can conclude that there is no difference in average price between Fair and Ideal cut quality of diamonds.
library(effectsize)
effectsize(wilcox.test(mydata2$Price~mydata2$CutQuality,
paired=FALSE,
correct= FALSE,
exact=FALSE,
alternative="two.sided"))
## r (rank biserial) | 95% CI
## ---------------------------------
## 0.31 | [-0.22, 0.70]
interpret_rank_biserial(0.31)
## [1] "large"
## (Rules: funder2019)
Conclusion: Using the sample data, we can not demonstrate that the average price of Fair and Ideal cut quality diamonds differed (the effect size is large, r = 0.31).