Part 1

Part 2

Part 3

library(ggplot2)
data(diamonds)

# Explore the dataset by displaying the first few rows
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
# Build the linear model
model <- lm(price ~ carat, data = diamonds)
summary(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
# Diagnostic Checks (Residual Plot)
par(mfrow=c(2,2))
plot(model)

# Interpret the coefficient for "carat"
coef_carat <- coef(model)["carat"]
cat("The coefficient for 'carat' is", coef_carat, "\n")
## The coefficient for 'carat' is 7756.426

Interpretation of Coefficients: let’s interpret one of the coefficients, specifically the coefficient for “carat,” which is the slope of the regression line. The coefficient represents the change in the price for a one-unit change in carat weight, holding all other variables constant.

In this model, the coefficient for “carat” represents the estimated change in the diamond price for a one-unit increase in carat weight. For example, if the coefficient is 7756.4, it means that, on average, for each additional carat of weight, the price of a diamond is estimated to increase by $7756.4, assuming all other factors remain the same.

Insights and Significance:

The diagnostic plots are crucial for assessing the model’s assumptions. We use them to check if the residuals are normally distributed, if there’s any heteroscedasticity, and if there are any influential outliers.

The Shapiro-Wilk test checks whether the residuals are normally distributed. A significant p-value suggests that the residuals may not follow a normal distribution, indicating a potential issue with the model’s assumptions.

Further Questions:

Are there any outliers in the data that are affecting the model’s performance?

Are there other variables that could improve the model’s predictive power, or is carat weight sufficient to explain the price of diamonds?

Should we consider transforming the response or explanatory variables to improve model fit?