function [D,t,df,pvalue,CI,sp]=student2group(Xn,Yn,Xmean,Ymean,Xstd,Ystd,alpha)
% Student't 2-sample equal variance t test with grouped data
% [D,t,df,pvalue,CI,sp]=student2group(Xn,Yn,Xmean,Ymean,Xstd,Ystd,alpha)
% Input: Xn, Yn = Size of X and Y groups
%        Xmean, Ymean = means for 2 groups
%        Xstd, Ystd = standard deviations for 2 groups
%        alpha level for CI, optional, 95% if not specified
% Output: D=Xmean-Ymean;
%         t=Student's t statistic
%         df=degrees of freedom for t statistic
%         p value, 2-sided, for t Statisitc, with df degrees of freedom
%         CI for 1-alpha CI, [L U];95% CI if alpha not specified.
%         sp pooled standard deviation for difference
% Based on Theorems 9.2.1 & 9.2.2, page 555 & 557 Larsen & Marx (2006)
% Introduction to Mathematical Statistics, 4th edition.
% uses Student's t from Statistics toolbox
% Written by Eugene.Gallagher@umb.edu, Revised 11/17/10, 2/27/11 (added
% sp to output)
% see also stud1sample, stud2sample,
n=Xn;
m=Yn;
D=Xmean-Ymean;
% Estimated pooled standard deviation (Theorem 9.2.1)
sp=sqrt(    ((n-1)*Xstd^2+(m-1)*Ystd^2)...
                /  (n+m-2) );
t=D/(sp*sqrt(1/n+1/m));
% Use Matlab's tcdf for p values
df=n+m-2;
if t>=0
    pvalue = 2*(1-tcdf(t,df));
else
    pvalue=2*tcdf(t,df);
end
if nargin<3;
    alpha=0.05;
end
% Use tinv.m from statistics toolbox
HalfCI=tinv(1-alpha/2,df)*sp*sqrt(1/n+1/m);
CI=[D-HalfCI D+HalfCI];