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
data(diamonds)

Random Samples of Data

random_samples <- lapply(1:10, function(i){
  sample_n(diamonds, size = nrow(diamonds)*0.5, replace = TRUE)
})

The above code create a list of 10 random samples with 50% of the original data with replacement. This is to set us up in order to explore how different these subsamples are and what we can learn about random sampling.

Subsample Exploration

summary_list <- lapply(random_samples, summary)
summary_list
## [[1]]
##      carat               cut        color       clarity         depth      
##  Min.   :0.2000   Fair     :  840   D:3310   SI1    :6554   Min.   :43.00  
##  1st Qu.:0.4000   Good     : 2483   E:4917   VS2    :6114   1st Qu.:61.10  
##  Median :0.7000   Very Good: 5934   F:4746   SI2    :4585   Median :61.80  
##  Mean   :0.7998   Premium  : 7023   G:5648   VS1    :4107   Mean   :61.75  
##  3rd Qu.:1.0400   Ideal    :10690   H:4177   VVS2   :2527   3rd Qu.:62.50  
##  Max.   :5.0100                     I:2736   VVS1   :1841   Max.   :71.80  
##                                     J:1436   (Other):1242                  
##      table           price             x                y         
##  Min.   :43.00   Min.   :  326   Min.   : 0.000   Min.   : 0.000  
##  1st Qu.:56.00   1st Qu.:  946   1st Qu.: 4.710   1st Qu.: 4.720  
##  Median :57.00   Median : 2415   Median : 5.700   Median : 5.710  
##  Mean   :57.47   Mean   : 3942   Mean   : 5.733   Mean   : 5.738  
##  3rd Qu.:59.00   3rd Qu.: 5334   3rd Qu.: 6.540   3rd Qu.: 6.540  
##  Max.   :73.00   Max.   :18823   Max.   :10.740   Max.   :58.900  
##                                                                   
##        z        
##  Min.   : 0.00  
##  1st Qu.: 2.91  
##  Median : 3.53  
##  Mean   : 3.54  
##  3rd Qu.: 4.04  
##  Max.   :31.80  
##                 
## 
## [[2]]
##      carat               cut        color       clarity         depth      
##  Min.   :0.2000   Fair     :  781   D:3394   SI1    :6564   Min.   :43.00  
##  1st Qu.:0.4000   Good     : 2437   E:4891   VS2    :6229   1st Qu.:61.10  
##  Median :0.7000   Very Good: 6036   F:4679   SI2    :4566   Median :61.80  
##  Mean   :0.7979   Premium  : 6936   G:5735   VS1    :4137   Mean   :61.75  
##  3rd Qu.:1.0400   Ideal    :10780   H:4125   VVS2   :2438   3rd Qu.:62.50  
##  Max.   :4.0100                     I:2694   VVS1   :1802   Max.   :79.00  
##                                     J:1452   (Other):1234                  
##      table           price             x                y         
##  Min.   :43.00   Min.   :  326   Min.   : 0.000   Min.   : 0.000  
##  1st Qu.:56.00   1st Qu.:  948   1st Qu.: 4.710   1st Qu.: 4.720  
##  Median :57.00   Median : 2397   Median : 5.700   Median : 5.710  
##  Mean   :57.46   Mean   : 3944   Mean   : 5.731   Mean   : 5.735  
##  3rd Qu.:59.00   3rd Qu.: 5387   3rd Qu.: 6.548   3rd Qu.: 6.540  
##  Max.   :73.00   Max.   :18823   Max.   :10.140   Max.   :58.900  
##                                                                   
##        z        
##  Min.   :0.000  
##  1st Qu.:2.910  
##  Median :3.530  
##  Mean   :3.538  
##  3rd Qu.:4.040  
##  Max.   :8.060  
##                 
## 
## [[3]]
##      carat               cut        color       clarity         depth      
##  Min.   :0.2000   Fair     :  778   D:3433   SI1    :6474   Min.   :43.00  
##  1st Qu.:0.4000   Good     : 2505   E:4861   VS2    :6038   1st Qu.:61.00  
##  Median :0.7000   Very Good: 6058   F:4941   SI2    :4733   Median :61.90  
##  Mean   :0.7964   Premium  : 6896   G:5527   VS1    :4094   Mean   :61.76  
##  3rd Qu.:1.0400   Ideal    :10733   H:4126   VVS2   :2572   3rd Qu.:62.50  
##  Max.   :4.5000                     I:2742   VVS1   :1801   Max.   :79.00  
##                                     J:1340   (Other):1258                  
##      table           price             x                y         
##  Min.   :43.00   Min.   :  337   Min.   : 0.000   Min.   : 0.000  
##  1st Qu.:56.00   1st Qu.:  949   1st Qu.: 4.710   1st Qu.: 4.720  
##  Median :57.00   Median : 2398   Median : 5.690   Median : 5.700  
##  Mean   :57.45   Mean   : 3920   Mean   : 5.727   Mean   : 5.729  
##  3rd Qu.:59.00   3rd Qu.: 5277   3rd Qu.: 6.540   3rd Qu.: 6.530  
##  Max.   :95.00   Max.   :18797   Max.   :10.230   Max.   :10.160  
##                                                                   
##        z         
##  Min.   : 0.000  
##  1st Qu.: 2.910  
##  Median : 3.520  
##  Mean   : 3.537  
##  3rd Qu.: 4.030  
##  Max.   :31.800  
##                  
## 
## [[4]]
##      carat               cut        color       clarity         depth      
##  Min.   :0.2000   Fair     :  833   D:3434   SI1    :6598   Min.   :43.00  
##  1st Qu.:0.4000   Good     : 2451   E:4806   VS2    :6043   1st Qu.:61.00  
##  Median :0.7000   Very Good: 6065   F:4785   SI2    :4578   Median :61.80  
##  Mean   :0.8005   Premium  : 6887   G:5644   VS1    :4121   Mean   :61.75  
##  3rd Qu.:1.0500   Ideal    :10734   H:4184   VVS2   :2549   3rd Qu.:62.50  
##  Max.   :5.0100                     I:2712   VVS1   :1821   Max.   :79.00  
##                                     J:1405   (Other):1260                  
##      table           price             x                y         
##  Min.   :49.00   Min.   :  326   Min.   : 0.000   Min.   : 0.000  
##  1st Qu.:56.00   1st Qu.:  956   1st Qu.: 4.720   1st Qu.: 4.730  
##  Median :57.00   Median : 2431   Median : 5.700   Median : 5.720  
##  Mean   :57.47   Mean   : 3965   Mean   : 5.738   Mean   : 5.743  
##  3rd Qu.:59.00   3rd Qu.: 5364   3rd Qu.: 6.550   3rd Qu.: 6.540  
##  Max.   :79.00   Max.   :18818   Max.   :10.740   Max.   :58.900  
##                                                                   
##        z        
##  Min.   :0.000  
##  1st Qu.:2.920  
##  Median :3.530  
##  Mean   :3.543  
##  3rd Qu.:4.040  
##  Max.   :8.060  
##                 
## 
## [[5]]
##      carat               cut        color       clarity         depth      
##  Min.   :0.2000   Fair     :  741   D:3435   SI1    :6625   Min.   :51.00  
##  1st Qu.:0.4000   Good     : 2487   E:4929   VS2    :6043   1st Qu.:61.00  
##  Median :0.7000   Very Good: 6008   F:4866   SI2    :4656   Median :61.80  
##  Mean   :0.7925   Premium  : 6820   G:5548   VS1    :4085   Mean   :61.74  
##  3rd Qu.:1.0400   Ideal    :10914   H:4105   VVS2   :2498   3rd Qu.:62.50  
##  Max.   :4.1300                     I:2668   VVS1   :1822   Max.   :78.20  
##                                     J:1419   (Other):1241                  
##      table           price             x               y         
##  Min.   :44.00   Min.   :  327   Min.   : 0.00   Min.   : 0.000  
##  1st Qu.:56.00   1st Qu.:  943   1st Qu.: 4.71   1st Qu.: 4.720  
##  Median :57.00   Median : 2368   Median : 5.68   Median : 5.700  
##  Mean   :57.42   Mean   : 3893   Mean   : 5.72   Mean   : 5.726  
##  3rd Qu.:59.00   3rd Qu.: 5292   3rd Qu.: 6.54   3rd Qu.: 6.530  
##  Max.   :95.00   Max.   :18818   Max.   :10.02   Max.   :58.900  
##                                                                  
##        z        
##  Min.   :0.000  
##  1st Qu.:2.910  
##  Median :3.520  
##  Mean   :3.531  
##  3rd Qu.:4.030  
##  Max.   :8.060  
##                 
## 
## [[6]]
##      carat               cut        color       clarity         depth      
##  Min.   :0.2000   Fair     :  854   D:3438   SI1    :6656   Min.   :43.00  
##  1st Qu.:0.4000   Good     : 2377   E:4798   VS2    :6066   1st Qu.:61.00  
##  Median :0.7000   Very Good: 6100   F:4714   SI2    :4620   Median :61.80  
##  Mean   :0.7969   Premium  : 6837   G:5725   VS1    :4087   Mean   :61.76  
##  3rd Qu.:1.0400   Ideal    :10802   H:4135   VVS2   :2560   3rd Qu.:62.50  
##  Max.   :5.0100                     I:2722   VVS1   :1772   Max.   :79.00  
##                                     J:1438   (Other):1209                  
##      table           price             x                y         
##  Min.   :44.00   Min.   :  336   Min.   : 0.000   Min.   : 0.000  
##  1st Qu.:56.00   1st Qu.:  949   1st Qu.: 4.713   1st Qu.: 4.720  
##  Median :57.00   Median : 2398   Median : 5.700   Median : 5.710  
##  Mean   :57.44   Mean   : 3927   Mean   : 5.729   Mean   : 5.734  
##  3rd Qu.:59.00   3rd Qu.: 5330   3rd Qu.: 6.530   3rd Qu.: 6.530  
##  Max.   :79.00   Max.   :18823   Max.   :10.740   Max.   :58.900  
##                                                                   
##        z         
##  Min.   : 0.000  
##  1st Qu.: 2.910  
##  Median : 3.520  
##  Mean   : 3.542  
##  3rd Qu.: 4.030  
##  Max.   :31.800  
##                  
## 
## [[7]]
##      carat               cut        color       clarity         depth      
##  Min.   :0.2000   Fair     :  780   D:3355   SI1    :6409   Min.   :50.80  
##  1st Qu.:0.4000   Good     : 2382   E:4867   VS2    :6159   1st Qu.:61.00  
##  Median :0.7000   Very Good: 5992   F:4800   SI2    :4621   Median :61.80  
##  Mean   :0.7953   Premium  : 7023   G:5655   VS1    :4151   Mean   :61.74  
##  3rd Qu.:1.0400   Ideal    :10793   H:4191   VVS2   :2585   3rd Qu.:62.50  
##  Max.   :4.0100                     I:2742   VVS1   :1772   Max.   :79.00  
##                                     J:1360   (Other):1273                  
##      table           price             x                y         
##  Min.   :43.00   Min.   :  326   Min.   : 0.000   Min.   : 3.680  
##  1st Qu.:56.00   1st Qu.:  945   1st Qu.: 4.710   1st Qu.: 4.720  
##  Median :57.00   Median : 2352   Median : 5.680   Median : 5.690  
##  Mean   :57.44   Mean   : 3901   Mean   : 5.725   Mean   : 5.728  
##  3rd Qu.:59.00   3rd Qu.: 5258   3rd Qu.: 6.540   3rd Qu.: 6.530  
##  Max.   :76.00   Max.   :18823   Max.   :10.020   Max.   :31.800  
##                                                                   
##        z        
##  Min.   :0.000  
##  1st Qu.:2.910  
##  Median :3.510  
##  Mean   :3.532  
##  3rd Qu.:4.030  
##  Max.   :6.310  
##                 
## 
## [[8]]
##      carat               cut        color       clarity         depth      
##  Min.   :0.2000   Fair     :  891   D:3426   SI1    :6517   Min.   :43.00  
##  1st Qu.:0.4000   Good     : 2392   E:4855   VS2    :6186   1st Qu.:61.10  
##  Median :0.7000   Very Good: 6048   F:4706   SI2    :4532   Median :61.80  
##  Mean   :0.7953   Premium  : 6804   G:5637   VS1    :4051   Mean   :61.76  
##  3rd Qu.:1.0400   Ideal    :10835   H:4211   VVS2   :2492   3rd Qu.:62.50  
##  Max.   :4.5000                     I:2695   VVS1   :1849   Max.   :78.20  
##                                     J:1440   (Other):1343                  
##      table           price             x                y        
##  Min.   :50.10   Min.   :  337   Min.   : 0.000   Min.   : 0.00  
##  1st Qu.:56.00   1st Qu.:  949   1st Qu.: 4.713   1st Qu.: 4.72  
##  Median :57.00   Median : 2381   Median : 5.690   Median : 5.70  
##  Mean   :57.45   Mean   : 3903   Mean   : 5.725   Mean   : 5.73  
##  3rd Qu.:59.00   3rd Qu.: 5269   3rd Qu.: 6.540   3rd Qu.: 6.53  
##  Max.   :95.00   Max.   :18804   Max.   :10.230   Max.   :58.90  
##                                                                  
##        z        
##  Min.   :0.000  
##  1st Qu.:2.910  
##  Median :3.520  
##  Mean   :3.535  
##  3rd Qu.:4.030  
##  Max.   :8.060  
##                 
## 
## [[9]]
##      carat               cut        color       clarity         depth      
##  Min.   :0.2000   Fair     :  782   D:3417   SI1    :6562   Min.   :44.00  
##  1st Qu.:0.4000   Good     : 2488   E:4935   VS2    :6219   1st Qu.:61.00  
##  Median :0.7000   Very Good: 6004   F:4899   SI2    :4583   Median :61.80  
##  Mean   :0.7957   Premium  : 6962   G:5511   VS1    :3931   Mean   :61.75  
##  3rd Qu.:1.0400   Ideal    :10734   H:4151   VVS2   :2542   3rd Qu.:62.50  
##  Max.   :5.0100                     I:2702   VVS1   :1867   Max.   :79.00  
##                                     J:1355   (Other):1266                  
##      table           price             x                y         
##  Min.   :44.00   Min.   :  326   Min.   : 0.000   Min.   : 0.000  
##  1st Qu.:56.00   1st Qu.:  956   1st Qu.: 4.720   1st Qu.: 4.730  
##  Median :57.00   Median : 2404   Median : 5.690   Median : 5.710  
##  Mean   :57.45   Mean   : 3925   Mean   : 5.729   Mean   : 5.732  
##  3rd Qu.:59.00   3rd Qu.: 5331   3rd Qu.: 6.540   3rd Qu.: 6.530  
##  Max.   :95.00   Max.   :18823   Max.   :10.740   Max.   :58.900  
##                                                                   
##        z        
##  Min.   :0.000  
##  1st Qu.:2.910  
##  Median :3.520  
##  Mean   :3.536  
##  3rd Qu.:4.030  
##  Max.   :8.060  
##                 
## 
## [[10]]
##      carat               cut        color       clarity         depth      
##  Min.   :0.2000   Fair     :  782   D:3441   SI1    :6540   Min.   :44.00  
##  1st Qu.:0.4000   Good     : 2443   E:4907   VS2    :6175   1st Qu.:61.00  
##  Median :0.7000   Very Good: 6079   F:4697   SI2    :4492   Median :61.80  
##  Mean   :0.7929   Premium  : 6941   G:5709   VS1    :4163   Mean   :61.74  
##  3rd Qu.:1.0400   Ideal    :10725   H:4142   VVS2   :2547   3rd Qu.:62.50  
##  Max.   :4.5000                     I:2712   VVS1   :1794   Max.   :79.00  
##                                     J:1362   (Other):1259                  
##      table           price             x                y         
##  Min.   :43.00   Min.   :  336   Min.   : 0.000   Min.   : 0.000  
##  1st Qu.:56.00   1st Qu.:  954   1st Qu.: 4.710   1st Qu.: 4.720  
##  Median :57.00   Median : 2391   Median : 5.690   Median : 5.710  
##  Mean   :57.46   Mean   : 3900   Mean   : 5.722   Mean   : 5.725  
##  3rd Qu.:59.00   3rd Qu.: 5318   3rd Qu.: 6.540   3rd Qu.: 6.530  
##  Max.   :95.00   Max.   :18806   Max.   :10.230   Max.   :31.800  
##                                                                   
##        z        
##  Min.   :0.000  
##  1st Qu.:2.910  
##  Median :3.520  
##  Mean   :3.532  
##  3rd Qu.:4.030  
##  Max.   :6.720  
## 

