#install.packages(ggplot)
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 200 units will be used as a sample size. Regarding the variables, I will only use carat (categorical) and price (numerical) for the first research question and cut quality (categorical) and cut clarity (categorical) for the second research question.

Description of the variables:

Next step is reducing the amount of units in our sample size to 200 units.

#install.packages("dplyr")
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
set.seed(30)
mydata <- sample_n(diamonds, 200)
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.3  Ideal     G     VS1      61.5    57   776  4.34  4.31  2.66
## 2  2    Premium   H     SI1      58.2    60 18371  8.34  8.26  4.83
## 3  1    Premium   E     SI2      60.4    60  4648  6.49  6.46  3.91
## 4  1    Very Good E     VS1      63.2    53  5914  6.4   6.33  4.02
## 5  1.01 Premium   H     VS2      61.9    62  4559  6.45  6.37  3.97
## 6  0.32 Very Good H     VVS2     61      58   645  4.41  4.44  2.7
#install.packages("psych")
library(psych)
## 
## Attaching package: 'psych'
## The following objects are masked from 'package:ggplot2':
## 
##     %+%, alpha
summary(mydata)
##      carat               cut     color     clarity       depth      
##  Min.   :0.2300   Fair     : 7   D:25   SI1    :57   Min.   :54.20  
##  1st Qu.:0.3800   Good     :13   E:50   VS2    :35   1st Qu.:60.90  
##  Median :0.7000   Very Good:38   F:25   VS1    :32   Median :61.80  
##  Mean   :0.7593   Premium  :63   G:40   VVS2   :31   Mean   :61.65  
##  3rd Qu.:1.0400   Ideal    :79   H:36   SI2    :22   3rd Qu.:62.50  
##  Max.   :2.0300                  I:15   VVS1   :16   Max.   :66.50  
##                                  J: 9   (Other): 7                  
##      table           price               x               y        
##  Min.   :53.00   Min.   :  361.0   Min.   :3.920   Min.   :3.980  
##  1st Qu.:56.00   1st Qu.:  928.2   1st Qu.:4.685   1st Qu.:4.685  
##  Median :58.00   Median : 2455.0   Median :5.660   Median :5.715  
##  Mean   :57.62   Mean   : 3837.2   Mean   :5.654   Mean   :5.658  
##  3rd Qu.:59.00   3rd Qu.: 5373.8   3rd Qu.:6.510   3rd Qu.:6.515  
##  Max.   :65.00   Max.   :18371.0   Max.   :8.340   Max.   :8.260  
##                                                                   
##        z        
##  Min.   :2.400  
##  1st Qu.:2.848  
##  Median :3.545  
##  Mean   :3.487  
##  3rd Qu.:4.020  
##  Max.   :5.010  
## 

RQ1:Is there a correlation between price of diamonds and carat of diamonds?

To use parametric test the following assumptions need to be met:

To see whether the variables are normally distributed or not, I will use Shapiro-Wilk test.

shapiro.test(mydata$price)
## 
##  Shapiro-Wilk normality test
## 
## data:  mydata$price
## W = 0.80861, p-value = 6.199e-15
shapiro.test(mydata$carat)
## 
##  Shapiro-Wilk normality test
## 
## data:  mydata$carat
## W = 0.91037, p-value = 1.212e-09

Hypothesis that we are checking for both variables are:

Based on data we can reject normality assumption for both price and carat of diamonds. The normality is violated in both groups,corresponding p-values are in both cases less than 0.001.

Next, assumption of linearity will be checked by visual representation with the use of scatterplot.

plot(x = mydata$carat, y = mydata$price,
     xlab = "Carat", ylab = "Price",
     main = "Scatterplot of Carat and Price",
     pch = 20, col = "blue")

abline(lm(price ~ carat, data = mydata),
       col = "red", lwd = 2)

From the scatterplot we can see that the assumption of linearity is violated. The units are increasing a bit more progressively.

Due to the violation of normality and linearity, the non-parametric test will be used for the analysis of the correlation - Spearman.

cor(mydata$carat, mydata$price,
    method = "spearman")
## [1] 0.9456413

The relationship between carats and price of diamonds is positive and very strong (r=0.95).

cor.test(mydata$carat, mydata$price,
    method = "spearman")
## Warning in cor.test.default(mydata$carat, mydata$price, method = "spearman"):
## Cannot compute exact p-value with ties
## 
##  Spearman's rank correlation rho
## 
## data:  mydata$carat and mydata$price
## S = 72476, p-value < 2.2e-16
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
##       rho 
## 0.9456413

To check how significant is the correlation coefficient, I will test next hxpothesis:

Based on data we can reject the null hypothesis (p<0.001) and conclude there is a relationship between the two variables.

To answer the first research question which was: Is there a correlation between price of diamonds and carat of diamonds? There exists significant relationship between carats and price of the diamonds (p<0.001) and is very strong (r=0.95).

RQ2: Is there association between clarity and cut quality of diamonds?

To use parametric test next assumptions need to be met:

