Exercise 1: Identifying Issues in Sampling

What’s wrong? Explain what is wrong in each of the following scenarios.

Scenario 1

“The population consists of all individuals selected in a simple random sample.”

Issue:
This statement is incorrect because the population refers to the entire group of individuals that a study is trying to analyze, while a sampleis a subset of that population. Saying that the population consists only of the selected sample confuses these two concepts. The sample is meant to be a representative subset of the population, but it is not the entire population itself.


Scenario 2

“Students in a class are asked to raise their hands if they have cheated on an exam one or more times within the past year.”

Issue:
This method introduces response bias because students may not answer honestly due to external factors like fear of consequences. A better approach would be an anonymous survey to reduce bias.


Scenario 3

“A population of subjects is put in alphabetical order and a simple random sample of size 10 is taken by selecting the first 10 subjects in the list.”

Issue:
This is not a simple random sample because not every individual has an equal chance of being selected. Alphabetical ordering could introduce unintended biases. A true SRS would require randomly selecting subjects using a method like random number generation.


Exercise 2: Bayes’ Theorem Application

# Define probabilities
P_G <- 0.6  # Prior probability of guilt
P_notG <- 0.4  # Probability of innocence
P_C_given_G <- 1  # Probability suspect has characteristic given guilt
P_C_given_notG <- 0.2  # Probability suspect has characteristic given innocence

# Compute P(C)
P_C <- (P_C_given_G * P_G) + (P_C_given_notG * P_notG)

# Compute P(G | C) using Bayes' Theorem
P_G_given_C <- (P_C_given_G * P_G) / P_C

# Output result
cat("Updated Probability of Guilt (P(G | C)):", P_G_given_C)
## Updated Probability of Guilt (P(G | C)): 0.8823529
# **Exercise 3: Modified Rescaling Function**
rescale01 <- function(x) { 
  rng <- range(x, na.rm = TRUE, finite = TRUE)

  # Apply the standard rescaling formula
  x <- (x - rng[1]) / (rng[2] - rng[1])

  # Replace -Inf with 0 and Inf with 1
  x[x == -Inf] <- 0
  x[x == Inf] <- 1
  
  return(x)
}

# Example input vector with -Inf and Inf
x <- c(-5, 0, 5, Inf, -Inf)

# Test the function
cat("Rescaled Values:", rescale01(x))
## Rescaled Values: 0 0.5 1 1 0
# **Exercise 4: Computing Skewness**
skewness <- function(x) {
  n <- length(x)           
  mean_x <- mean(x)        
  sd_x <- sd(x)           
  

  skewness2 <- (1/n) * sum(((x - mean_x) / sd_x)^3)
  
  return(skewness2)
}

# Test the function
set.seed(123)  # Set seed for reproducibility
x <- rnorm(100)  # Generate 100 random numbers from N(0,1)
cat("Skewness:", skewness(x))
## Skewness: 0.05959426