2

I apologize for the vague title, but I don't really know to explain it well cause I just don't understand what it is I'm supposed to do.

enter image description here

enter image description here

Below is my code which I believe provides everything e (ii) asks for, but I don't know what to do for the alt = "two-sided" argument. Any help in clarifying it would be appreciated and please let me know if what I have is wrong

ev <- function(x, mu0 = y, lev = 0.95) {
  ts<- ((mean(x)-mu0)/(sd(x)/sqrt(length(x))))
  pval <- 2*pnorm(-abs(ts))
  ci<- mean(x) + qt(lev / 2, length(x) - 1) * sd(x) / sqrt(length(x)) * c(-1, 1)
    return(list(c(ts,pval, ci)))
}
s<- rnorm(20)
> ev(s,0.2)
[[1]]
[1] -0.65345075  0.51346573  0.10089696  0.07954784


ev <- function(x, mu0 = y, alt = "two-sided", lev = 0.95) {
  ts<- ((mean(x)-mu0)/(sd(x)/sqrt(length(x))))
  if(alt == "two-sided") {
    pval <- 2*pnorm(-abs(ts))
  ci<- c((mu0- pval*(length(x)/ts)), mu0 + pval*(length(x)/ts))
  }
  else if(alt == "greater"){
    pval <- (1- pnorm(-abs(ts)))
    ci<- c((mu0- pval*(length(x)/ts)), mu0 + pval*(length(x)/ts))
  }
  else if(alt == "less") {
    pval <- pnorm(-abs(ts))
    ci<- c((mu0- pval*(length(x)/ts)), mu0 + pval*(length(x)/ts))
  }
    return(list(c(ts,pval, ci)))
}
redwoods
  • 59
  • 5
  • 1
    So the `alt = "two-sided"` would indicate which type of test to make: one sided or two-sided test (possibly "greater than" or "lower than"). You could use `if(alt == "two-sided"){ do something}else if(alt == 'lower'){ do something different }` for example. – Oliver Mar 25 '22 at 17:36
  • Are you familiar with the differences between [one and two sided tests](https://en.wikipedia.org/wiki/One-_and_two-tailed_tests) and you're struggling with the implementation, or do you know what you're trying to do and struggling with the code to do it? – Dubukay Mar 25 '22 at 17:36
  • I understand what one and two-sided tests are, my brain just isn't getting that transition from writing it on paper to coding it @Dubukay – redwoods Mar 25 '22 at 17:43
  • 2
    So I should be getting 3 different p-values and confidence intervals for "two-sided", "greater", and "less"? @Oliver – redwoods Mar 25 '22 at 17:53
  • 2
    @redwoods That's correct - those are fundamentally different tests with different priors, so they'll have different p-values and confidence intervals – Dubukay Mar 25 '22 at 18:00
  • 2
    I'm 99% sure my formulas are wrong but does this new code at the bottom look right? Thanks for helping me out @Dubukay – redwoods Mar 25 '22 at 21:56
  • 1
    @redwoods Yep, that's a great way of handling the control flow if/else logic! There's some more advanced advice and syntax in Hadley's book [here](https://adv-r.hadley.nz/control-flow.html) if you're curious. – Dubukay Mar 25 '22 at 22:08

1 Answers1

1

From comments:

Use if/else statements to handle the different alternative hypotheses because they are fundamentally different tests with different priors, so they'll have different p-values and confidence intervals.

if(alt == "two-sided") {
    # Do the thing
  } else if(alt == "greater"){
    # Do the other thing
  } else if(alt == "less") {
    # Do the third thing
  } else {
    stop('"alt" must be one of "two-sided", "greater", "less"'
  }
Dubukay
  • 1,764
  • 1
  • 8
  • 13