Make sure that we grade your HW based solely on your R code script. If we don’t see the correct results when we run your code, you will get 0 point for those questions. 1. Create a R function to show the central limit theorem. This function should have the following properties: - In the argument of the function, you have an option to consider poisson, exponential, uniform, normal distributions as the population distribution. - Depending on the choice of the population distribution in part (1), the function will receive extra argument(s) for the parameters of the distribution. For example, if a normal distri- bution is chosen, the mean and SD are needed in the function argument. Note that each distribution has a different parameter setting. - If the distribution is not selected from (“Normal”, “Poisson”, “Uniform”, “Exponential”), the function needs to print the following error message: check the distributional setting: consider ("Normal", "Poisson", "Uniform", "Exponential") and stop. - The function should give the summary statistics (minimum, 1st quartile, median, mean, 3rd quartile, maximum) of 1, 000 sample mean values for given n values (n = 10, 50, 100, 500). - The result should have the following statement at the beginning, for example, if a normal distribution with mean 1 and SD 0.5 was chosen: ‘‘For the Normal distribution, the central limit theorem is tested’’ where the term “Normal” is automatically inserted in the statement based on the argument. And the output should have the following form: For the Normal distribution, the central limit theorem is tested When n=10: Min. 1st Qu. Median Mean 3rd Qu. Max. 0.5187 0.8930 1.0016 0.9993 1.1019 1.4532 When n=50: Min. 1st Qu. Median Mean 3rd Qu. Max. 0.7964 0.9508 1.0010 0.9997 1.0493 1.2309 1 When n=100: Min. 1st Qu. Median Mean 3rd Qu. Max. 0.8534 0.9679 0.9972 0.9992 1.0325 1.1711 When n=500: Min. 1st Qu. Median Mean 3rd Qu. Max. 0.9258 0.9836 1.0006 0.9997 1.0154 1.0678 I Using your own function, test the N(−1,0.52) and the Unif(−3,6) case.
时间: 2024-03-04 17:49:24 浏览: 75
Sure, I can help you with that! Here's the R code for the function that meets the requirements:
```{r}
central_limit_theorem <- function(distribution, ...) {
# check if the selected distribution is valid
if (!(distribution %in% c("Normal", "Poisson", "Uniform", "Exponential"))) {
message("Check the distributional setting: consider ('Normal', 'Poisson', 'Uniform', 'Exponential')")
stop()
}
# create a vector of n values to test
n_values <- c(10, 50, 100, 500)
# create a data frame to store the summary statistics
results <- data.frame(matrix(ncol = length(n_values) + 1, nrow = 6))
# set the column names
colnames(results) <- c("Statistic", paste0("n=", n_values))
# set the row names
rownames(results) <- c("Min.", "1st Qu.", "Median", "Mean", "3rd Qu.", "Max.")
# generate 1000 sample means for each n value
for (i in seq_along(n_values)) {
# set the sample size
n <- n_values[i]
# generate 1000 sample means
sample_means <- replicate(1000, mean(rdist(distribution, n = n, ...)))
# calculate the summary statistics and store them in the results data frame
results[, i + 1] <- round(summary(sample_means), 4)
}
# create the output statement
output_statement <- paste0("For the ", distribution, " distribution, the central limit theorem is tested")
# print the output statement and the summary statistics
cat(output_statement, "\n")
print(results)
}
# create a helper function to generate random numbers from different distributions
rdist <- function(distribution, n, ...) {
switch(distribution,
"Normal" = rnorm(n, ...),
"Poisson" = rpois(n, ...),
"Uniform" = runif(n, ...),
"Exponential" = rexp(n, ...))
}
# test the function with Normal distribution with mean -1 and SD 0.5
central_limit_theorem("Normal", mean = -1, sd = 0.5)
# test the function with Uniform distribution with minimum -3 and maximum 6
central_limit_theorem("Uniform", min = -3, max = 6)
```
Hope this helps! Let me know if you have any questions.
阅读全文