McNemar's Test Code
How to save this page as a macro
macro
###########################################################################
#
# Name: MCNEMAR.MAC
# Version: Release 15 and 16
# Written by: Eli Walters and Eduardo Santiago 7/23/2007
# Modified: 6/1/2009 and 10/14/2011 and 1/14/2013
#
# This macro performs McNemar's test on either (1) a 2x2 table
# stored in two columns of the active worksheet or (2) two columns
# of raw data in the active worksheet. Input constant f indicates
# the form of the data (1 = table, 2 = raw data). For example, if a
# 2x2 table is stored in the first two cells of columns C1 and C2,
# then type:
#
# %mcnemar 1 C1 C2;
# correction;
# confidence 0.99.
#
# The output will provide the chi-square statistic with a continuity
# correction (to provide the standard chi-square statistic omit the
# subcommand for correction), p-value for the test and a 99% confidence
# interval for the difference of the observed marginal proportions
# (if the confidence subcommand is omitted a 95% confidence interval will
# be created by default).
#
###########################################################################
#
# Neither Minitab, Inc. nor the author(s) of this MACRO makes any claim
# of or offers any Warranty whatsoever with regard to the accuracy of
# this MACRO or its suitability for use. Minitab, Inc. and the author(s)
# of this MACRO each hereby disclaims any Warranty and/or liability with
# respect thereto.
#
###########################################################################
mcnemar f x y;
correction;
confidence conf.
mcolumn x y x2 y2 labels xlab ylab cellct deltap lowci upci k obserx obsery dummy civals cint
mconstant f a b c d delta chi k1 pval xval yval bc conf typex typey ct phr1 phr2 phr3 phr4 phr5 tconf tlowci tupci xlen ylen val prob
default conf=0.95
mreset
noecho
brief 0
notitle
if conf >= 1 or conf <= 0
note
note **Error** The confidence level is a number between 0 and 1, exclusive.
note
exit
endif
if f=1 # Data has been entered in the worksheet as a 2x2 table
# Check to verify that the data is numeric
dtype x typex
dtype y typey
n x xval
n y yval
brief
if typex = 0 or typey = 0 or xval ne 2 or yval ne 2
note
note **Error** The columns for this table need to be numeric and need to have only two rows each.
note
exit
endif
let a = x(1)
let b = y(1)
let c = x(2)
let d = y(2)
elseif f=2 # Raw data has been entered in the worksheet
stats;
by x;
gval xlab.
stats;
by y;
gval ylab.
n xlab xval
n ylab yval
n x xlen
n y ylen
brief
if xlen ne ylen
note
note **Error** Columns must be of equal length.
note
exit
endif
if xval ne 2 or yval ne 2
note
note **Error** Each column must have exactly two distinct levels.
note
exit
endif
if xlab ne ylab
note
note **Error** Each column must have the same two distinct levels.
note
exit
endif
brief 0
# If the column lengths are not the same at this point, the macro will be automatically terminated.
dtype x typex
dtype y typey
if typex = 0 and typey = 0
copy x y x2 y2;
Exclude;
Where "x = "" "" or y = "" """.
elseif typex ne 0 and typex ne 0 and typex = typey
copy x y x2 y2;
Exclude;
Where "x = '*' or y = '*'".
endif
n x2 obserx
n y2 obsery
brief 1
if obserx ne obsery
note
note **Error** Since the data is matched, each column must have the same number of observations.
note
exit
endif
brief 0
# This assumes the raw data is text or numeric.
let ct = count(x2)
Set dummy
1( 1 : 1 / 1 )ct
End.
Statistics dummy;
By x2 y2;
Sums cellct.
let a = cellct(1)
let b = cellct(2)
let c = cellct(3)
let d = cellct(4)
if a='*'
let a=0
endif
if b='*'
let b=0
endif
if c='*'
let c=0
endif
if d='*'
let d=0
endif
else
brief 1
note
note **Error** The first input parameter can only take values of 1 or 2.
note
exit
endif
let bc=b+c
brief
if bc < 10
note
note **Warning** Test statistic may not be approximated well by the chisquare distribution in this case.
note A sign test may be more appropriate.
note
endif
brief 0
if correction = 0
let chi = (b-c)**2/(b+c)
else
let chi = ((abs(b-c)-1)**2)/(b+c)
endif
cdf chi k1;
chisquare 1.
let pval = 1-k1
let delta = (c-b)/(a+b+c+d)
let prob = 1-(1-conf)/2
invcdf prob k;
normal 0 1.
let lowci = delta - k*sqrt(((a+b+c+d)*(b+c) - (b-c)**2)/(a+b+c+d)**3)
let upci = delta + k*sqrt(((a+b+c+d)*(b+c) - (b-c)**2)/(a+b+c+d)**3)
brief 1
if correction = 0
mtitle "McNemar's Test for Marginal Homogeneity"
else
mtitle "McNemar's Test for Marginal Homogeneity with Continuity Correction"
endif
let conf=conf*100
let civals[1]=conf
let civals[2]=lowci
let civals[3]=upci
text civals civals
let tconf=left(civals[1],2)
let tlowci=civals[2]
let tupci=civals[3]
kkset phr1 "The "
kkset phr2 "% CI for delta is: "
kkset phr3 "("
kkset phr4 ","
kkset phr5 ")."
kkcat phr1 tconf phr1
kkcat phr1 phr2 phr1
kkcat phr3 tlowci val
kkcat val phr4 val
kkcat val tupci val
kkcat val phr5 val
kkcat phr1 val val
let cint = val
name chi "Chi-Square Statistic:"
name pval "P-value:"
print chi pval
write cint
write "where delta represents the difference of the observed marginal proportions."
endmacro
Disclaimer:
Minitab Inc. provides the Macro Library as a convenience only. Minitab neither endorses, supports, nor verifies the accuracy of any content, information, or functionality of any macro found in the Macro Library. Minitab specifically disclaims any and all responsibility or liability arising from or related to any reliance upon, use, or incorporation of any content, information, or macro found in the Macro Library.