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)
The response variable will be price and the explanatory variables will be carat, depth, table, and cut_category.
set_1 = diamonds |>
mutate(cut_cat = cut_width(depth, width = 5, boundary = 60)) |>
group_by(cut_cat) |>
summarize(avg_price = mean(price),
avg_carat = mean(carat),
avg_depth = mean(depth),
avg_table = mean(table))
set_1
## # A tibble: 7 × 5
## cut_cat avg_price avg_carat avg_depth avg_table
## <fct> <dbl> <dbl> <dbl> <dbl>
## 1 [40,45] 4148 1.03 43.3 55.3
## 2 (50,55] 2239. 0.683 53.4 63.5
## 3 (55,60] 4468. 0.882 59.1 59.7
## 4 (60,65] 3863. 0.783 62.0 57.2
## 5 (65,70] 4352. 1.08 66.2 57.6
## 6 (70,75] 4575. 1.23 71.1 56.8
## 7 (75,80] 2140 0.677 78.7 66.7
In this set of variable combinations, I’m looking at the average price, carat, depth, and table for different depth categories. The data is grouped by the categorical variable cut_cat and we take a look at the average of each variables in each cut range. I wanted to see how different cut categories can impact the average price and other characteristics of diamonds.
# Map color grades to numeric values
color_numeric <- c("J", "I", "H", "G", "F", "E", "D")
color_values <- 1:7
set_2 <- diamonds |>
mutate(price_per_carat = price / carat,
color_numeric = color_values[match(color,color_numeric)],
color_grade = cut_interval(color_numeric, n=3)) |>
group_by(color_grade) |>
summarize(avg_carat = mean(carat),
avg_price_per_carat = mean(price_per_carat),
avg_depth = mean(depth))
set_2
## # A tibble: 3 × 4
## color_grade avg_carat avg_price_per_carat avg_depth
## <fct> <dbl> <dbl> <dbl>
## 1 [1,3] 0.992 3973. 61.8
## 2 (3,5] 0.755 4150. 61.7
## 3 (5,7] 0.658 3865. 61.7
In this set of variable combinations, carat is the response variable and price_per_carat, color_grade, and depth are the explanatory variables. In this set, I’m looking at the average carat, price per carat, and depth for different color groups. I grouped the data into 3 different groups based on color. I converted the color labels to 1 to 7 and calculated the average for carat, price per carat, and depth within each color group. I wanted to see if color affects the average carat and its price or not. At first glance, color has an impact on carat on average while it doesn’t seem like color has an impact on its average price and depth.
clarity_numeric <- c("I1", "SI2", "SI1", "VS2", "VS1", "VVS2", "VVS1", "IF")
clarity_values <- 1:8
set_3 <- diamonds |>
mutate(volume = x*y*z,
clarity_numeric = clarity_values[match(clarity, clarity_numeric)],
clarity_cat = cut_interval(clarity_numeric, n=7)) |>
group_by(clarity_cat) |>
summarize(avg_cut = mean(as.numeric(cut)),
avg_volume = mean(volume),
avg_price = mean(price))
set_3
## # A tibble: 7 × 4
## clarity_cat avg_cut avg_volume avg_price
## <fct> <dbl> <dbl> <dbl>
## 1 [1,2] 3.62 177. 4978.
## 2 (2,3] 3.75 138. 3996.
## 3 (3,4] 3.98 124. 3925.
## 4 (4,5] 4.00 119. 3839.
## 5 (5,6] 4.12 97.6 3284.
## 6 (6,7] 4.23 82.5 2523.
## 7 (7,8] 4.43 83.3 2865.
In this set of variable combinations, I’m looking at the average cut quality and volume for different clarity groups. The data is grouped based on the clarity. I chose 7 groups because I wanted whole numbers as the range for each clarity group. Then I calculate the average for cut, volume, and price in each group. By looking at the tibble, clarity seems to have a direct impact on all three variables cut, volume, and price. The average cut increases based on the clarity. On the other hand, the average volume and price decrease as the clarity increases.
ggplot(set_1, aes(x = cut_cat, y = avg_price)) +
geom_bar(stat = "identity", fill = "orange") +
labs(x = "Cut Category", y = "Average Price", title = "Average Price by Cut Category") +
theme_minimal()
Based on the cut category, there doesn’t seem to be a direct relationship between cut and its average price. The average price is relatively similar to others with an estimate of around $4000 except around 50 cut and 75+ where the average price drops down to approximately $2250. This could be the potential outlier groups that I could explore further into.
ggplot(set_2, aes(x = color_grade, y = avg_carat, fill = avg_depth)) +
geom_bar(stat = "identity") +
labs(x = "Color Grade", y = "Average Carat", fill = "Average Depth",
title = "Average Carat by Color Grade and Depth") +
scale_fill_viridis_c() +
theme_minimal()
Based on the color grade, there seems to be a direct relationship between color and carat. The average carat goes down as the color increases in its group.
ggplot(set_3, aes(x = clarity_cat, y = avg_cut, fill = avg_volume)) +
geom_bar(stat = "identity") +
labs(x = "Clarity Category", y = "Average Cut", fill = "Average Volume",
title = "Average Cut by Clarity Category and Volume") +
scale_fill_viridis_c() +
theme_minimal()
Based on this bar plot, clarity seems to have a direct impact on average cut. Diamond cut on average increases as the clarity increases. In addition, the average volume tends to go down as the cut increases. In conclusion, there’s a positive relationship between clarity and cut and there’s a negative relationship between volume and cut on average based on the bar graph above.
price_ci <- t.test(diamonds$price, conf.level = 0.95)$conf.int
price_ci
## [1] 3899.132 3966.467
## attr(,"conf.level")
## [1] 0.95
The response variable for set 1 is price so I’m calculating the C.I for price with 95% confidence. With 95% confidence, the true average price of diamond in the population is estimated to fall within the range of [$3899, $3967] based on the dataset. Looking at the box plot I have for set 1, the confidence interval is pretty accurate because most of the bars are around $4200 but there are two categories that lowered the overall range of estimate for price as mentioned above.
carat <- t.test(diamonds$carat, conf.level = 0.95)$conf.int
carat
## [1] 0.7939395 0.8019400
## attr(,"conf.level")
## [1] 0.95
The response variable for set 2 is carat so I’m calculating the C.I for carat with 95% confidence. With 95% confidence, the true average carat weight of diamonds in the dataset is estimated to fall within the range of [0.794, 0.802] based on the dataset. Looking at the box plot I have for set 2, the confidence interval is pretty accurate because most of the bars are within that range but as clarity increases, the weight of diamond tends to go down. However, since most of the diamonds are within the first 3 color grades (from the previous weeks’ data dives), there isn’t enough diamonds to skew the confidence interval down too heavily from the mass.
cut <- t.test(as.numeric(diamonds$cut), conf.level = 0.95)$conf.int
cut
## [1] 3.894674 3.913520
## attr(,"conf.level")
## [1] 0.95
The response variable for set 3 is cut so I’m calculating the C.I for cut with 95% confidence. With 95% confidence, the true average cut of diamonds in the dataset is estimated to fall within the range of [3.895, 3.914] based on the dataset. Since the cut variable is categorical, I have to convert it into numeric values. Looking at the box plot I have for set 3, the confidence interval is pretty accurate because most of the bars are within the range of 3.5 to 4. However, a numeric value for cut could be difficult to understand because the cut quality is originally a categorical variable so a numerical value for cut quality could be harder to understand.