library(tidyverse)
library(Stat2Data)
data("Diamonds")

What are the variable types in this dataset?

glimpse(Diamonds)
## Observations: 351
## Variables: 6
## $ Carat      <dbl> 1.08, 0.31, 0.31, 0.32, 0.33, 0.33, 0.35, 0.35, 0.37,…
## $ Color      <fct> E, F, H, F, D, G, F, F, F, D, E, F, D, D, F, F, D, D,…
## $ Clarity    <fct> VS1, VVS1, VS1, VVS1, IF, VVS1, VS1, VS1, VVS1, IF, V…
## $ Depth      <dbl> 68.6, 61.9, 62.1, 60.8, 60.8, 61.5, 62.5, 62.3, 61.4,…
## $ PricePerCt <dbl> 6693.3, 3159.0, 1755.0, 3159.0, 4758.8, 2895.8, 2457.…
## $ TotalPrice <dbl> 7228.8, 979.3, 544.1, 1010.9, 1570.4, 955.6, 860.0, 8…

What are the levels of the “Clarity” variable in the Diamonds data?

levels(Diamonds$Clarity)
## [1] "IF"   "SI1"  "SI2"  "SI3"  "VS1"  "VS2"  "VVS1" "VVS2"

Fit a model with TotalPrice as the outcome and Clarity as the explanatory variable

lm(TotalPrice ~ Clarity, data = Diamonds)
## 
## Call:
## lm(formula = TotalPrice ~ Clarity, data = Diamonds)
## 
## Coefficients:
## (Intercept)   ClaritySI1   ClaritySI2   ClaritySI3   ClarityVS1  
##     6396.56       -18.42       230.47     -2824.46      1234.23  
##  ClarityVS2  ClarityVVS1  ClarityVVS2  
##     1197.50      2279.81      1171.77

Change the referent category to “SI1” and refit the model

Diamonds <- Diamonds %>%
  mutate(Clarity = fct_relevel(Clarity, c("SI1", "IF", "SI2", "SI3", "VS1", "VS2", "VVS1", "VVS2")))
lm(TotalPrice ~ Clarity, data = Diamonds)
## 
## Call:
## lm(formula = TotalPrice ~ Clarity, data = Diamonds)
## 
## Coefficients:
## (Intercept)    ClarityIF   ClaritySI2   ClaritySI3   ClarityVS1  
##     6378.14        18.42       248.89     -2806.04      1252.65  
##  ClarityVS2  ClarityVVS1  ClarityVVS2  
##     1215.92      2298.23      1190.19

Add the variable “Depth” to your model. How do you interpret the coefficient for this parameter?

lm(TotalPrice ~ Clarity + Depth, data = Diamonds)
## 
## Call:
## lm(formula = TotalPrice ~ Clarity + Depth, data = Diamonds)
## 
## Coefficients:
## (Intercept)    ClarityIF   ClaritySI2   ClaritySI3   ClarityVS1  
##  -1.377e+04   -6.338e+02   -5.031e-01   -2.550e+03    6.044e+02  
##  ClarityVS2  ClarityVVS1  ClarityVVS2        Depth  
##   4.310e+02    1.280e+03    8.399e+02    3.221e+02

A one unit increase in Depth yields an expected increase in Total Price of the diamond of 0.0322 holding Clarity constant.