Homework 4
Name: James Johnson
October 25, 2013
(a) What is the expected value of the sample mean? What is the variance of the sample mean? What is the standard deviation of the sample mean?
# $\mu_x$ = 1/(1/10) = 10
# $\sigma_x^2$ = 1/(1/10)^2 = 100
# $\sigma_x$ = squrt(100)/3=10^3
(b) Run a simulation by drawing 10,000 random samples, each of size 30, from Exp(λ = 1/10 ) and then 10 compute the mean of each random sample. Store the means of of all the samples in a variable called xbar. Use set.seed(123) as the first line of code in your code chunk. What proportion of the sample means are as large as or larger than 12?
set.seed(123)
N <- 10^4
xbar <- numeric(N)
for (i in 1:N) {
xbar[i] <- mean(rexp(30, 1/10))
}
RS <- sum(xbar >= 12)/N
RS
## [1] 0.1341
# 13.41% of the values are larger than 12
( c ) Graph the simulated sampling distribution of the X ̄’s and shade the area greater than 12. Superimpose a normal distribution with mean and standard deviation equal to that reported in 12a.
library(ggplot2)
DF <- data.frame(xbar = xbar)
ggplot(data = DF, aes(x = xbar)) + geom_density(fill = "green")
ggplot(data = DF, aes(X = xbar)) + geom_density(fill = "green") + stat_function(fun = dnorm,
args = list(mean = 10, sd = sqrt(10/3)))
## Error: stat_density requires the following missing aesthetics: x
(d) Could the values in xbar have come from a normal distribution with mean and standard deviation equal to those reported in 12a? Use a goodness-of-fit test with 10 categories to answer the question.
H_O: The distribution for the mean and stadard deviationequal to those of 12a is normal.
H_A: The distribution for the mean and stadard deviation equal to those of 12ais not normal.
me <- 10
s <- 3.3333
bin <- qnorm(c(0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1), me, s)
table(cut(RS, breaks = bin))
##
## (-Inf,5.73] (5.73,7.19] (7.19,8.25] (8.25,9.16] (9.16,10] (10,10.8]
## 1 0 0 0 0 0
## (10.8,11.7] (11.7,12.8] (12.8,14.3] (14.3, Inf]
## 0 0 0 0
OBS <- as.vector(table(cut(RS, breaks = bin)))
OBS
## [1] 1 0 0 0 0 0 0 0 0 0
x2 <- sum((OBS - 10)^2/10)
x2
## [1] 98.1
pvalue <- 1 - pchisq(48.1, 10)
pvalue
## [1] 5.951e-07
# the pvalue is 5.95 * 10^-7, which means this did not come from a normal
# distribution. So we can reject the null hypothesis, which states that it
# dose followa normal distribution.
(e) Is a mean of 12 unusual for a sample of size 30 from an Exp(λ= 1/10 )?
# It is not unusual to have a mean of 12 for a sample size of 30.
̄ 13. LetX1,X2,…,X10 ∼ N(20,8)andY1,Y2,…,Y15 ∼ N(16,7). Let W =X+Y. (a) Give the exact sampling distribution of W .
# E[bar_X]=20 V[X]=8^2/10=6.4 E[bar_Y]= 16 V[X]=7^2/15=3.267
# W~ N(20 + 16, 3.109)
(b) Simulate the sampling distribution of W in R and plot your results. Specifically, use set.seed(123) and simulate W 10,000 times. Superimpose the theoretical distribution from part 13a over the simulated values of W. Check that the simulated mean and standard error are close to the theoretical mean and standard error. Report the percent error the mean and standard deviation from the simulation of W are from the theoretical mean and standard deviation of W . Are your simulated values for the mean and standard deviation of W both within 2% of the theoretical values for W?
set.seed(123)
N <- 10^4
xbar <- numeric(N)
ybar <- numeric(N)
for (i in 1:N) {
xbar[i] <- mean(rnorm(10, 20, 8))
ybar[i] <- mean(rnorm(15, 16, 7))
}
W = xbar + ybar
mean(W)
## [1] 36.05
# d(W)
# [1] 3.052
DF <- data.frame(W = W)
library(ggplot2)
ggplot(data = DF, aes(x = W)) + geom_density(fill = "green")
ggplot(data = DF, aes(X = W)) + geom_density(fill = "green") + stat_function(fun = dnorm,
args = list(mean = 36, sd = sqrt(29/3)))
## Error: stat_density requires the following missing aesthetics: x
m.error <- ((36.05424 - 36)/36) * 100
m.error
## [1] 0.1507
sd.error <- ((sqrt(29/3) - 3.051807)/sqrt(29/3)) * 100
sd.error
## [1] 1.844
The mean and standard devitaionare within 2% of the theoretical values of W.
( c ) Use your simulation to find P (W < 40). Calculate an exact answer and compare.
pnorm(40, 36.051807, 3.051807)
## [1] 0.9021
THe probability of W being less than 40 is .902. THe probability of W being less than 40 for the exact answer is .900. This means we are more likely for W to be less than 40 with our simulation than we are with the exact answer.