# Load the CSV file
data <- read.csv("player_data.csv")
# Set the number of subsamples you want to generate
num_subsamples <- 5

# Initialize an empty list to store subsamples
subsamples <- list()

# Loop to create subsamples
for (i in 1:num_subsamples) {
  # Determine the number of rows for each subsample (roughly 50%)
  num_rows <- round(0.5 * nrow(data))
  
  # Generate random indices with replacement
  sample_indices <- sample(1:nrow(data), num_rows, replace = TRUE)
  
  # Extract data for each subsample from selected columns (columns 1-6, for example)
  subsample <- data[sample_indices, 1:6]
  
  # Store the subsample in the list
  subsamples[[paste("df", i, sep = "_")]] <- subsample
}
# Loop through subsamples for analysis
for (i in 1:num_subsamples) {
  # Extract the current subsample
  current_subsample <- subsamples[[paste("df", i, sep = "_")]]
  
  # Provide data overview
  cat("Subsample", i, "Overview:\n")
  print(summary(current_subsample))
  
  # Visualize data (e.g., histograms, box plots)
  par(mfrow = c(2, 3))  # Create a grid for multiple plots
  for (col in 1:ncol(current_subsample)) {
    # Check if the column is numeric before creating a histogram
    if (is.numeric(current_subsample[, col])) {
      hist(current_subsample[, col], main = paste("Histogram of", colnames(current_subsample)[col]))
      boxplot(current_subsample[, col], main = paste("Boxplot of", colnames(current_subsample)[col]))
    } else {
      cat("Column", colnames(current_subsample)[col], "is not numeric and will not be plotted.\n")
    }
  }
  par(mfrow = c(1, 1))  # Reset the plotting layout

}
## Subsample 1 Overview:
##     Player               From            To           Pos           
##  Length:2490        Min.   :1947   Min.   :1947   Length:2490       
##  Class :character   1st Qu.:1971   1st Qu.:1974   Class :character  
##  Mode  :character   Median :1990   Median :1996   Mode  :character  
##                     Mean   :1988   Mean   :1992                     
##                     3rd Qu.:2007   3rd Qu.:2013                     
##                     Max.   :2022   Max.   :2022                     
##                                                                     
##        Ht              Wt       
##  Min.   : 53.0   Min.   :136.0  
##  1st Qu.: 64.0   1st Qu.:190.0  
##  Median : 66.0   Median :210.0  
##  Mean   :145.2   Mean   :209.1  
##  3rd Qu.: 69.0   3rd Qu.:225.0  
##  Max.   :611.0   Max.   :330.0  
##                  NA's   :2      
## Column Player is not numeric and will not be plotted.
## Column Pos is not numeric and will not be plotted.

## Subsample 2 Overview:
##     Player               From            To           Pos           
##  Length:2490        Min.   :1947   Min.   :1947   Length:2490       
##  Class :character   1st Qu.:1970   1st Qu.:1973   Class :character  
##  Mode  :character   Median :1989   Median :1995   Mode  :character  
##                     Mean   :1988   Mean   :1992                     
##                     3rd Qu.:2008   3rd Qu.:2014                     
##                     Max.   :2022   Max.   :2022                     
##                                                                     
##        Ht              Wt       
##  Min.   : 53.0   Min.   :133.0  
##  1st Qu.: 64.0   1st Qu.:190.0  
##  Median : 67.0   Median :210.0  
##  Mean   :147.8   Mean   :210.1  
##  3rd Qu.: 69.0   3rd Qu.:225.0  
##  Max.   :611.0   Max.   :325.0  
##                  NA's   :4      
## Column Player is not numeric and will not be plotted.
## Column Pos is not numeric and will not be plotted.

## Subsample 3 Overview:
##     Player               From            To           Pos           
##  Length:2490        Min.   :1947   Min.   :1947   Length:2490       
##  Class :character   1st Qu.:1971   1st Qu.:1975   Class :character  
##  Mode  :character   Median :1989   Median :1995   Mode  :character  
##                     Mean   :1988   Mean   :1993                     
##                     3rd Qu.:2008   3rd Qu.:2014                     
##                     Max.   :2022   Max.   :2022                     
##                                                                     
##        Ht              Wt       
##  Min.   : 53.0   Min.   :114.0  
##  1st Qu.: 64.0   1st Qu.:190.0  
##  Median : 66.0   Median :210.0  
##  Mean   :140.5   Mean   :209.3  
##  3rd Qu.: 69.0   3rd Qu.:225.0  
##  Max.   :611.0   Max.   :330.0  
##                  NA's   :2      
## Column Player is not numeric and will not be plotted.
## Column Pos is not numeric and will not be plotted.

## Subsample 4 Overview:
##     Player               From            To           Pos           
##  Length:2490        Min.   :1947   Min.   :1947   Length:2490       
##  Class :character   1st Qu.:1971   1st Qu.:1974   Class :character  
##  Mode  :character   Median :1990   Median :1997   Mode  :character  
##                     Mean   :1989   Mean   :1993                     
##                     3rd Qu.:2010   3rd Qu.:2016                     
##                     Max.   :2022   Max.   :2022                     
##                                                                     
##        Ht              Wt       
##  Min.   : 53.0   Min.   :114.0  
##  1st Qu.: 64.0   1st Qu.:190.0  
##  Median : 66.0   Median :210.0  
##  Mean   :143.2   Mean   :209.6  
##  3rd Qu.: 69.0   3rd Qu.:225.0  
##  Max.   :611.0   Max.   :325.0  
##                  NA's   :1      
## Column Player is not numeric and will not be plotted.
## Column Pos is not numeric and will not be plotted.

