[R] t-test or conf interval with known variance?
(Ted Harding)
Ted.Harding at nessie.mcc.ac.uk
Sun Jan 30 15:41:27 CET 2005
On 30-Jan-05 Thomas Hopper wrote:
> Hello,
>
> Is there a built-in test in R for hypothesis testing with samples of
> known variance?
>
> For example, I've got a set of data, a mean to compare against, and a
> known variance, and I want to determine the p-value for which I can
> reject the null hypothesis (mu_1 = mu_0) and accept the alternative
> (mu_1 > mu_0). I've found that JMP and Minitab can both do this (in
> JMP, it's a one-sided confidence interval in the Distribution
> platform), but I haven't figured out how to do such a test in R.
Since you know the variance (and I think you may be implicitly
assuming that the data are Normally distributed though you don't
say so), it should be very straightforward. It's all so close
to "raw R" that there doesn;t seem to be a user-friendly function
already written!
Let V be the known variance.
The mean M of the data will have a normal distribution with mean
m1 and variance V/n where n is the sample size. Since you are
looking at alternatives m1 > m0, you need large values of M to
"reject" the H0, that m1 = m0.
On H0, (M - m0)/S has a standard normal distribution with mean 0
and variance 1, where
S = sqrt(V/n)
Hence your P-value is
1 - pnorm((mean(x) - m0)/sqrt(V/n)
and your (say 95%) lower 1-sided confidence limit for m1 is
M + sqrt(V/n)*qnorm(p)
where p = (1 - 0.95) = 0.05. For any other confidence level,
use the corresponding value of p.
You can wrap this in a function my.test:
my.test <- function(x,V,m0,P) {
M<-mean(x); n<-length(x); S<-sqrt(V/n)
p<-(1 - pnorm((M-m0)/S))
LCL<-(M + S*qnorm(1-P/100))
value<-list(Pvalue=p,LCL=LCL)
print(sprintf("P-value = %g",p))
print(sprintf("Lower %.2f%% Confidence Limit = %g",
P, LCL))
}
where x is the sample, V is the known variance,
m0 is the mean to test against, and P is the *percentage*
confidence level (e.g. 95 for 95%) that you want for the
confidence limit.
Hoping this helps,
Ted.
--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861 [NB: New number!]
Date: 30-Jan-05 Time: 14:41:27
------------------------------ XFMail ------------------------------
More information about the R-help
mailing list