0

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
r2evans
  • 141,215
  • 6
  • 77
  • 149
  • Instead of `something` try `x$p.value > 0.05`. – Rui Barradas Dec 07 '22 at 12:10
  • 1
    (1) If you look at `names(x)`, you'll see many properties of the t-test, including `"t"` (the "t" statistic) and `"p.value"`. Perhaps you want something like `if (x$p.value < 0.05)` (or some **meaningful** number instead of the classroom-default of 0.05). (2) While it likely works fine here, I suggest `while (m < 150)` instead of `while (m != 150)`; in this case there's little risk, but [floating-point equality](https://stackoverflow.com/q/9508518/3358272) can be an issue, and your condition is that you don't want to go above `150`, so `>` is safer. – r2evans Dec 07 '22 at 12:10
  • Thank you so much, I believe it works now, but it says the non-regection of H0, so should I use < or > in the if-statement, both comment's have different opinions –  Dec 07 '22 at 12:22

1 Answers1

0

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

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66