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)
# Define the number of subsamples
num_subsamples <- 5
# Define the size of each subsample (roughly 50% of the data)
subsample_size <- nrow(diamonds) * 0.5
# Create empty data frames to store the subsamples
sample_dfs <- vector("list", length = num_subsamples)
# Create and store the subsamples
for (i in 1:num_subsamples) {
# Sample rows with replacement
sample_indices <- sample(1:nrow(diamonds), size = subsample_size, replace = TRUE)
# Create a new data frame with the sampled data from columns 1 to 9
sample_df <- diamonds[sample_indices,1:10]
# Store the subsample data frame
sample_dfs[[i]] <- sample_df
}
# Analyze and compare the subsamples
for (i in 1:num_subsamples) {
cat("Subsample", i, "Summary and Scatter Plot:\n")
# Calculate summary statistics
summary_stats <- sample_dfs[[i]] %>%
summarise(
Mean_Price = mean(price),
Median_Price = median(price),
Min_Price = min(price),
Max_Price = max(price)
)
# Create a scatter plot
scatter_plot <- ggplot(sample_dfs[[i]], aes(x = carat, y = price)) +
geom_point() +
labs(title = "Scatter Plot of Carat vs. Price")
# Print summary statistics and the scatter plot
print(summary_stats)
print(scatter_plot)
cat("\n")
}
## Subsample 1 Summary and Scatter Plot:
## # A tibble: 1 × 4
## Mean_Price Median_Price Min_Price Max_Price
## <dbl> <dbl> <int> <int>
## 1 3941. 2414 326 18818
##
## Subsample 2 Summary and Scatter Plot:
## # A tibble: 1 × 4
## Mean_Price Median_Price Min_Price Max_Price
## <dbl> <dbl> <int> <int>
## 1 3940. 2398 326 18818
##
## Subsample 3 Summary and Scatter Plot:
## # A tibble: 1 × 4
## Mean_Price Median_Price Min_Price Max_Price
## <dbl> <dbl> <int> <int>
## 1 3925. 2401 326 18823
##
## Subsample 4 Summary and Scatter Plot:
## # A tibble: 1 × 4
## Mean_Price Median_Price Min_Price Max_Price
## <dbl> <dbl> <int> <int>
## 1 3918. 2401 336 18823
##
## Subsample 5 Summary and Scatter Plot:
## # A tibble: 1 × 4
## Mean_Price Median_Price Min_Price Max_Price
## <dbl> <dbl> <int> <int>
## 1 3954. 2442 326 18823
# You can further analyze and compare the subsamples as needed.
Subsampling: We created 5 random subsamples from the diamonds dataset to simulate real-world data collection.
Scrutiny: We analyzed these subsamples by calculating summary statistics and visualizing data distributions.
Variability: Differences in subsamples highlight the natural variability within the dataset.
Impact: Recognizing this variability is crucial for robust data analysis and generalization.
Future Considerations: Future analyses should account for subsample variability, and questions may arise about its underlying causes and statistical methods to mitigate it.