View the step-by-step solution to:

Question

# 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

}

}

mean(T)

max(T)

counter/n # Simulation P-Value

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

### 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