macro
# Macro: MKTREND.MAC
# Written by: Cathy Akritas, July 2004
# Updated: May 2011 to increase the speed of the calculations
# Version: Minitab 18
#
# This macro calculates the Mann-Kendall test for detecting a trend in
# data collected over time. An adjustment is made for tied observations
# in this non-parametric test. You must have at least 10 observations
# for the Normal approximation to be appropriate.
#
# Suppose your data is in C2. To run the macro, choose Edit > Command
# Line Editor. Type the command
#
# %mktrend C2
#
# Click on Submit Commands.
#
# To change the value of alpha to .1 instead of the default .05, choose
# Edit > Command Line Editor and type the commands
#
# %mktrend C2;
# alpha .1.
#
# Click on Submit Commands.
#
# Reference: Section 4.3.4.1 at the link
# http://www.epa.gov/quality/qs-docs/g9s-final.pdf
#
###########################################################################
#
# 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.
#
###########################################################################
mktrend x;
alpha a.
mcolumn x d y freqs wp S VarS Z p temp tmp1 tmp2 tmp3
mconstant i k m nx nx1 ny y1 y2 xname lab1 lab2 tag a ttl1 ttl2 ttl3
mconstant ttl4 ttl5 nd az az1
default a = .05
mreset
notitle
brief 0
noecho
endmtitle
if a ge 1 or a le 0
mtitle "Mann-Kendall Trend Test Macro Error";
notitle.
brief 2
Print " ** Error ** Alpha must be greater than 0 and less than 1"
Print " Alpha = .05 will be used"
endmtitle
brief 0
let a = .05
endif
tsplot x
kkname xname x
kkset lab1 "No trend in "
kkcat lab1 xname lab2
let nx = n(x)
let nx1 = nx-1
if nx lt 10
mtitle "Mann-Kendall Trend Test Macro Error";
notitle.
brief 2
print " ** Error ** The number of observations in the data set must"
print " be greater than or equal to 10 for the Normal"
print " approximation method to produce valid results."
print " "
print " Macro Exiting"
endmtitle
goto 100
endif
copy x y
wdiff x y tmp3 tmp1 tmp2
let m = max(tmp1)
copy tmp3 d;
include;
where "tmp1 > tmp2".
erase tmp1 tmp2 tmp3
tally x ;
store y wp.
let y = sign(d)
tally y;
store y freqs.
let ny = n(y)
if ny = 1
if y = -1
let S = -1*freqs
elseif y = 0
let S = 0
elseif y = 1
let S = Freqs
endif
elseif ny = 2
let y1 = y[1]
let y2 = y[2]
if y1 = -1 and y2 = 1
let S = freqs[2] - freqs[1]
elseif y1 = -1 and y2 = 0
let S = -1*freqs[1]
elseif y1 = 0 and y2 = 1
let S = freqs[2]
endif
elseif ny = 3
let S = freqs[3] - freqs[1]
endif
let VarS =((nx*nx1*(2*nx+5)) - sum(wp*(wp-1)*(2*wp+5)))/18
if S > 0
let Z = (S-1)/sqrt(VarS)
elseif S = 0
let Z = 0
elseif S < 0
let Z = (S+1)/sqrt(varS)
endif
cdf Z p
let p = 1-p ### p-value for upward trend.
copy a tmp1
text tmp1 tmp1;
maxwidth 12.
copy tmp1 ttl1
kkset ttl2 "At alpha = "
kkcat ttl2 ttl1 ttl3
if p lt a
let ttl1 = ", there is enough evidence to determine that there"
else
let ttl1 = ", there is not enough evidence to determine that there"
endif
kkcat ttl3 ttl1 ttl4
let tmp1[1] = ttl4
let tmp1[2] = " is an upward trend."
let ttl1 = "Ha: There is an upward trend, the p-value = "
text p tmp2;
maxwidth 12.
copy tmp2 ttl2
kkcat ttl1 ttl2 ttl5
stack ttl5 tmp1 tmp1
#let az1 = "The calculated z = "
copy z az
#concat az1 az az
name lab2 " Ho: " az " The calculated z = "
mtitle "Mann-Kendall Trend Test by Normal Approximation";
notitle.
brief 2
print az
print lab2
stack " " "Test for Upward Trend" tmp1 tmp1
print tmp1;
format(A80).
cdf Z p ### p-value for downward trend
erase tmp1
if p lt a
let ttl1 = ", there is enough evidence to determine that there"
else
let ttl1 = ", there is not enough evidence to determine that there"
endif
kkcat ttl3 ttl1 ttl4
let tmp1[1] = ttl4
let tmp1[2] = " is a downward trend."
let ttl1 = "Ha: There is a Downward trend, the p-value = "
text p tmp2;
maxwidth 12.
copy tmp2 ttl2
kkcat ttl1 ttl2 ttl5
stack ttl5 tmp1 tmp1
stack " " "Test for Downward Trend" tmp1 tmp1
print tmp1;
format (A80).
mlabel 100
endmtitle
endmacro