library(ggplot2)
data("diamonds")

Part 1

# Load the required package
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
# Assuming you have the Diamonds dataset loaded
# You can load the dataset using the following command:
# diamonds <- ggplot2::diamonds

# Select the "price" column as the response variable
response_variable <- diamonds %>%
  select(price)

# View the first few rows of the response variable
head(response_variable)
## # A tibble: 6 × 1
##   price
##   <int>
## 1   326
## 2   326
## 3   327
## 4   334
## 5   335
## 6   336

electing the “price” column as the response variable in the Diamonds dataset is significant for understanding diamond pricing. It’s valuable for buyers, sellers, and investors. Key questions for further investigation include factors affecting diamond prices, market trends, geographic variations, consumer preferences, and competitive analysis. This insight is crucial for making informed decisions in the diamond industry.

Part 2

# Assuming you have the Diamonds dataset loaded
# You can load the dataset using the following command:
# diamonds <- ggplot2::diamonds

# Perform ANOVA test
anova_result <- aov(price ~ cut, data = diamonds)

# Summarize the ANOVA results
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

Insight: We tested whether the quality of the diamond’s cut influences its price using ANOVA.

Significance: A low p-value suggests cut quality significantly affects the price, providing crucial information for buyers and sellers.

Further Questions: Investigate which specific cut qualities have the most significant impact on price and how cut quality interacts with other attributes in pricing diamonds.

Part 3

# Assuming you have the Diamonds dataset loaded
# You can load the dataset using the following command:
# diamonds <- ggplot2::diamonds

# Build a linear regression model
linear_model <- lm(price ~ carat, data = diamonds)

# Summary of the regression model
summary(linear_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

The linear regression model for diamond prices with carat weight as the predictor reveals the following insights:

Coefficient of “carat”: It shows how much the price changes for a one-unit increase in carat weight. A positive coefficient suggests that as carat weight goes up, diamond price tends to increase linearly.

Intercept: The intercept isn’t practically meaningful in the context of diamonds since zero carat weight doesn’t exist; it’s mainly a mathematical reference.

R-squared Value: The R-squared value indicates how well carat weight explains the variation in diamond prices. A higher R-squared suggests carat weight explains more price variability.

Hypothesis Testing:

H0: There’s no relationship between carat weight and price (coefficient of carat weight is zero). H1: There is a significant relationship between carat weight and price (non-zero coefficient). Reject H0 if the p-value is less than your significance level (e.g., 0.05). Diagnostic Plots:

These help check model assumptions and detect issues like non-linearity or heteroscedasticity. Recommendations:

The coefficient for carat weight allows you to estimate how price changes with carat weight. However, remember other factors influence diamond prices. To make more precise pricing recommendations, consider interactions with cut, color, and clarity.

Part 4

# Assuming you have the Diamonds dataset loaded
# You can load the dataset using the following command:
# diamonds <- ggplot2::diamonds

# Build a linear regression model with multiple variables and interaction terms
complex_model <- lm(price ~ carat + cut + color + clarity + carat:cut + carat:color + carat:clarity, data = diamonds)

# Summary of the regression model
summary(complex_model)
## 
## Call:
## lm(formula = price ~ carat + cut + color + clarity + carat:cut + 
##     carat:color + carat:clarity, data = diamonds)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -7539.7  -653.2  -124.8   558.9  8767.6 
## 
## Coefficients:
##                  Estimate Std. Error  t value Pr(>|t|)    
## (Intercept)     -3197.730     18.380 -173.982  < 2e-16 ***
## carat            8980.499     19.061  471.133  < 2e-16 ***
## cut.L             367.389     41.387    8.877  < 2e-16 ***
## cut.Q            -286.218     36.088   -7.931 2.21e-15 ***
## cut.C             203.386     30.188    6.737 1.63e-11 ***
## cut^4            -127.540     23.195   -5.499 3.85e-08 ***
## color.L          -607.063     32.331  -18.776  < 2e-16 ***
## color.Q           116.477     30.820    3.779 0.000157 ***
## color.C           -72.772     28.377   -2.564 0.010337 *  
## color^4            63.356     25.515    2.483 0.013027 *  
## color^5           213.850     24.011    8.906  < 2e-16 ***
## color^6             9.141     21.636    0.422 0.672680    
## clarity.L         -29.305     58.399   -0.502 0.615810    
## clarity.Q         573.593     56.184   10.209  < 2e-16 ***
## clarity.C        -989.707     47.662  -20.765  < 2e-16 ***
## clarity^4         656.623     37.548   17.487  < 2e-16 ***
## clarity^5        -510.237     29.762  -17.144  < 2e-16 ***
## clarity^6         207.319     25.354    8.177 2.97e-16 ***
## clarity^7         -62.421     22.082   -2.827 0.004703 ** 
## carat:cut.L       374.107     37.423    9.997  < 2e-16 ***
## carat:cut.Q        56.280     33.134    1.699 0.089405 .  
## carat:cut.C       -69.648     29.271   -2.379 0.017343 *  
## carat:cut^4       186.820     23.857    7.831 4.93e-15 ***
## carat:color.L   -1361.475     31.653  -43.012  < 2e-16 ***
## carat:color.Q    -646.960     30.252  -21.386  < 2e-16 ***
## carat:color.C      32.168     28.572    1.126 0.260231    
## carat:color^4     -34.588     26.501   -1.305 0.191833    
## carat:color^5    -311.501     25.411  -12.259  < 2e-16 ***
## carat:color^6     -25.463     23.630   -1.078 0.281223    
## carat:clarity.L  4831.965     61.437   78.649  < 2e-16 ***
## carat:clarity.Q -1179.967     57.216  -20.623  < 2e-16 ***
## carat:clarity.C  1490.792     51.415   28.995  < 2e-16 ***
## carat:clarity^4  -725.051     44.789  -16.188  < 2e-16 ***
## carat:clarity^5   598.171     38.823   15.408  < 2e-16 ***
## carat:clarity^6  -108.650     33.543   -3.239 0.001200 ** 
## carat:clarity^7   255.707     27.138    9.422  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1033 on 53904 degrees of freedom
## Multiple R-squared:  0.9329, Adjusted R-squared:  0.9329 
## F-statistic: 2.143e+04 on 35 and 53904 DF,  p-value: < 2.2e-16

In the complex regression model for diamond prices:

“carat” represents carat weight (continuous). “cut,” “color,” and “clarity” represent quality (categorical). Interaction terms (e.g., “carat:cut”) account for non-additive effects. Interpretation:

Coefficients reveal the impact of each variable on price. Interaction terms show how carat weight’s effect on price varies based on categorical factors. P-values assess significance, aiding variable selection. This model provides detailed price predictions by considering carat weight, quality, and their interactions. Valuable for understanding diamond pricing intricacies for both buyers and sellers.