My professor hinted that we will need to use rnorm and for loops to generate the
ID: 3178163 • Letter: M
Question
My professor hinted that we will need to use rnorm and for loops to generate the answers, but I just can't figure it out. We are to use R to answer these questions and provide the code used along with the answers.
where S is the sample standard deviation and the R package function sd() can compute it.
(a) Generate Y1,Y2, ...,Y100 from N(100,10). Here, 10 is the standard deviation.
(b) For each Yi = yi from (a), generate Xi | Yi = yi from N(yi ,1) for i = 1, ...,100.
(c) Estimate E(X | Y) and Var (X | Y) by the sample mean and Equation (0.1) above, respectively.
Hence, you have 100 estimated E(X | Y) and Var (X | Y).
(d) Now, estimate E(X) and Var (X) by using the equations (3.8) and (3.9) in the class notes.
Your answer should be close to 100 and 101.
E[X] = E[E(X | Y)] (3.8)
Var(X) = E[Var(X | Y)] + Var[E(X | Y)] (3.9)
N(Au, o 2)Explanation / Answer
Here is the code for all these
Also, it is not mentioned how X to be generated for each Yi, I am generating 100 X for each Yi
y=rnorm(100,100,10) #generate 100 yi#
x=matrix(0,nrow=100,ncol=100)
for (i in 1:100)
{
x[i,]=rnorm(100,y[i],1)
}#generate 100 xi|yi#
E=rowMeans(x)#each row mean is a E(X|Y=y)#
V= unlist(lapply(1:100, function(i){sd(x[i,])})) # each element isV(X|Y)#
EX= mean(E)
VX= mean(V)+var(E)
The EX and VX will give you E(X) and V(X), I have the answer
EX= 99.7204
VX= 100.9647
Which are very close to 100,101 respetively
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.