Home > Back-end >  If statement in hypothesis testing with a normal distribution
If statement in hypothesis testing with a normal distribution

Time:12-08

I have this code that does a hypothesis test that tests H0: mu = 0.3 against H1: mu > 0.3. I am asked to estimate the probability of the test leading to the non-rejection of H0 when mu = 1.1. I wanted to do an if statement as followed but I don't know what argument I should use from the t.test

set.seed(1343)
m = 0
accept = 0
valores = numeric(0)
while (m != 150){
  amostra <- rnorm(27, mean = 1.1, 2.8)
  x <- t.test(amostra, mu = 0.3, alternative = "greater", conf.level = 0.96)
  if  (something){
    accept = accept   1
  }
  m = m   1
}
prob = accept/m

CodePudding user response:

Instead of updating the total in the loop, run the loop, save the p-values and after the loop compute the mean of a logical condition. FALSE/TRUE are coded as 0/1 so you will have the proportion you want.

set.seed(1343)
m <- 150
pval <- numeric(m)
for(i in seq_along(pval)) {
  amostra <- rnorm(27, mean = 1.1, 2.8)
  x <- t.test(amostra, mu = 0.3, alternative = "greater", conf.level = 0.96)
  pval[i] <- x$p.value
}
prob <- mean(pval > 0.05)
prob
#> [1] 0.6

Created on 2022-12-07 with reprex v2.0.2

  •  Tags:  
  • r
  • Related