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
# Load the "diamonds" dataset
data("diamonds")
# Set a seed for reproducibility
set.seed(123)
# Set up the first set of variables
set1 <- diamonds %>%
mutate(total_price = price * carat)
# Plot the first relationship between total_price and carat
ggplot(set1, aes(x = carat, y = total_price)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
labs(title = "Relationship between Carat and Total Price")
## `geom_smooth()` using formula = 'y ~ x'
# Calculate the correlation coefficient for the first set
correlation_set1 <- cor(set1$carat, set1$total_price)
cat("Correlation for Set 1 (Carat vs. Total Price): ", correlation_set1, "\n")
## Correlation for Set 1 (Carat vs. Total Price): 0.9030313
# Build a confidence interval for the response variable (total_price)
conf_interval_set1 <- t.test(set1$total_price)$conf.int
cat("Confidence Interval for Total Price: [", conf_interval_set1[1], ", ", conf_interval_set1[2], "]\n")
## Confidence Interval for Total Price: [ 4816.83 , 4944.911 ]
# Set up the second set of variables
set2 <- diamonds %>%
mutate(price_per_carat = price / carat)
# Plot the second relationship between price_per_carat and carat
ggplot(set2, aes(x = carat, y = price_per_carat)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
labs(title = "Relationship between Carat and Price per Carat")
## `geom_smooth()` using formula = 'y ~ x'
# Calculate the correlation coefficient for the second set
correlation_set2 <- cor(set2$carat, set2$price_per_carat)
cat("Correlation for Set 2 (Carat vs. Price per Carat): ", correlation_set2, "\n")
## Correlation for Set 2 (Carat vs. Price per Carat): 0.7697439
# Build a confidence interval for the response variable (price_per_carat)
conf_interval_set2 <- t.test(set2$price_per_carat)$conf.int
cat("Confidence Interval for Price per Carat: [", conf_interval_set2[1], ", ", conf_interval_set2[2], "]\n")
## Confidence Interval for Price per Carat: [ 3991.409 , 4025.38 ]
# Set up the third set of variables
set3 <- diamonds %>%
mutate(depth_table_ratio = depth / table)
# Plot the third relationship between depth_table_ratio and depth
ggplot(set3, aes(x = depth, y = depth_table_ratio)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
labs(title = "Relationship between Depth and Depth-Table Ratio")
## `geom_smooth()` using formula = 'y ~ x'
# Calculate the correlation coefficient for the third set
correlation_set3 <- cor(set3$depth, set3$depth_table_ratio)
cat("Correlation for Set 3 (Depth vs. Depth-Table Ratio): ", correlation_set3, "\n")
## Correlation for Set 3 (Depth vs. Depth-Table Ratio): 0.6815994
# Build a confidence interval for the response variable (depth_table_ratio)
conf_interval_set3 <- t.test(set3$depth_table_ratio)$conf.int
cat("Confidence Interval for Depth-Table Ratio: [", conf_interval_set3[1], ", ", conf_interval_set3[2], "]\n")
## Confidence Interval for Depth-Table Ratio: [ 1.076116 , 1.077017 ]
Task 1: Carat vs. Total Price
Insight: Positive linear relationship between carat and total price. Significance: Carat is a strong predictor of total price. Further Questions: What other factors influence diamond price, and can we build a more comprehensive pricing model? Task 2: Carat vs. Price per Carat
Insight: Smaller diamonds have a higher price per carat, and larger diamonds have a lower price per carat. Significance: Valuable for buyers and sellers, suggesting smaller diamonds are better value per carat. Further Questions: What factors contribute to this trend, and does it apply to different diamond qualities or types? Task 3: Depth vs. Depth-Table Ratio
Insight: No strong linear relationship between depth and depth-table ratio. Significance: Depth and ratio seem relatively independent in assessing diamond quality. Further Questions: What are the implications for visual appearance and quality, and are there other factors to consider? Correlation Coefficients
Significance: Quantitative measures of relationships’ strength and direction. Further Questions: Explore factors influencing total price, reasons for the carat-price per carat relationship, and factors affecting depth-table ratio. Confidence Intervals
Significance: Measure uncertainty around population parameters. Further Questions: Understand variability in total price, identify specific factors affecting price per carat, and investigate reasons for variations in depth-table ratio.