View the step-by-step solution to:


I am trying to simulate the p-value in R.

However, I am not getting the correct p-value

in my simulation. Can you please tell me where the error is in my code? Thank you

#Question 1 Simulation

 n <- 100000 # number of sim runs 

 T <- 0 # initialize T 

 counter <- 0 

 r <- 564 #sample size

 for(j in 1:n){ 

  x <-sample(3,564,replace = TRUE) 

  x1 <- length(x[x<2]) 

  x2 <- length(x[x<3])-x1

  x3 <- length(x[x<4])-x1-x2

  T[j] <- T[j] <- ( ((x1-r/4)^2)/(r/4) + ((x2-r/2)^2)/(r/2) + ((x3-r/4)^2)/(r/4)) #T test statistic formula

  if (T[j]>0.8617){  #.8617 is the chi square approximation answer.

   counter <- counter+1 






 counter/n # Simulation P-Value 

 1-pchisq(0.8617,2) #P - Value Built in Function 

chegg 1.PNG

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes