library(ggplot2)
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(tidyr)
library(stats)
diamonds <- read.csv("diamonds.csv")
head(diamonds)
## X carat cut color clarity depth table price x y z
## 1 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43
## 2 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31
## 3 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
## 4 4 0.29 Premium I VS2 62.4 58 334 4.20 4.23 2.63
## 5 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75
## 6 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48
In this analysis, we use price as our response variable
and cut as our categorical explanatory variable.
We’ll test if there’s a significant difference in the average price across the different cut categories.
# ANOVA Test
anova_result <- aov(price ~ cut, data=diamonds)
summary(anova_result)
## Df Sum Sq Mean Sq F value Pr(>F)
## cut 4 1.104e+10 2.760e+09 175.7 <2e-16 ***
## Residuals 53935 8.474e+11 1.571e+07
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Next, we’ll build a linear regression model using carat
to predict price.
lm_model <- lm(price ~ carat, data=diamonds)
summary(lm_model)
##
## Call:
## lm(formula = price ~ carat, data = diamonds)
##
## Residuals:
## Min 1Q Median 3Q Max
## -18585.3 -804.8 -18.9 537.4 12731.7
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -2256.36 13.06 -172.8 <2e-16 ***
## carat 7756.43 14.07 551.4 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1549 on 53938 degrees of freedom
## Multiple R-squared: 0.8493, Adjusted R-squared: 0.8493
## F-statistic: 3.041e+05 on 1 and 53938 DF, p-value: < 2.2e-16
par(mfrow=c(2,2))
plot(lm_model)
We’ll enhance our model by adding the cut variable and
an interaction term.
lm_with_cut <- lm(price ~ carat + cut + carat:cut, data=diamonds)
summary(lm_with_cut)
##
## Call:
## lm(formula = price ~ carat + cut + carat:cut, data = diamonds)
##
## Residuals:
## Min 1Q Median 3Q Max
## -14878.3 -793.0 -23.0 546.3 12706.2
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1839.07 84.35 -21.802 < 2e-16 ***
## carat 5924.50 72.31 81.933 < 2e-16 ***
## cutGood -583.65 95.77 -6.094 1.11e-09 ***
## cutIdeal -461.30 86.57 -5.329 9.93e-08 ***
## cutPremium -540.83 88.12 -6.137 8.46e-10 ***
## cutVery Good -578.59 88.73 -6.521 7.06e-11 ***
## carat:cutGood 1555.14 86.30 18.021 < 2e-16 ***
## carat:cutIdeal 2267.90 76.05 29.820 < 2e-16 ***
## carat:cutPremium 1883.26 76.43 24.641 < 2e-16 ***
## carat:cutVery Good 2011.48 78.16 25.737 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1498 on 53930 degrees of freedom
## Multiple R-squared: 0.8591, Adjusted R-squared: 0.859
## F-statistic: 3.653e+04 on 9 and 53930 DF, p-value: < 2.2e-16
From the above analysis, we can see how different factors, namely carat and cut, influence the price of diamonds. Both the ANOVA and regression models provide valuable insights into these relationships.