Question
Please help with R-Programming code! Thank you! ```{r, echo=FALSE} reps
Please help with R-Programming code! Thank you!
```{r, echo=FALSE} reps <- 10000 N <- 50 means <- rep(0,reps) #this creates a vector of 10,000 zeros. We'll fill it with means.
for (i in 1:reps) { #this starts a "loop" that will repeat "reps" times incrementing i each time tempdata <- runif(min=10,max=40,n=N) #draws a random sample of size N from a uniform distribution means[i] <- mean(tempdata) #adds the mean of tempdata to the ith entry in our large vector } ```
```{r, echo=FALSE} mean(means) sd(means) hist(means)
```
The "standard error" of a mean can be thought of as the standard deviation of the sampling distribution from which that mean came. The formula for the standard error of a means is $\frac{s}{\sqrt{n}}$.
Verify this for data that comes from a normal distribution with mean = 60, sd = 12 and n = 100. Simulate means from this distribution, then calculate the standard deviation of this collection of simulated means. Compare it to the theoretical standard error given by $std. err.(\bar{x}) =\frac{\sigma}{\sqrt{n}}$ Here is example code that simulates 5 values from a normal distribution:
```{r} smalldata <- rnorm(mean=15,sd=4,n=5) smalldata ```
Check that the Type I error rate of a basic t-test at $\alpha=0.05$ is really 5%. Here's code that generates a p-value when $H_0:\mu_1-\mu_2$ is true:
```{r} group1 <- rnorm(mean=10,sd=2,n=16) group2 <- rnorm(mean=10,sd=2,n=16) t.test(group1,group2)$p.value ```
Simulate drawing 10,000 p-values when $H_0$ is true (doesn't matter what mean and standard deviation and sample size you use). Then check to see what proportion of these p-values are less than 0.05 using "sum(whatever_your_vector_is_named<0.05)/1e4"
Plot a histogram of the p-values you generated. What does this histogram tell you about the distribution of p-values when $H_0$ is true?
Problem 6. Now make the null false by making the means different by 1 standard deviation. Plot another histogram of p-values. How does the distribution of p-values change when $H_0$ is false?
Plot a sampling distribution of the difference in sample means, when population means are equal. You can use code like this to get the difference in sample means for each rep:
```{r} group1 <- rnorm(mean=10,sd=2,n=16) group2 <- rnorm(mean=10,sd=2,n=16) mean(group1)-mean(group2) ```
This distribution ought to look like a normal distribution centered on zero. Does it?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started