Z-test with 2 samples

20 visualizaciones (últimos 30 días)
Ephel Duithy
Ephel Duithy el 3 de Nov. de 2012
Comentada: raym el 5 de Mzo. de 2023
Does Matlab calculate the Z-test with 2 samples?
  1 comentario
Scott Callahan
Scott Callahan el 31 de Ag. de 2018
Visit my page for a 2 sample z test function I created.

Iniciar sesión para comentar.

Respuestas (1)

Wayne King
Wayne King el 3 de Nov. de 2012
Editada: Wayne King el 3 de Nov. de 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 comentarios
raym
raym el 5 de Mzo. de 2023
After we get zval, how to get p-value?
raym
raym el 5 de Mzo. de 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

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by