Reputation: 59
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.
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)))
}
Upvotes: 2
Views: 83
Reputation: 2071
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"'
}
Upvotes: 1