STAT 231 Online Assignment 5 Download the Assignment 5 Template which is posted as a Word document in the Assignment 5 folder on Learn. Your assignment submission must be typed and follow the template exactly. There are no exceptions. If you wish you may create your own document in Google Docs, LaTeX or another word processor but it must follow the same layout as in the provided Word document. Create a .pdf file of your assignment. Crowdmark only accepts .pdf files. Upload your assignment to Crowdmark. Here is a useful link for all information related to Crowdmark assessments: https://crowdmark.com/help/ The code for this assignment is posted both as a text file called RCodeAssignment5.txt and an R file called RCodeAssignment5R.R which are posted in the Assignment 5 folder on Learn. Problem 1: Tests of hypothesis for Binomial data The purpose of this problem is to use R to test the hypothesis H0 : = 0 for Binomial(n,) data. See Sections 5.1 and 5.3 of the Course Notes, Example 5.3.1, and Chapter 5, Problem 1. Run the following R code: ################################################################################### # Problem 1: Tests of hypothesis for Binomial data id<-20456484 set.seed(id) # generate a random value of theta from a Uniform(0.3,0.35) distribution theta<-round(runif(1,0.3,0.35),digits=3) theta0<-round(theta+0.1,digits=1) # value for theta0 n<-30 y<-rbinom(1,n,theta) # observation from Binomial(n,theta) distribution thetahat<-y/n #maximum likelihood estimate of theta # display values cat('theta = ', theta, ', theta0 = ',theta0, "\n") cat('n = ', n, ', y = ', y, ', thetahat = ',thetahat, "\n") # observed value of likelihood ratio test for testing the null hypothesis theta=theta0 lambda<--2*log((theta0/thetahat)^y*((1-theta0)/(1-thetahat))^(n-y)) cat('observed value of likelihood ratio statistic = ',lambda, "\n") pvalue<-2*(1-pnorm(sqrt(lambda),0,1)) cat('approximate p-value for testing hypothesis theta = theta0: ',pvalue, "\n") # observed value of approximate Gaussian test statistic for testing the null hypothesis theta=theta0 d<-abs(thetahat-theta0)/sqrt(theta0*(1-theta0)/n) cat('observed value of approximate Gaussian test statistic = ',d, "\n") pvalue<-2*(1-pnorm(d,0,1)) cat('approximate p-value for testing hypothesis theta = theta0: ',pvalue , "\n") # exact p-value using test statistic D=|Y-n*theta0| pvalue<-pbinom(y,n,theta0)+1-pbinom(n*theta0+abs(y-n*theta0)-1,n,theta0) cat('exact p-value for testing hypothesis theta = theta0: ',pvalue, "\n") ################################################################################### Verify that you obtain the following output: theta = 0.316 , theta0 = 0.4 n = 30 , thetahat = 0.2333333 observed value of likelihood ratio statistic = 3.729682 approximate p-value for testing hypothesis theta = theta0: 0.05345357 observed value of approximate Gaussian test statistic = 1.86339 approximate p-value for testing hypothesis theta = theta0: 0.06240742 exact p-value for testing hypothesis theta = theta0: 0.09163583 Run the R code for again except modify the line "id<-20456484" by replacing the number 20456484 with your UWaterloo ID number. In the Assignment 5 template fill in the required information based on the output for the data generated using your ID. Modify the R code that you just used (the code with your ID number inserted) so that n = 90 but theta, theta0, and thetahat remain the same as for n = 30. (Note: you don’t need to generate any new data.) Run your modified code and fill in the required information in the Assignment 5 template. Problem 2: Tests of hypothesis for Poisson data The purpose of this problem is to use R to test the hypothesis H0 : = 0 for Poisson() data. See Sections 5.1 and 5.3, and Chapter 5, Problem 8 in the Course Notes. Run the following R code: ################################################################################### # Problem 2: Tests of hypothesis for Poisson data id<-20456484 set.seed(id) # generate a random value of theta from a Uniform(3,4) distribution theta<-round(runif(1,3,4),digits=3) theta0<-round(theta+0.6,digits=1) # value for theta0 n<-30 # determine the maximum likelihood estimate based on a random sample # generated from a Poisson(theta) distribution thetahat<-mean(rpois(n,theta)) # display values cat('theta = ', theta, ', theta0 = ',theta0, "\n") cat('n = ', n, ', thetahat = ',thetahat, "\n") # observed value of likelihood ratio test for testing the null hypothesis theta=theta0 lambda<--2*log((theta0/thetahat)^(n*thetahat)*exp(n*(thetahat-theta0))) cat('observed value of likelihood ratio statistic = ',lambda, "\n") pvalue<-2*(1-pnorm(sqrt(lambda),0,1)) cat('approximate p-value for testing hypothesis theta = theta0: ',pvalue, "\n") # observed value of approximate Gaussian test statistic for testing the null hypothesis theta=theta0 d<-abs(thetahat-theta0)/sqrt(theta0/n) cat('observed value of approximate Gaussian test statistic = ',d, "\n") pvalue<-2*(1-pnorm(d,0,1)) cat('approximate p-value for testing hypothesis theta = theta0: ',pvalue, "\n") # exact p-value d1<-n*abs(thetahat-theta0) pvalue<-ppois(n*theta0-d1,n*theta0)+1-ppois(n*theta0+d1-1,n*theta0) cat('exact p-value for testing hypothesis theta = theta0: ',pvalue, "\n") ################################################################################### Verify that you obtain the following output: theta = 3.326 , theta0 = 3.9 n = 30 , thetahat = 3.2 observed value of likelihood ratio statistic = 4.017457 approximate p-value for testing hypothesis theta = theta0: 0.04503156 observed value of approximate Gaussian test statistic = 1.941451 approximate p-value for testing hypothesis theta = theta0: 0.05220364 exact p-value for testing hypothesis theta = theta0: 0.05778636 Run the R code for again except modify the line "id<-20456484" by replacing the number 20456484 with your UWaterloo ID number. In the Assignment 5 template fill in the required information based on the output for the data generated using your ID. Problem 3: Tests of hypothesis for Exponential data The purpose of this problem is to use R to test the hypothesis H0 : = 0 for Exponential() data. See Section 5.3, Example 5.3.2, and Chapter 4, Problem 26 in the Course Notes. Run the following R code: ################################################################################### # Tests of hypothesis for Exponential data id<-20456484 set.seed(id) # generate a random value of theta from a Uniform(9,10) distribution theta<-round(runif(1,9,10),digits=3) theta0<-round(theta*1.3,digits=1) # value for theta0 n<-35 # determine the maximum likelihood estimate based on a random sample # generated from a Exponential(theta) distribution thetahat<-mean(rexp(n,1/theta)) # display values cat('theta = ', theta, ', theta0 = ',theta0, "\n") cat('n = ', n, ', thetahat = ',thetahat, "\n") # observed value of likelihood ratio test for testing the null hypothesis theta=theta0 lambda<--2*log((thetahat/theta0)^n*exp(n*(1-thetahat/theta0))) cat('observed value of likelihood ratio statistic = ',lambda, "\n") pvalue<-2*(1-pnorm(sqrt(lambda),0,1)) cat('approximate p-value for testing hypothesis theta = theta0: ',pvalue, "\n") # observed value of approximate Gaussian test statistic for testing the null hypothesis theta=theta0 d<-abs(thetahat-theta0)/(theta0/sqrt(n)) cat('observed value of approximate Gaussian test statistic = ',d, "\n") pvalue<-2*(1-pnorm(d,0,1)) # exact p-value using test statistic D1=2n*thetatilde/theta0 cat('approximate p-value for testing hypothesis theta = theta0: ',pvalue, "\n") d1<-2*n*thetahat/theta0 pvalue<-min(2*pchisq(d1,2*n),2*(1-pchisq(d1,2*n))) cat('exact p-value for testing hypothesis theta = theta0 using D1: ',pvalue, "\n") ################################################################################## Verify that you obtain the following output: theta = 9.326 , theta0 = 12.1 n = 35 , thetahat = 9.293216 observed value of likelihood ratio statistic = 2.236861 approximate p-value for testing hypothesis theta = theta0: 0.1347543 observed value of approximate Gaussian test statistic = 1.372327 approximate p-value for testing hypothesis theta = theta0: 0.1699617 exact p-value for testing hypothesis theta = theta0 using D1: 0.1504173 Run the R code for again except modify the line "id<-20456484" by replacing the number 20456484 with your UWaterloo ID number. In the Assignment 5 template fill in the required information based on the output for the data generated using your ID. Modify the R code that you just used (the code with your ID number inserted) so that n = 70 but theta, theta0, and thetahat remain the same as for n = 35. (Note: you don’t need to generate any new data.) Run your modified code and fill in the required information in the Assignment 5 template. Problem 4: Tests of hypothesis for Gaussian data The purpose of this problem is to use R to test the hypotheses H0 : μ = μ0 and H0 : σ = σ0 for G(μ,σ) data. See Section 5.2 in the Course Notes. Run the following R code: ################################################################################### # Problem 4: Tests of hypothesis for Gaussian data id<-20456484 set.seed(id) # generate a random value of mu from a Uniform(4,5) distribution mu<-round(runif(1,4,5),digits=3) mu0<-round(mu+1.25,digits=2) # value for mu0 # generate a random value of sigma from a Uniform(2,3) distribution sigma<-round(runif(1,3,4),digits=3) sigma0<-round(sigma*1.2,digits=1) # value for sigma0 n<-50 # determine the estimates based on a random sample # generated from a G(mu,sigma) distribution y<-rnorm(n,mu,sigma) muhat<-mean(y) s<-sd(y) # display values cat('mu = ', mu, ', mu0 = ',mu0, "\n") cat('sigma = ', sigma, ', sigma0 = ',sigma0, "\n") cat('n = ', n, ', muhat = ',muhat,', s = ',s, "\n") # Use t.test fo test hypothesis mu=mu0 t.test(y,mu=mu0)$statistic t.test(y,mu=mu0)$parameter t.test(y,mu=mu0)$p.value df<-length(y)-1 d<-df*s^2/sigma0^2 # observed value of statistic for testing the null hypothesis sigma=sigma0 cat('observed value of test statistic = ',d, "\n") q<-pchisq(d,df) # p-value for testing sigma=sigma0 cat('p-value for testing hypothesis sigma = sigma0 using D: ',min(2*q,2*(1-q)), "\n") ################################################################################### Verify that you obtain the following output: mu = 4.326 , mu0 = 5.58 sigma = 3.121 , sigma0 = 3.7 n = 50 , muhat = 4.542454, s = 2.787237 t -2.632198 df 49 [1] 0.01131382 observed value of test statistic = 27.80613 p-value for testing hypothesis sigma = sigma0 using D: 0.01268351 Run the R code for again except modify the line "id<-20456484" by replacing the number 20456484 with your UWaterloo ID number. In the Assignment 5 template fill in the required information based on the output for the data generated using your ID.
欢迎咨询51作业君