function twostone(x,y,c,alpha)
%TWOSTONE Two-sample Student's t-test when one has only one observation.
% This file is a two-sample Student's t-test when one of the samples has
% only one observation.
%
% Syntax: function twostone(x,y,alpha)
%
% Inputs:
% x - vector data of one of the samples
% y - one data of the other sample
% alpha - significance value (=0.05 by default)
% Output:
% Complete table of the Student's t-test
%
% Example: From the Box 9.7 of Sokal and Rohlf (1995) two places were
% sampled and one of them has a single observation. Data are the wing
% length (mm) of the mosquito Aedes canadiensis. With a significance of
% 0.05We are interested to test if the places are equal. Data are:
%
% Observation
% Place 1 2 3 4 5
% 1 4.02 3.88 3.34 3.87 3.18
% 2 3.02
%
% x = [4.02,3.88,3.34,3.87,3.18];
% y = 3.02;
%
% Calling on Matlab the function:
% twostone(x,y,1)
%
% Answer is:
%
% ---------------------------------------------
% Sample-sizes t df P
% ---------------------------------------------
% 5 1 -1.5637 4 0.1924
% ---------------------------------------------
% With a given significance level of: 0.025
% Null-hypothesis that samples are equal is not rejected
% It was asked for a two-tail t-test
%
% Created by A. Trujillo-Ortiz, R. Hernandez-Walls and K. Barba-Rojo
% Facultad de Ciencias Marinas
% Universidad Autonoma de Baja California
% Apdo. Postal 453
% Ensenada, Baja California
% Mexico.
% atrujo@uabc.mx
%
% Copyright. April 25, 2008.
%
% To cite this file, this would be an appropriate format:
% Trujillo-Ortiz, A., R. Hernandez-Walls and K. Barba-Rojo. (2008). twostone:
% Two-sample Student's t-test when one has only one observation. A MATLAB file.
% [WWW document]. URL http://www.mathworks.com/matlabcentral/fileexchange/
% loadFile.do?objectId=19787
%
% Reference:
% Sokal, R.S. and Rohlf, F.J. (1995), Biometry.
% W.H. Freeman and Company:New York
%
if nargin == 3,
alpha = 0.05; %default
end
if ~isscalar(alpha)
error('TWOSTONE requires a scalar ALPHA value.');
end
if ~isnumeric(alpha) || isnan(alpha) || (alpha <= 0) || (alpha >= 1)
error('TWOSTONE requires 0 < ALPHA < 1.');
end
if ~isvector(x)
error('TWOSTONE requires a x vector rather than matrix data.');
elseif length(x)==1
error('TWOSTONE requires a x vector rather than a scalar.');
end
if ~isscalar(y)
error('TWOSTONE requires a y scalar value.');
end
mx = mean(x);
sx = std(x);
n = length(x);
v = n - 1; %degrees of freedom
t = (y - mx)/(sx*sqrt((n+1)/n)); %Student's t-statistic
if c == 1;
alpha = alpha/2; %two-sided test
else
alpha; %one-sided test
end
P = 1-tcdf(1-alpha,v); %P-value
n = [length(x) length(y)];
fprintf('---------------------------------------------\n');
disp('Sample-sizes t df P')
fprintf('---------------------------------------------\n');
fprintf(' %d %d%16.4f%8.i%12.4f\n',n,t,v,P);
fprintf('---------------------------------------------\n');
fprintf('With a given significance level of: %.3f\n', alpha);
if P >= alpha;
disp('Null-hypothesis that samples are equal is not rejected');
disp('It was asked for a two-tail t-test');
else
disp('Null-hypothesis that samples are equal is rejected');
disp('It was asked for an one-tail t-test');
end
return,