Z-test with 2 samples
Show older comments
Does Matlab calculate the Z-test with 2 samples?
1 Comment
Scott Callahan
on 31 Aug 2018
Visit my page for a 2 sample z test function I created.
Answers (1)
Wayne King
on 3 Nov 2012
Edited: Wayne King
on 3 Nov 2012
Why not use the two-sample t-test? I think there are very few situations in which the two-sample z-test is going to give you something the two-sample t-test will not.
For example, as far as I know, the z-test means that you know the (population) standard deviation of the samples. That is pretty restrictive.
But you can easily define a two-sample z-test
function zval = ztest2(x,y,mux,muy,varax,vary)
% x - sample 1
% y - sample 2
% mux - mean of x to be used in difference. If you are not testing a
% specific difference, use mux = 0
% muy - mean of y. If you are not testing a
% specific difference, use muy = 0
% varx - variance of x
% vary - variance of y
Nx = length(x);
Ny = length(y);
zval = ((mean(x)-mean(y))-(mux-muy))/sqrt(varax/Nx+vary/Ny);
end
Save ztest2.m in a folder on the MATLAB path and try
x = 4+randn(100,1);
y = randn(100,1);
zval = ztest2(x,y,0,0,1,1);
% now use
zval1 = ztest2(x,y,4,0,1,1);
3 Comments
Sonja Brett
on 15 Nov 2022
I am having issues with the t-test because I am using large sample sizes (20,000). My p-values are coming up as 0 or 1, no decimals even when changing to format long. I believe it is because the t-test is designed for smaller sample sizes. I would like to compare the same population (large large sample size) before and after treatment, which would be a paired z-test.
raym
on 5 Mar 2023
After we get zval, how to get p-value?
raym
on 5 Mar 2023
This is my modified function
function zval = ztest22(x,y,mux,muy,varx,vary)
if ~nargin
x = rand(10000,1);
y = 1+rand(10000,1);
end
%%
% x - sample 1
% y - sample 2
% mux - mean of x to be used in difference. If you are not testing a
% specific difference, use mux = 0
% muy - mean of y. If you are not testing a
% specific difference, use muy = 0
% varx - variance of x
% vary - variance of y
if ~exist('mux','var');mux=0;end
if ~exist('muy','var');muy=0;end
if ~exist('varx','var');varx=std(x);end
if ~exist('vary','var');vary=std(y);end
%%
Nx = length(x);
Ny = length(y);
zval = ((mean(x)-mean(y))-(mux-muy))/sqrt(varx/Nx+vary/Ny);
end
Categories
Find more on Hypothesis Tests in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!