Problem 2

lrt <- vector()

for(i in 1:100){

  sample.norm <- rnorm(100,0,sqrt(2))

  

  sample.mean <- mean(sample.norm)

  sample.var <- var(sample.norm) * 99 / 100 # var function is using n-1 as the denominator 

  

  lrt[i] <- (sum(sample.norm^2) - sum((sample.norm - sample.mean)^2)) /sample.var

  

}
# probability = TRUE for hist and add=TRUE for curve

hist(lrt,probability = TRUE)

curve(dchisq(x,1),from=0,to=20, add=TRUE)

plot of chunk unnamed-chunk-2

sample.norm <- rnorm(100,0,sqrt(2))

  

sample.mean <- mean(sample.norm)

sample.var <- var(sample.norm) * 99 / 100

  

lrt2 <- (sum(sample.norm^2) - sum((sample.norm - sample.mean)^2))/sample.var



1 - pchisq(lrt2, 1)
[1] 0.7873
lrt.2e <- vector()

for(i in 1:100){

  sample.norm <- rnorm(10000,0,sqrt(2))

  

  sample.mean <- mean(sample.norm)

  sample.var <- var(sample.norm) * 99 / 100 # var function is using n-1 as the denominator 

  

  lrt.2e[i] <- (sum(sample.norm^2) - sum((sample.norm - sample.mean)^2)) /sample.var

  

}



hist(lrt.2e,probability = TRUE)

curve(dchisq(x,1),from=0,to=20, add=TRUE)

plot of chunk unnamed-chunk-4

lrt3 <- vector()

for(i in 1:100){

  sample.norm <- rnorm(100,0.5,sqrt(2))

  

  sample.mean <- mean(sample.norm)

  sample.var <- var(sample.norm) * 99 / 100

  

  lrt3[i] <- (sum(sample.norm^2) - sum((sample.norm - sample.mean)^2))/sample.var

  

}



hist(lrt3,probability = TRUE)

curve(dchisq(x,1),from=0,to=20, add=TRUE)

plot of chunk unnamed-chunk-5

-2g. No. We generated the samples from a distribution that has different parameters from the null hypothesis so the histogram is shifted to the right.

sample.norm <- rnorm(100,0.5,sqrt(2))

  

sample.mean <- mean(sample.norm)

sample.var <- var(sample.norm) * 99 / 100

  

lrt2 <- (sum(sample.norm^2) - sum((sample.norm - sample.mean)^2)) / sample.var



1 - pchisq(lrt2, 1)
[1] 0.1635
lrt3 <- vector()

for(i in 1:100){

  sample.norm <- rnorm(100,1,sqrt(2))

  

  sample.mean <- mean(sample.norm)

  sample.var <- var(sample.norm) * 99 / 100

  

  lrt3[i] <- (sum(sample.norm^2) - sum((sample.norm - sample.mean)^2))/sample.var

  

}



hist(lrt3,probability = TRUE)

curve(dchisq(x,1),from=0,to=20, add=TRUE)

plot of chunk unnamed-chunk-7