[R] Z-test

Joshua Wiley jwiley.psych at gmail.com
Sat Jul 16 07:49:06 CEST 2011


Hi,

The Z is basically:

(mean(x) - mean(y))/sqrt(var(x)/length(x) + var(y)/length(y))

and pnorm will give you a p-value, if you desire it.

If the n - 1 divisior used in var() is a problem for you, it is
trivial to work around:

X <- cbind(x, y)
XX <- crossprod(X - tcrossprod(matrix(1, nrow(X))) %*% X *
(1/nrow(X))) * 1/nrow(X)
diff(colMeans(X))/sqrt(sum(diag(XX)/nrow(X)))

where the last line gives the Z and again, pnorm() will give you a
p-value if desired.

In most cases a t-test is preferred (and is available using the t.test
function).

HTH,

Josh

On Fri, Jul 15, 2011 at 9:56 PM, Bogdan Tanasa <tanasa at gmail.com> wrote:
> Hi Josh,
>
> thanks for your email. I have been looking into pnorm, but hmmm ... it does
> not seem to assess the difference between 2 populations, it says
> that it works on a vector of quantiles, and sd=1, mean = 0. please let me
> know if you have any suggestions. thanks,
>
> bogdan
>
> On Fri, Jul 15, 2011 at 9:49 PM, Joshua Wiley <jwiley.psych at gmail.com>
> wrote:
>>
>> Hi Bogdan,
>>
>> Look at ?pnorm
>>
>> Josh
>>
>> On Fri, Jul 15, 2011 at 9:10 PM, Bogdan Tanasa <tanasa at gmail.com> wrote:
>> > Hi,
>> >
>> > please could you recommend a R package that computes a 2 sample z-test ?
>> >
>> > thanks,
>> >
>> > Bogdan
>> >
>> >        [[alternative HTML version deleted]]
>> >
>> > ______________________________________________
>> > R-help at r-project.org mailing list
>> > https://stat.ethz.ch/mailman/listinfo/r-help
>> > PLEASE do read the posting guide
>> > http://www.R-project.org/posting-guide.html
>> > and provide commented, minimal, self-contained, reproducible code.
>> >
>>
>>
>>
>> --
>> Joshua Wiley
>> Ph.D. Student, Health Psychology
>> University of California, Los Angeles
>> https://joshuawiley.com/
>
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
https://joshuawiley.com/



More information about the R-help mailing list