The above code shows the summary statistics for all 10 samples that we randomly pulled from the original diamonds dataset. The summary statistics for all 10 samples could be a little difficult to see the real differences between the samples so plots should be a lot easier to visualize and see the differences.

Distribution of Price

#create a 2x5 grid for 10 plots
par(mfrow = c(2, 5))
# Create box plots for each sample in the grid
for (i in 1:10) {
  boxplot(random_samples[[i]]$price, main = paste("Sample", i))
}

for (i in 1:10) {
  boxplot(random_samples[[i]]$price, main = paste("Sample", i), outline = FALSE)
}

The above shows the 10 box plots for the variable Price of each sample. The first graph shows the boxplots with outliers, and the second graph shows the boxplots without the outliers. The first graph was hard to see the differences between the price of each sample so I decided to remove the outliers. Examining the boxplots without the outliers, the boxplots are nearly identical to each other with minimal differences.

Distribution of Carat

par(mfrow=c(2, 5))
for (i in 1:10) {
  density_plot <- density(random_samples[[i]]$carat)
  plot(density_plot, main = paste("Sample", i))
}

for (i in 1:10) {
  hist(random_samples[[i]]$carat, main = paste("Sample", i),
       xlab = "Carat", ylab = "Frequency", col = "lightblue", xlim = c(0,4))
}

