# Return to Minitab macro index

# How to save this page as a macro

# CONTRAST.MAC 
# This macro computes the test statistic for contrasts of means (as part of
# an ANOVA study).
# 
# Data stored in separate columns (ci, ..., cj),
#  with contrast coefficients in column(s) C:
#    %CONTRAST C; 
#       SAMPLES ci...cj. 
# 
# Data in the same column,
#  with contrast coefficients in column(s) C:
#    %CONTRAST C; 
#       GROUPS data group.
#
# Summary statistics (frequencies, means, SDs)
#  with contrast coefficients in column(s) C:
#    %CONTRAST C; 
#       SUMMARY freq mean SD.   
# 
#  AUTHOR:  Darryl K. Nester
#           Bluffton College 
#           Bluffton, OH  45817
#           (419)358 - 3483  
#
MACRO 
CONTRAST Coeffs.1 - Coeffs.C;
 SUMMARY Freq xbar StdDev; 
 GROUP Dat Grp; 
 SAMPLES X.1 - X.N;
 CONF alpha.

MCOLUMN Coeffs.1-Coeffs.C  Freq xbar StdDev X.1-X.N Dat Grp 
MCOLUMN DF Categs Temp t Contrast StdErrC Low High
MCONSTANT A B I N M Y Z PooledS DFE alpha
MTITLE 'Contrast of Means'

#NOTE 
#NOTE  Neither Minitab Inc. nor the author of this MACRO makes any claim 
#NOTE  or offers any warranty whatsoever with regard to the accuracy of 
#NOTE  this MACRO or its suitability for use, and Minitab Inc. and the 
#NOTE  author of this MACRO each disclaims any liability with respect thereto.   
#NOTE   

BRIEF 0 
IF CONF = 1
  IF alpha>1
     LET alpha = alpha / 100
  ENDIF
  IF alpha>1 OR alpha<0
     LET alpha = 0.95
  ENDIF
ELSE
  LET alpha = 0.95
ENDIF

IF GROUP = 1
  STATS Dat;
    BY Grp;
    GVALUES Categs;
    N Freq;
    MEAN xbar;
    STDEV StdDev.
  N Freq N
ELSEIF SUMMARY = 1
  N Freq N
  SET Categs
    1:N
  END
ELSE 
  DO A = 1:N  
    LET Freq(A) = N(X.A)         #calculate COUNT for group 
    LET xbar(A) = MEAN(X.A)      #calculate MEAN for group
    LET StdDev(A) = STDEV(X.A)   #calculate STANDARD DEVIATION for group 
    LET Categs(A) = A
  ENDDO 
ENDIF 
LET DF = Freq - 1
LET DFE = SUM(DF)
LET PooledS = SQRT( SUM(DF * StdDev ** 2) / DFE )

BRIEF 2
PRINT Categs Freq xbar StdDev
NOTITLE
PRINT PooledS alpha

LET alpha = (1-alpha)/2

LET DF=DFE

DO A = 1:C
 BRIEF 0
 LET Contrast = SUM(Coeffs.A * xbar)
 LET StdErrC = PooledS * SQRT(SUM(Coeffs.A * Coeffs.A / Freq))
 LET t = Contrast / StdErrC
 LET Z = - ABSOLUTE (t)
 CDF Z Temp;
   T DFE.
 InvCDF alpha Low;
   T DFE.
 LET High = -Low
 LET Low = Contrast + Low * StdErrC
 LET High = Contrast + High * StdErrC
 BRIEF 2

 NOTE ----------------
 PRINT Coeffs.A
 PRINT Contrast StdErrC t DF Temp Low High;
  FORMAT ('    Estimated contrast: ',F10.5,', with standard error ',F10.5,'.',/,&
          '    t=',F10.5,' (with df=',I3,'); one-sided P-value=',F10.8,/,&
          '    Confidence interval: (',F10.5,',',F10.5,').').

ENDDO

ENDMACRO