## Subsample 5 Overview:
##     Player               From            To           Pos           
##  Length:2490        Min.   :1947   Min.   :1947   Length:2490       
##  Class :character   1st Qu.:1972   1st Qu.:1976   Class :character  
##  Mode  :character   Median :1991   Median :1997   Mode  :character  
##                     Mean   :1990   Mean   :1994                     
##                     3rd Qu.:2009   3rd Qu.:2015                     
##                     Max.   :2022   Max.   :2022                     
##                                                                     
##        Ht              Wt       
##  Min.   : 53.0   Min.   :114.0  
##  1st Qu.: 64.0   1st Qu.:190.0  
##  Median : 66.0   Median :210.0  
##  Mean   :146.9   Mean   :209.9  
##  3rd Qu.: 69.0   3rd Qu.:225.0  
##  Max.   :611.0   Max.   :330.0  
##                  NA's   :1      
## Column Player is not numeric and will not be plotted.
## Column Pos is not numeric and will not be plotted.

# Task: Compare Subsamples
# Compare means of a numeric variable across subsamples
means <- numeric(length = num_subsamples)

for (i in 1:num_subsamples) {
  current_subsample <- subsamples[[paste("df", i, sep = "_")]]
  means[i] <- mean(current_subsample$Wt)
}

# Calculate and print mean comparison statistics
mean_diff <- diff(means)
cat("Mean Differences between Subsamples:\n")
## Mean Differences between Subsamples:
print(mean_diff)
## [1] NA NA NA NA
# Task: Identify Anomalies/Outliers for the "Wt" column
numeric_col <- "Wt"
outliers <- list()

for (i in 1:num_subsamples) {
  current_subsample <- subsamples[[paste("df", i, sep = "_")]]
  variable <- current_subsample[[numeric_col]]
  
  # Remove non-finite values (e.g., NA, Inf) and check if there are valid data points
  valid_variable <- variable[is.finite(variable)]
  
  if (length(valid_variable) > 0) {
    # Calculate Tukey's IQR-based outlier bounds
    Q1 <- quantile(valid_variable, 0.25)
    Q3 <- quantile(valid_variable, 0.75)
    IQR <- Q3 - Q1
    lower_bound <- Q1 - 1.5 * IQR
    upper_bound <- Q3 + 1.5 * IQR
    
    # Identify outliers
    outlier_indices <- which(valid_variable < lower_bound | valid_variable > upper_bound)
    outliers[[i]] <- valid_variable[outlier_indices]
    
    # Visualize outliers with a boxplot
    boxplot(valid_variable, main = paste("Boxplot of", numeric_col))
    if (length(outlier_indices) > 0) {
      points(x = outlier_indices, y = valid_variable[outlier_indices], col = "red", pch = 19)
    }
  } else {
    cat("No valid data points for", numeric_col, "in Subsample", i, "\n")
  }
}

# Print details of outliers
cat("Outliers in", numeric_col, "across Subsamples:\n")
## Outliers in Wt across Subsamples:
for (i in 1:num_subsamples) {
  if (length(outliers[[i]]) > 0) {
    cat("Subsample", i, ":")
    print(outliers[[i]])
  }
}
## Subsample 1 : [1] 280 305 281 280 280 280 136 280 280 281 305 330 284 300 280 285 307 284 325
## [20] 280 310 307
## Subsample 2 : [1] 300 310 133 280 308 280 315 136 310 285 281 280 280 285 307 290 300 325 281
## [20] 289 137 290 303 280 295 280 280
## Subsample 3 : [1] 300 280 308 305 280 280 290 284 284 305 285 114 136 279 280 284 330 284 279
## Subsample 4 : [1] 284 285 280 280 300 305 303 280 290 280 285 136 300 135 114 325 307 285 114
## [20] 279 280 280 284 325
## Subsample 5 : [1] 280 284 114 290 114 305 310 285 285 330 315 289 280 280 290 136 285 325 289
## [20] 279 305 284 280 285 285 315 310 325 280 284
# Task: Analyze Data Consistency for the "CategoricalColumn"
categorical_col <- "Player"
cat_counts <- numeric(length = num_subsamples)

for (i in 1:num_subsamples) {
  current_subsample <- subsamples[[paste("df", i, sep = "_")]]
  unique_values <- unique(current_subsample[[categorical_col]])
  cat_counts[i] <- length(unique_values)
}

# Print the counts of unique values in the categorical column across subsamples
cat("Counts of Unique Values in", categorical_col, "across Subsamples:\n")
## Counts of Unique Values in Player across Subsamples:
print(cat_counts)
## [1] 1939 1943 1952 1943 1937

##Scrutinize these subsamples:

##How different are they? - To quantitatively assess the differences between subsamples, I compared the means of a numeric variable “Wt” across subsamples. - The “Mean Differences between Subsamples” section provides insights into the variability in the data collection process and potential sampling bias.

##What would you have called an anomaly in one sub-sample that you wouldn’t in another? - To identify anomalies or outliers, I analyzed the “Wt” column in each subsample. - One subsample has a wider distribution of weights (Wt), data points and in the other subsample, they were within the expected range.

##Are there aspects of the data that are consistent among all sub-samples? - To assess data consistency, I analyzed the “Player” column, counting the unique values in this categorical column across subsamples. - The “Counts of Unique Values in Player across Subsamples” section provides insights of variations in the players selected across subsamples.

##Consider how this investigation affects how you might draw conclusions about the data in the future: - This investigation highlights the importance of understanding data variability and consistency. - It demonstrates that different sub samples may exhibit variations in means, anomalies, or unique categories. - Future conclusions drawn are the potential impact of sub sampling and the variability it introduces as well as the representatives of sub samples and the potential need for further investigation or adjustments when making conclusions.