How do I determine the z score from a p value? Thanks!

 Accepted Answer

Star Strider
Star Strider on 3 Aug 2012
Edited: Star Strider on 3 Aug 2012
Using the ‘erfcinv’ function (part of core MATLAB) and writing it as an anonymous function:
z = @(p) -sqrt(2) * erfcinv(p*2);
where ‘p’ is the probability.
So:
zscore = z([0.025 0.5 0.975])
produces:
zscore =
-1.9600e+000 0.0000e+000 1.9600e+000

12 Comments

An addition: assuming normality.
Nuchto
Nuchto on 3 Aug 2012
Edited: Nuchto on 3 Aug 2012
zscore = z([0.025 0.5 0.975]). What is z? I need explanation of this equation...
Star Strider
Star Strider on 3 Aug 2012
Edited: Star Strider on 3 Aug 2012
I created an anonymous function that I called ‘z’ that uses the core MATLAB ‘erfcinv’ function to calculate the z-score from the probability. So if you copy-paste the function:
z = @(p) -sqrt(2) * erfcinv(p*2);
to your code, then call it as I did with:
zscore = z(0.025)
to calculate the z-score for a probability ‘p’ = 0.025, you will get:
zscore =
-1.9600e+000
Since you asked about a z-score for p = 0.001,
zscore = z(0.001)
zscore =
-3.0902e+000
and:
zscore = z(0.0005)
zscore =
-3.2905e+000
So a z-score of -1.96 corresponds to a probability of 0.025, and a z-score of -3.0902 corresponds to a probability of 0.001 and a z-score of -3.2905 corresponds to a probability of 0.0005. (In my experience, a z-score implies the normal distribution.)
I initially called ‘z’ with a vector of values as an illustration on how to do that.
My ‘z’ function uses the inverse of the complementary error function, closely related to the normal distribution. If you have the Statistics Toolbox, you can use ‘norminv’ instead. Since not everyone has all the toolboxes, I decided to use ‘erfcinv’ in my answer. I refer you to the documentation for ‘erfcinv’ and ‘erfc’ and the related functions for details.
Nuchto
Nuchto on 3 Aug 2012
Thanks, now I understand. I am kind of a newbie in Matlab. So I see now that z is a function created on the go. I get -3.0902 0 1.9600 with this method. And I get -3.0902 using norminv(.001). But I am supposed to get Z = 3.29; for a p = 0.001, but don't get that value. I wonder why.
Supposed to get 3.29 from which distribution?
Star Strider
Star Strider on 3 Aug 2012
Edited: Star Strider on 3 Aug 2012
I anticipated your problem, so I calculated the z-score for p = 0.0005 as -3.2905 as well. The reason is that the normal distribution has two ‘tails’ at the upper and lower extremes. So a probability of 0.001 means (in most situations) that the values of interest lie outside the center 0.999 probability. This means that each of the two ‘tails’ have areas equivalent to (0.001 / 2) or 0.0005. The z-score corresponding to p = 0.0005 is ± 3.2905.
What I meant by ‘in most situations’ is that the test considers that a given value can be either above or below a given value, so the probability takes that into account and the test is called ‘two-tailed’, as it apparently is in your situation. In some situations, the hypothesis is that the value is either ‘only greater than’ or ‘only less than’, and the ‘one-tailed’ probability is used. See your statistics text for a more detailed explanation.
I believe you can use the same correction for two tails using the norminv function.
abs(norminv(p/2))
Which produces 3.2905. abs takes the absolute value.
The beauty of a catalog going back a long time:
(from a function header)
%The implementation of tinv is by Star Strider
% https://www.mathworks.com/matlabcentral/fileexchange/56500
%The implementation of alpha_to_Z is by Star Strider as well
% https://www.mathworks.com/matlabcentral/answers/45173#answer_55318
tdist2T = @(t,v) (1-betainc(v/(v+t^2), v/2, 0.5));% 2-tailed t-distribution
tdist1T = @(t,v) 1-(1-tdist2T(t,v))/2; % 1-tailed t-distribution
% T-Statistic Given Probability alpha & Degrees-Of-Freedom v
t_inv=@(a,v) fzero(@(tval) (max(a,(1-a))-tdist1T(tval,v)),5);
alpha_to_Z = @(a) -sqrt(2) * erfcinv(a*2);%get Z value given the alpha
Thank you for making these available to all without the stats toolbox, have a vote.
@Rik —
As always, my pleasure! Thank you for the vote!
I wasn’t aware that someone else had used my code, or the citation.
Rik
Rik on 21 Mar 2019
I will be using it a function. I haven't decided yet if it will end up on the FEX, but if it does, these links will be there. (the use case here is estimating the limits of agreement and their confidence intervals in a Bland-Altman plot)
Rik
Rik on 25 Aug 2020
A bit late maybe, but I did end up putting it on the FEX, so here is the promised link: BlandAltmanPlot. Once Mathworks gets the example tab working again for github submission, you will even be able to see a nice doc.
Rik —
Interesting function! Thanks for the citation!

Sign in to comment.

More Answers (1)

Oleg Komarov
Oleg Komarov on 3 Aug 2012
Edited: Oleg Komarov on 3 Aug 2012
Use icdf() from the Statistics Toolbox.
For (standard) normal:
icdf('normal',[0.005 0.995],0,1)

2 Comments

Nuchto
Nuchto on 3 Aug 2012
Mmm, could you put an example? How to compute z from p = 0.001?
Which distribution?

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!