Since we didn’t find any meaningful insight by comparing price of diamonds in each sample, I decided to take a look at the variable Carat. From the density plot, it is really hard to see any difference between each sample but sample 9 stood out from the rest of the samples using histogram. Sample 9 is the only histogram that has a unimodal distribution compared to the bimodal distribution from other samples. All 10 distribution of carat looks very similar to each other since most of the data is around 0-2 carat and heavily right-skewed.

Relationship of Cut and Color

Since I didn’t find anything too meaningful when looking at distribution of a single continuous variable, I decided to take a look at some of the topics I’ve looked at last week’s data dive and try to replicate the same thing except with the random samples this time.

freq_table <- list()

for (i in 1:10){
  freq_table[[i]] <- random_samples[[i]] |>
    group_by(color,cut) |>
    summarize(n=n()) |>
    group_by(cut) |>
    mutate(freq=n/sum(n)) |>
    select(color,cut,freq)
}
## `summarise()` has grouped output by 'color'. You can override using the
## `.groups` argument.
## `summarise()` has grouped output by 'color'. You can override using the
## `.groups` argument.
## `summarise()` has grouped output by 'color'. You can override using the
## `.groups` argument.
## `summarise()` has grouped output by 'color'. You can override using the
## `.groups` argument.
## `summarise()` has grouped output by 'color'. You can override using the
## `.groups` argument.
## `summarise()` has grouped output by 'color'. You can override using the
## `.groups` argument.
## `summarise()` has grouped output by 'color'. You can override using the
## `.groups` argument.
## `summarise()` has grouped output by 'color'. You can override using the
## `.groups` argument.
## `summarise()` has grouped output by 'color'. You can override using the
## `.groups` argument.
## `summarise()` has grouped output by 'color'. You can override using the
## `.groups` argument.
for (i in 1:10){
   print(ggplot(freq_table[[i]], aes(x=color, y=cut, fill=freq))+
    geom_tile() +
    labs(title = paste("Heatmap for Sample", i), x= "Color", y= "Cut", fill = "Frequency")+
    theme_minimal())
}

