Given the codes for X^2(chisq) and G^2(likelihood rato test statisic). Please write the following code in R.
(e) (2 points) What proportion (pN ) of the observations
generated in part a above, has |χ2 − G2| > 0.05?
Note: The proportion you are calculating here (i.e. pN ) is an
estimate of
P(|χ2 −G2|>0.05).
(f) (7 points) In the questions above, we did the calculations with a sample of size n=10. Nowlet’smakeitn=100. Withn=100,calculatepN forN = 200, 400, 600, · · · 20000 and make a plot of pN aganst N . Comment on what you learn from the plot.
n<-10
N<-100
p1<-0.1
p2<-0.2
p3<-0.3
p4<-0.4
p<-c(p1,p2,p3,p4)
multi<-rmultinom(n,N,p)
est<-(data)/100
exact<-matrix(rep(p,10),4,10)
to_sum<-((est-exact)^2)/(exact)
chisq<-colSums(to_sum)*N
chisq
mu<-100*pi
for(i in 1:length(N)){
gsq<-2*colSums(multi*log(multi/mu))
}
gsq
n<-10
N<-100
p1<-0.1
p2<-0.2
p3<-0.3
p4<-0.4
p<-c(p1,p2,p3,p4)
multi<-rmultinom(n,N,p)
est<-(data)/100
exact<-matrix(rep(p,10),4,10)
to_sum<-((est-exact)^2)/(exact)
chisq<-colSums(to_sum)*N
chisq
mu<-100*pi
for(i in 1:length(N)){
gsq<-2*colSums(multi*log(multi/mu))
}
gsq
#part e
k=0;s=0
for(i in 1:n)
{
s[i]=abs(chisq[i]-gsq[i])
if(s[i]>0.05)
{
k=k+1
}
}
pN=k/n # required answer
#part f
N=seq(200,20000,200)
n=100
for(j in 1:length(N))
{
multi<-rmultinom(n,N[j],p)
est<-(data)/100
exact<-matrix(rep(p,10),4,10)
to_sum<-((est-exact)^2)/(exact)
chisq<-colSums(to_sum)*N[j]
chisq
mu<-100*pi
gsq<-2*colSums(multi*log(multi/mu))
k=0;s=0
for(i in 1:n)
{
s[i]=abs(chisq[i]-gsq[i])
if(s[i]>0.05)
{
k=k+1
}
}
pN[j]=k/n
}
plot(1:length(N),pN)
Get Answers For Free
Most questions answered within 1 hours.