How to apply Mann Kendall and sen slope?

Hey everyone,
I need to apply Mann Kendall and sen slope in matlab, I dwnloaded the functions however, the sen slope that I got doesn't make sense!
any help woud be highly appreciated!
Riyadh

 Respuesta aceptada

Star Strider
Star Strider el 23 de En. de 2026

0 votos

I assume you are referring to Mann-Kendall Tau-b with Sen's Method (enhanced). You need the Statistics and Machine Learning Toolbox licensed and installed to use it correctly, since it uses some of those functions.
Beyond that, it appears to be relatively straightforward.
What analysis are you doing, and what does not make sense? (It would definitely help to have your data and your code.)

6 comentarios

John D'Errico
John D'Errico el 23 de En. de 2026
Hmm. Would you then be a sen-sei? :)
Riyadh
Riyadh el 23 de En. de 2026
Editada: Torsten el 23 de En. de 2026
Thank you for your answer Star Strider,
My data and code is below:
years = (2001:2007);
xy=[24.80,24.25,23.75,24.52,24.38,24.9,25.24];
data=[years; xy];
alpha = 0.05; % significance level
[h,p] = Mann_Kendall(xy, alpha)
h = logical
0
p = 0.2296
[tau, h, sig, Z, S, senSlope] = ktaub(data, alpha)
Taub Message: When absolute value S=1, Continuity correction is setting S = 0. This will affect calculated significance. Taub Message: S = 0. P-value cannot = 100-percent. P-value is adjusted using S = 1 and should be reported as p > NaN. Taub Message: A non-zero Sens slope occurred when S =0. This is not an error, more a notification. This anomaly may occur because the median may be computed on one value equal to zero and one non-zero, etc.
tau = 1
h = 1
sig = NaN
Z = NaN
S = NaN
senSlope = 1
disp(['Tau: ', num2str(tau)]);
Tau: 1
disp(['Trend detected (h): ', num2str(h)]);
Trend detected (h): 1
disp(['Sen''s slope: ', num2str(senSlope)]);
Sen's slope: 1
midYear = median(years);
midValue = median(xy);
y_fit = midValue + senSlope * (years - midYear);
plot(years, xy, 'bo-', 'LineWidth', 1.5); hold on;
plot(years, y_fit, 'r--', 'LineWidth', 2);
legend('Observed Data','Sen''s Slope Trend');
Star Strider
Star Strider el 24 de En. de 2026
Editada: Star Strider el 24 de En. de 2026
Rather than going through the function code, I decided to code this myself.
That turns out to not be trivial.
EDIT -- (24 Jan 2026 at 17:30)
My approach is:
years = (2001:2007);
xy=[24.80,24.25,23.75,24.52,24.38,24.9,25.24];
data=[years; xy];
dx = diff(data(1,:));
dy = diff(data(2,:));
slopes = dy ./ dx
slopes = 1×6
-0.5500 -0.5000 0.7700 -0.1400 0.5200 0.3400
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
m = median(slopes) % Calculated Slope
m = 0.1000
x_norm = [0 cumsum(dx)];
resid = data(2,:) - m*x_norm;
b = median(resid) % Calculated Intercept
b = 24.2200
figure
plot(data(1,:), data(2,:), 'o-', LineWidth=2, MarkerFaceColor='b', DisplayName='Data')
hold on
plot(data(1,:), x_norm*m + b, '--', LineWidth=2, DisplayName=sprintf('Thiel-Sen Regression, y = %.2f\\cdotx%+.2f',m,b))
hold off
legend(Location='best')
That part was relatively straightforward.
I am unable to understand how to apply Kendall's tau-b to this to get a significance probability on the slope.
The slope is normally distributed, so the confidence limits of it are:
CI95 = norminv([0.025 0.975], mean(slopes), std(slopes)) + mean(slopes)
CI95 = 1×2
-0.9335 1.2269
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
If this interpretation is correct, that would indicate that it is not significantly different from zero with these data.
EDIT -- (24 Jan 2026 at 20:00)
Using the MATLAB function with the 'Kendall' 'Type' option produces --
[r,p] = corr(data(1,:).', data(2,:).', Type='Kendall')
r = 0.4286
p = 0.2389
.
Torsten
Torsten el 24 de En. de 2026
Editada: Torsten el 24 de En. de 2026
That's what I get from
years = 2001:2007;
xy = [24.80,24.25,23.75,24.52,24.38,24.9,25.24];
comb = nchoosek(1:7,2);
slopes = (xy(comb(:,1))-xy(comb(:,2)))./(years(comb(:,1))-years(comb(:,2)));
m = median(slopes);
s = sort(slopes)
s = 1×21
-0.5500 -0.5250 -0.5000 -0.1400 -0.1050 -0.0933 0.0200 0.0433 0.0733 0.1350 0.1625 0.1900 0.1980 0.2400 0.3150 0.3400 0.3725 0.3833 0.4300 0.5200 0.7700
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
s(1)
ans = -0.5500
s(end)
ans = 0.7700
n = numel(s);
s(ceil(0.025*n))
ans = -0.5500
s(ceil(0.975*n))
ans = 0.7700
intercept = median(xy-m*years);
y1_fit = intercept + m*years;
y2_fit = polyval(polyfit(years,xy,1),years);
plot(years, xy, 'bo-', 'LineWidth', 1.5); hold on;
plot(years, y1_fit, 'r--', 'LineWidth', 2);
plot(years, y2_fit, 'g--', 'LineWidth', 2);
legend('Observed Data','Sen''s Slope Trend','Usual Regression');
As you can see, computing a confidence interval for the little number of data points doesn't make sense here: the answer you get is that the slope m is to 95% in between the minimum and maximum slope computed (what doesn't help).
Riyadh
Riyadh el 24 de En. de 2026
Thank you so much! appreciate it!
Riyadh
Riyadh el 24 de En. de 2026
Thank you for your cooperation!
is the code of Man kendall here correct?
alpha = 0.05; % significance level
[h,p] = Mann_Kendall(xm, alpha);

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 23 de En. de 2026

Editada:

el 24 de En. de 2026

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by