This might be a little bit difficult to compare all 10 graphs at once but I couldn’t figure out a way to group them into one graph like I did previously. However, As expected, diamonds of all cut quality appear more in the range of F-H color while there’s fewer count in the color D and I while J has very few diamonds in that color for all 10 samples. From the density map, most ideal and premium cuts are from the range E-H while G has the most, most very good and good cut diamonds are from color E, fair cut diamonds appear the most from color F-H, and diamonds are rarely found in color J.

Relationship between Price and Color

freq_table <- list()

for (i in 1:10){
  freq_table[[i]] <- random_samples[[i]] |>
    group_by(price,color) |>
    summarize(n=n()) |>
    group_by(color) |>
    mutate(freq=n/sum(n)) |>
    select(color,price,freq)
}
## `summarise()` has grouped output by 'price'. You can override using the
## `.groups` argument.
## `summarise()` has grouped output by 'price'. You can override using the
## `.groups` argument.
## `summarise()` has grouped output by 'price'. You can override using the
## `.groups` argument.
## `summarise()` has grouped output by 'price'. You can override using the
## `.groups` argument.
## `summarise()` has grouped output by 'price'. You can override using the
## `.groups` argument.
## `summarise()` has grouped output by 'price'. You can override using the
## `.groups` argument.
## `summarise()` has grouped output by 'price'. You can override using the
## `.groups` argument.
## `summarise()` has grouped output by 'price'. You can override using the
## `.groups` argument.
## `summarise()` has grouped output by 'price'. You can override using the
## `.groups` argument.
## `summarise()` has grouped output by 'price'. You can override using the
## `.groups` argument.
for (i in 1:10){
   print(ggplot(freq_table[[i]], aes(x=color, y=price, color=color))+
    geom_boxplot() +
    labs(title = paste("Boxplot for Sample", i), x= "Color", y= "Price")+
    theme_minimal())
}

From the above table for all 10 samples, color I and J have the biggest IQR which means their price distribution will vary more than the other colors. Color H and G have similar IQR values but color H diamonds have higher mean and median values so most diamonds in color G will be lower priced than diamonds with color H. Diamonds with color D, E, and F have similar IQR, mean, and median so their distribution will be similar to each other.

Conclusion

After a thorough examination of the subsamples of data, it is evident that there is a high degree of consistency and similarity among all subsamples. Several key observations can be made: