Solutions - Week 7
Solutions - Week 7
1. (1) Pr (X ≤ 1.5) is given by pnorm(1.5,1,sqrt(.25)). You can get 0.8413447 from R. Note
that we use sqrt(.25) because in R, prnorm() accepts the third value as standard
deviation.
( X−1 ) (1.5−1 )
(2) Give that we know =Z . When X =1.5, Z= =1.0. That is, X =1.5,
√0.25 √ 0.25
metaphorically speaking, has the same probability as Z=1.0
(3) Pr (Z ≤ 1) is given by pnorm(1,0,sqrt(1)), which is 0.8413447. This, as expected, is
the same as the answer of (1).
2. The population from which the sample is drawn is not known to be normally distributed.
By central limit theorem, we know that the sample mean is approximately normally
distributed under larger samples. Using n ≥ 30 as a rule of thumb of large samples, we
got:
( )
4.26−4.18
P ( X ≥ 4.26 ) ≈ P Z ≥ =P ( Z ≥0.67 ),
0.84
√ 50
where the answer from the P ( X ≥ 4.26 ) can be obtained as
1-pnorm(4.26,4.18,0.84/sqrt(50))≈ 0.25; from Q1, we know the same answer can also be
obtained from the standard normal P ( Z ≥ 0.67 ) as 1-pnorm(0.67,0,1)¿0.25.
NOTE 1: You can choose to use either the first command or standard normal distribution
NOTE 2: Importantly, for this and Q3, the “ ≈ (approximately equal to) is meant to
emphasize that the normal distribution under CLM is just an approximation of the
distribution of X —meaning that, under the theorem and supposedly a large sample size,
3. (1)
( )
16−18
P ( X ≥16 )=P Z ≥ =P ( Z ≥−1.85 )
7
√ 42
Either use the R command 1-pnorm(-1.85,0,1)≈ 0.9680. Or use the R command 1-
pnorm(16,18,7/sqrt(42)). Same note as Q1.
( )
16−17.5
(2) P ( X ≤16 )=P Z ≤ =P ( Z ≤−2.03 )
7
√ 90
Use either R command pnorm(-2.03,0,1)≈ 0.0211 or
pnorm(16,17.5,7/sqrt(90))≈ 0.0211. Same caveat as Q1.
4. (1)
> runif(6,0,1)
[no standard answer; you results will differ]
> set.seed(100)
> runif(6,0,1)
0.30776611 0.25767250 0.55232243 0.05638315 0.46854928 0.48377074
Here you can see firsthand the effect of setting a seed value. The first runif command is
executed without setting a specific seed value. Thus, the six numbers are totally
unpredictable. By contrast, the second runif command is under its influence: whoever
runs this runif(6,0,1) after set.seed(100) will get these six numbers.
Note that when a seed value is set, it will “fix” all subsequent commands that generate
random variables (e.g., rnorm(), sample()) in the present R session unless you reset the
seed value.
(2)
> set.seed(50)
> rnorm(3,0,1)
[1] 0.54966989 -0.84160374 0.03299794
> rnorm(3,0,1)
[1] 0.5241497 -1.7276041 -0.2778645
First, notice that while the seed value presets the “randomness” in subsequent
Tutorial Solutions 7 Page 2
commands, it certainly will not “fix” the values from the random variable. This can be
seen by comparing the first three and the next three numbers (they are different).
However, running these three commands will always yield these six numbers.
(3)
> set.seed(50)
> runif(3,0,1)
[1] 0.7087271 0.4376599 0.2000049
> rnorm(3,0,1)
[1] 0.7292185 -1.6985301 0.3754676
Again, comparing the above outputs, we see that the seed value will not make the result of
runif(3,0,1) to be the same as that of rnorm(3,0,1). Comparing the result from rnorm(3,0,1)
and that of the second runif(3,0,1) in part b), we certainly will not expect that those two
commands will yield the same three numbers, even though these two commands are in the
same position after the set.seed command.
But, whoever runs these three commands will always get exactly the same result as shown
above.
(2)
# Think of x as the piggy bank that you keep adding values (i.e., i^2) for 200 times
x<-0
for (i in 1:200){
x=x+i^2 # this reads as “let x equal to (the current value of) x plus i^2
}
x
2686700
But of course, this is not the only way. You can get the same answer (if you are not using
For-loop) by vector multiplication
> x=c(1:200)
> x=x^2
> sum(x)
Tutorial Solutions 7 Page 3
(3)
x<-0
for (i in 100:200){
x=x+i^2
}
x
2358350
Or,
> x=c(100:200)
> x=x^2
> sum(x)
6.
(1) # this “x_bar_3” is used to store the sample mean calculated in each of the 1000
iterations
x_bar_3=rep(0,1000)
set.seed(1)
for (idx in 1:1000) {
x_bar_3 [idx] =mean(rbinom(3,10,0.3))
}
hist(x_bar_3)
(2)
x_bar_3=rep(0,1000)
for (idx in 1:1000) {
set.seed(idx)
x_bar_3 [idx] =mean(rbinom(3,10,0.3))
}
hist(x_bar_3)