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
library(ggplot2)
library(dplyr)
library(ggrepel)
## Warning: package 'ggrepel' was built under R version 4.2.3
library(effsize)
## Warning: package 'effsize' was built under R version 4.2.3
library(pwrss)
## Warning: package 'pwrss' was built under R version 4.2.3
## 
## Attaching package: 'pwrss'
## 
## The following object is masked from 'package:stats':
## 
##     power.t.test
library(boot)
library(broom)
library(lindia)
## Warning: package 'lindia' was built under R version 4.2.3
library(vcd)
## Warning: package 'vcd' was built under R version 4.2.3
## Loading required package: grid

ANOVA Test

diamonds |>
  ggplot() +
  geom_boxplot(mapping = aes(y = price, x = color)) +
  scale_y_log10(labels = \(x) paste('$', x / 1000, 'K')) +
  annotation_logticks(sides = 'l') +
  labs(x = "Color Of Diamond",
       y = "Log Sales Price (in $1000s)") +
  theme_minimal()

The continuous variable I chose here is the price of diamonds. It’s pretty self explanatory on why prices of diamonds are usually the most value to both buyers and sellers so it’s an easy choice.

The explanatory variable that I expect might influence the response variable (price) is the color of diamonds. From previous data dives, we’ve explored pretty much every relationship between price and other variables so that’s why I chose color here.

My null hypothesis is:

\[ H_0 : \text{average diamond price is equal across all color quality} \]

m <- aov(price ~ color, data = diamonds)
summary(m)
##                Df    Sum Sq   Mean Sq F value Pr(>F)    
## color           6 2.685e+10 4.475e+09   290.2 <2e-16 ***
## Residuals   53933 8.316e+11 1.542e+07                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Given such a small p-value, and such a large dataset, we can conclude that it is very unlikely the means are all equal, and at least one is different from the rest.

Ultimately, there is not enough evidence to conclude that the average diamond price is equal across all color quality, so it would be safe to assume that we can have different average prices for different color qualities.

Linear Regression Model

diamonds |>
  ggplot(aes(x=carat, y=price)) +
  geom_point() +
  labs(x='Carat', y='Price')+
  theme_minimal()

The continuous column of data that might influence the response variable that I chose is carat. The relationship of this variable and the response is roughly linear as shown in the graph above. It’s not as linear as I’d like it to but since there’s not many columns to work with in my dataset and this is the one that is the most ‘linear’ so I chose this.

model <- lm(price ~ carat, diamonds)
model
## 
## Call:
## lm(formula = price ~ carat, data = diamonds)
## 
## Coefficients:
## (Intercept)        carat  
##       -2256         7756
diamonds |>
  ggplot(mapping = aes(x = carat, y = price)) +
  geom_point(size = 1) +
  geom_smooth(method = "lm", se = FALSE, color = 'red') + 
  theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'

tidy(model, conf.int = TRUE)
## # A tibble: 2 × 7
##   term        estimate std.error statistic p.value conf.low conf.high
##   <chr>          <dbl>     <dbl>     <dbl>   <dbl>    <dbl>     <dbl>
## 1 (Intercept)   -2256.      13.1     -173.       0   -2282.    -2231.
## 2 carat          7756.      14.1      551.       0    7729.     7784.
tidy(model, conf.int = TRUE) |>
  ggplot(mapping = aes(x = estimate, y = term)) +
  geom_point() +
  geom_vline(xintercept = 0, linetype = 'dotted', color = 'gray') +
  geom_errorbarh(mapping = aes(xmin = conf.low, xmax = conf.high, 
                               height = 0.5)) +
  labs(title = "Model Coefficient C.I.") +
  theme_minimal()

Since the p-value is at 0, there is sufficient evidence to say that carat has a significant effect on price.

So the coefficients of my models are -2256 and 7756 respectively for the intercept and the slope. Looking at the graph of carat and price with the best fit line, it makes sense that the intercept starts at a negative value in order to get the best fit line. In context of diamonds, it doesn’t make sense to have a 0 carat diamond which would result in negative money. So to safely estimate a price of diamond using carat alone, we would need to have a positive number for carat.

Multivariate Linear Regression Model

model <- lm(price ~ carat + table, data = diamonds)
model$coefficients
## (Intercept)       carat       table 
##  1961.99233  7820.03788   -74.30075

So with the table column accounted for in our model, the intercept for our equation is now positive. It does help in the context that money can’t be negative in this context. This is important because it shows that the table column contributes to help our model to accurately predict the diamond price.