Because there is a lot of categories in clarity of diamonds and in cut quality variable. I will combine them in categories “low”, “medium”, “high” for both variables.

#install.packages("tidyverse")
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ lubridate 1.9.3     ✔ tibble    3.2.1
## ✔ purrr     1.0.2     ✔ tidyr     1.3.0
## ✔ readr     2.1.5     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ psych::%+%()    masks ggplot2::%+%()
## ✖ psych::alpha()  masks ggplot2::alpha()
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
mydata1 <- mydata %>%
  mutate(cut_quality = case_when(
    cut %in% c("Fair", "Good") ~ "Low",
    cut %in% c("Very Good", "Premium") ~ "Medium",
    cut == "Ideal" ~ "High"),

  d_clarity = case_when(
    clarity %in% c("I1", "SI2", "SI1") ~ "Low",
    clarity %in% c("VS2", "VS1") ~ "Medium",
    clarity %in% c("VVS2", "VVS1", "IF") ~ "High"))

table(mydata1$d_clarity, mydata1$cut_quality)
##         
##          High Low Medium
##   High     30   1     22
##   Low      23  13     44
##   Medium   26   6     35
results <- chisq.test(mydata1$d_clarity, mydata1$cut_quality,
                      correct = FALSE)
results
## 
##  Pearson's Chi-squared test
## 
## data:  mydata1$d_clarity and mydata1$cut_quality
## X-squared = 14.17, df = 4, p-value = 0.006772

Based on the data we can reject the null hypothesis (p=0.007) and conclude that there is an association between cut quality and clarity of diamonds.

Next, observed frequencies will be checked.

addmargins(results$observed)
##                  mydata1$cut_quality
## mydata1$d_clarity High Low Medium Sum
##            High     30   1     22  53
##            Low      23  13     44  80
##            Medium   26   6     35  67
##            Sum      79  20    101 200

In this sample there is 101 diamonds with medium cut quality and 22 diamonds with high clarity. There is only one diamond with low cut quality and high clarity.

Now, we are going to check if there are any of expected frequencies less than 1 or 5 and check how many of them are between 1 and 5.

round(results$expected, 2)
##                  mydata1$cut_quality
## mydata1$d_clarity  High Low Medium
##            High   20.93 5.3  26.76
##            Low    31.60 8.0  40.40
##            Medium 26.46 6.7  33.84

From the table above, we can see that expected frequencies don’t violate the second and the third assumption. All of the expected frequencies are more than 5, which means that the parametric test can be done - Pearson Chi2 test, which has already been performed at the beginning of the analysis.

With the next table, standardised residuals are going to be checked.

round(results$res, 2)
##                  mydata1$cut_quality
## mydata1$d_clarity  High   Low Medium
##            High    1.98 -1.87  -0.92
##            Low    -1.53  1.77   0.57
##            Medium -0.09 -0.27   0.20

Standardized residuals tell us if differences are significant. We can see that only diamonds with high clarity and high cut quality have standardized residual higher than 1.96, more exactly 1.98, which means that it falls outside the critical zone by z-distribution. Concluding, there is more than expected diamonds in the category high clarity and cut quality (alpha=0.05).

In the following tables proportions will be checked.

addmargins(round(prop.table(results$observed), 3))
##                  mydata1$cut_quality
## mydata1$d_clarity  High   Low Medium   Sum
##            High   0.150 0.005  0.110 0.265
##            Low    0.115 0.065  0.220 0.400
##            Medium 0.130 0.030  0.175 0.335
##            Sum    0.395 0.100  0.505 1.000

In this sample there is 6,5% of diamonds which have low clarity and low cut quality.

addmargins(round(prop.table(results$observed, 1), 3), 2) 
##                  mydata1$cut_quality
## mydata1$d_clarity  High   Low Medium   Sum
##            High   0.566 0.019  0.415 1.000
##            Low    0.288 0.162  0.550 1.000
##            Medium 0.388 0.090  0.522 1.000

Out of all diamonds that have medium clarity, 5.22% are medium cut quality.

addmargins(round(prop.table(results$observed, 2), 3), 1) 
##                  mydata1$cut_quality
## mydata1$d_clarity  High   Low Medium
##            High   0.380 0.050  0.218
##            Low    0.291 0.650  0.436
##            Medium 0.329 0.300  0.347
##            Sum    1.000 1.000  1.001

Out of all diamonds that have high cut quality, 3.8% are high clarity.

#install.packages("effectsize")
library(effectsize)
## 
## Attaching package: 'effectsize'
## The following object is masked from 'package:psych':
## 
##     phi
effectsize::cramers_v(mydata1$cut_quality, mydata1$d_clarity)
## Cramer's V (adj.) |       95% CI
## --------------------------------
## 0.16              | [0.00, 1.00]
## 
## - One-sided CIs: upper bound fixed at [1.00].
interpret_cramers_v(0.16)
## [1] "small"
## (Rules: funder2019)

Is there association between clarity and cut quality of diamonds? Based on the data we can reject the null hypothesis (p=0.007) and conclude that there is an significant association between cut quality and clarity of diamonds. The association is small (r=0.16).