In: Statistics and Probability
R Programming Exercise 3.4
From a normal distribution which has a standard deviation of 40 and mean of 10, generate 2 to 600 samples. After generating the samples utilize the plot command to plot the mean of the generated sample (x-axis) against the number of samples (Y-axis). Use proper axis labels. Create a second plot of the density of the 600 samples that you generated.
Use adequate comments to explain your reasoning.
This code can be solved in 4 to 8 lines.
For this problem use the following variables:
For the mean use: Mean_of_data
For Standard Deviation use: Standard_deviation_of_data
mean_used <- 10 # mean provided
std_used <- 40 # standard deviation provided
sample_size <- c(2:600) # Size of each sample
mean_of_data <- rep(0, 599) # variable to hold the mean from the
sample
simulated_data <- data.frame(sample_size, mean_of_data) #
Data frame to hold the sample size and mean of the data
list2 <- c() # Empty list to hold the sample size
set.seed(1001) # To get the same sample each time
# Now we will run a loop to generate 2 to 600 samples and store the
mean in our data frame
for (i in sample_size) {
list2 <- c(list2, rnorm(i, mean_used, std_used))
simulated_data$mean_of_data[i-1] <- mean(rnorm(i, mean_used,
std_used))
}
# Plot Mean on the Xaxis and Sample size on the Yaxis
plot(simulated_data$mean_of_data, simulated_data$sample_size, main
= "Mean of Sample vs Size of Sample",
xlab = "Mean of Sample", ylab = "Size of Sample")
# Calculate the density of the 600 samples
density_of_data <- dnorm(list2, mean_used, std_used)
# Plotting the density
plot(density_of_data)