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

### Recently Asked Questions

- An English teacher needs to pick 10 books to put on his reading list for the next school year, and he needs to plan the order in which they should be read. He

- Fill in the blank A sampling distribution for a sample mean is made up of many _________________calculated from many ___________________ from a population.The

- We wish to estimate what percent of adult residents in a certain county are parents. Out of 100 adult residents sampled, 9 had kids. Based on this, construct a