#1(A)
# Set parameters
N <- 1000
p <- 0.5
iterations <- 5000
# Initialize vector to store results
odds_ratios <- numeric(iterations)
# Simulate data and calculate odds ratios
for (i in 1:iterations) {
# Generate sample of Bernoulli random variables
sample_data <- rbinom(N, 1, p)
# Calculate sample average
sample_average <- mean(sample_data)
# Calculate odds ratio
odds_ratio <- sample_average / (1 - sample_average)
# Store result
odds_ratios[i] <- odds_ratio
}
# Plot histogram of simulated odds ratios
hist(odds_ratios, freq = FALSE, breaks = 30, main = "Histogram of Simulated Odds Ratios",
xlab = "Odds Ratio", ylab = "Density")
# Calculate mean and variance of odds ratios
mean_odds_ratio <- mean(odds_ratios)
var_odds_ratio <- var(odds_ratios)
# Calculate standard error using Delta method
se_delta_method <- sqrt((1/(1-p)^3)*(p/N))
# Plot normal distribution with mean and standard error
curve(dnorm(x, mean = mean_odds_ratio, sd = se_delta_method),
col = "blue", lwd = 2, add = TRUE)
#1(B)
# Set parameters
N_values <- c(10, 30, 50, 100, 500)
p <- 0.5
iterations <- 5000
# Function to calculate standard error using Delta method
calculate_se_delta_method <- function(p, N) {
return(sqrt((1/(1-p)^3)*(p/N)))
}
# Function to simulate data and plot histogram for given N
simulate_and_plot <- function(N) {
# Initialize vector to store results
odds_ratios <- numeric(iterations)
# Simulate data and calculate odds ratios
for (i in 1:iterations) {
# Generate sample of Bernoulli random variables
sample_data <- rbinom(N, 1, p)
# Calculate sample average
sample_average <- mean(sample_data)
# Calculate odds ratio
odds_ratio <- sample_average / (1 - sample_average)
# Store result
odds_ratios[i] <- odds_ratio
}
# Plot histogram of simulated odds ratios
hist(odds_ratios, freq = FALSE, breaks = 30,
main = paste("Histogram of Simulated Odds Ratios (N =", N, ")"),
xlab = "Odds Ratio", ylab = "Density")
# Calculate mean and variance of odds ratios
mean_odds_ratio <- mean(odds_ratios)
var_odds_ratio <- var(odds_ratios)
# Calculate standard error using Delta method
se_delta_method <- calculate_se_delta_method(p, N)
# Plot normal distribution with mean and standard error
curve(dnorm(x, mean = mean_odds_ratio, sd = se_delta_method),
col = "blue", lwd = 2, add = TRUE)
# Add legend
legend("topright", legend = "Normal Curve", col = "blue", lwd = 2)
}
# Loop over different values of N
for (N in N_values) {
simulate_and_plot(N)
}
#2(A)
# Function to compute g(p)
compute_g <- function(p) {
return(p / (1 - p))
}
# Function to compute delta method approximation for expected value
compute_expected_value <- function(sample_mean) {
return(sample_mean / (1 - sample_mean))
}
# Function to compute delta method approximation for variance
compute_variance <- function(sample_mean, N, p) {
g_prime <- 1 / ((1 - p)^2)
var_x_bar <- p * (1 - p) / N
return((g_prime^2 * var_x_bar) / (p / (1 - p)))
}
# Set the sample sizes and true parameters
sample_sizes <- c(10, 30, 100, 1000)
true_parameters <- c(0.01, 0.1, 0.25, 0.5, 0.75, 0.9, 0.99)
# Store results
results <- data.frame()
# Loop through each simulation configuration
for (N in sample_sizes) {
for (p in true_parameters) {
# Repeat 100 times for each configuration
for (i in 1:100) {
# Step 1: Sample Generation
sample_data <- rbinom(N, 1, p)
# Step 2: Compute Sample Mean
sample_mean <- mean(sample_data)
# Step 3: Calculate g(X_bar)
g_x_bar <- compute_g(sample_mean)
# Step 4: Compute Delta Method Approximation for Expected Value
estimated_expected_value <- compute_expected_value(sample_mean)
# Step 5: Compute Delta Method Approximation for Variance
estimated_variance <- compute_variance(sample_mean, N, p)
# Store results
result <- data.frame(Sample_Size = N,
True_Parameter = p,
Estimated_Expected_Value = estimated_expected_value,
Estimated_Variance = estimated_variance)
results <- rbind(results, result)
}
}
}
# Save results to CSV
write.csv(results, file = "delta_method_results_monday.csv", row.names = FALSE)
#2(A) (Another solution for finding Samples)
# Function to generate samples and compute delta method estimates
generate_samples <- function(sample_sizes, true_parameters) {
# Function to compute g(p)
compute_g <- function(p) {
return(p / (1 - p))
}
# Function to compute delta method approximation for expected value
compute_expected_value <- function(sample_mean) {
return(sample_mean / (1 - sample_mean))
}
# Function to compute delta method approximation for variance
compute_variance <- function(sample_mean, N, p) {
g_prime <- 1 / ((1 - p)^2)
var_x_bar <- p * (1 - p) / N
return((g_prime^2 * var_x_bar) / (p / (1 - p)))
}
# Store results
results <- list()
# Loop through each simulation configuration
for (N in sample_sizes) {
for (p in true_parameters) {
sample_data <- list()
estimates <- list()
# Repeat 100 times for each configuration
for (i in 1:100) {
# Step 1: Sample Generation
sample_data[[i]] <- rbinom(N, 1, p)
# Step 2: Compute Sample Mean
sample_mean <- mean(sample_data[[i]])
# Step 3: Calculate g(X_bar)
g_x_bar <- compute_g(sample_mean)
# Step 4: Compute Delta Method Approximation for Expected Value
estimated_expected_value <- compute_expected_value(sample_mean)
# Step 5: Compute Delta Method Approximation for Variance
estimated_variance <- compute_variance(sample_mean, N, p)
# Store estimates
estimates[[i]] <- list(Estimated_Expected_Value = estimated_expected_value,
Estimated_Variance = estimated_variance)
}
# Store results for this configuration
results[[paste("N_", N, "_p_", p, sep = "")]] <- list(Sample_Data = sample_data,
Estimates = estimates)
}
}
return(results)
}
# Usage:
# Define sample sizes and true parameters
sample_sizes <- c(10, 30, 100, 1000)
true_parameters <- c(0.01, 0.1, 0.25, 0.5, 0.75, 0.9, 0.99)
# Generate samples and compute estimates
samples <- generate_samples(sample_sizes, true_parameters)
# Access samples for a particular configuration
# For example, to access sample data for N = 10 and p = 0.5
sample_data_1 <- samples[["N_10_p_0.5"]][["Sample_Data"]]
sample_data_2 <- samples[["N_30_p_0.5"]][["Sample_Data"]]
sample_data_3<- samples[["N_100_p_0.5"]][["Sample_Data"]]
sample_data_4<- samples[["N_1000_p_0.5"]][["Sample_Data"]]
#sample_data
sample_data_2
[[1]]
[1] 1 0 1 0 0 1 1 0 0 0 0 1 0 0 1 1 0 0 1 1 0 0 1 0 1 1 1 1 1 1
[[2]]
[1] 1 0 1 1 0 0 0 0 0 1 0 1 0 1 0 0 1 1 0 1 1 1 0 1 0 1 1 0 1 1
[[3]]
[1] 1 0 1 0 0 0 1 0 1 1 1 0 1 0 1 1 1 1 1 0 0 0 1 1 0 1 0 0 1 0
[[4]]
[1] 1 0 0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