How to calculate trend value per year for time series data?

17 visualizaciones (últimos 30 días)
Bishwajit Roy
Bishwajit Roy el 6 de Nov. de 2018
Comentada: dpb el 16 de Mzo. de 2021
I have data of water level (both maximum and minimum in a scale of the meter) on a daily basis for a certain number of years. The data set has some missing values as well. I have replaced those missing values by NaN. I want to calculate the trend in changes of water level per year. I also need to compute the standard error, to see if the trends are statistically significant or not. Is there any MATLAB functions available for doing this analysis? I used the 12 months running mean to calculate the annual mean for removing the seasonal cycles (codes are as follows).
alldays=dados.textdata;
[ntotal nada]=size(alldays)
water=dados.data;
[nvalues nada]=size(water)
max_monthly=water(:,1);
min_monthly=water(:,2);
bissexto=[1952 1956 1960 1964 1968 1972 1976 1980 1984 1988 1992 1996 2000 2004 2008 2012 2016 2020 2024 2028 2032 2036 2040 2044 2048 2052 2056 2060 2064 2068 2072 2076 2080 2084 2088 2092 2096];
figure
plot(max_monthly,'r-')
hold on
plot(min_monthly,'b-')
ylim([-4 6])
% close all
%%%%%%%%Starting day %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
day=1;
month=4;
year=1990;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
m=1;
k=1;
p=1;
nyear=1;
[tam nada]=size(max_monthly);
d=1;
som1=max_monthly(1);
som2=min_monthly(1);
contador=2;
while (d<tam)
d=d+1;
f=find (year==bissexto);
TF=isempty(f);
if(TF==1)
ndays=[31 28 31 30 31 30 31 31 30 31 30 31];
else
ndays=[31 29 31 30 31 30 31 31 30 31 30 31];
% ano
end
if(day>=ndays(month))
soma1=nansum(som1);
soma2=nansum(som2);
avg1=soma1./ndays(month);
avg2=soma2./ndays(month);
% if avg1==0
% avg1=NaN;
% end
% if avg2==0
% avg2=NaN;
% end
if(month<12)
max_monthly(m)=[avg1];
min_monthly(m)=[avg2];
max_allmonths(p)=avg1;
min_allmonths(p)=avg2;
clear avg1 avg2 contador som1 som2 soma1 soma2
contador=1;
m=m+1;
k=k+1;
p=p+1;
end
day=1;
som1=max_monthly(d);
som2=min_monthly(d);
contador=contador+1;
month=month+1;
else
som1(contador)=max_monthly(d);
som2(contador)=min_monthly(d);
contador=contador+1;
day=day+1;
end
if(month>12)
max_year(nyear)=mean(max_allmonths);
min_year(nyear)=mean(min_allmonths);
k=1;
month=1;
year=year+1;
nyear=nyear+1;
else
month=month;
year=year;
end
clear ndays TF f figura3
end
%%Difference
DifMaxMin=max_year-abs(min_year);
figure
plot(max_year,'r-')
hold on
plot(min_year,'b-')
figure
subplot(2,1,1)
plot(1:tam,max_monthly,'r-')
hold on
plot(min_monthly,'b-')
subplot(2,1,2)
plot(1:length(max_year),max_year,'r-')
hold on
plot(1:length(min_year),min_year,'b-')
I am very new in MATLAB. So, any kind of help will be appreciated immensely! Thanks again!
  2 comentarios
dpb
dpb el 6 de Nov. de 2018
Import into a timetable and use retime and you'll be done in just a line or two...
Bishwajit Roy
Bishwajit Roy el 6 de Nov. de 2018
Hi, I revised my question. Can you be bit specific or provide me the functions or code, please? Thanks! Roy

Iniciar sesión para comentar.

Respuesta aceptada

dpb
dpb el 7 de Nov. de 2018
For starters using timetable and retime...
opts=detectImportOptions('barguna.txt'); % build basic import options object
opts=setvartype(opts,1,'datetime'); % set the first column datetime type
opts=setvaropts(opts,1,'InputFormat','dd-MM-yyyy'); % and the input format for it
opts.VariableNames={'Date','MaxLev','MinLev'}; % define useful variable names
tt=table2timetable(readtable('barguna.txt',opts)); % and read table, convert to timetable
yrmeans=retime(tt,'yearly',@nanmean); % compute the annual means
yr=yrmeans.Date; yr.Format='yyyy'; yrmeans.Date=yr; % change to only display year
>> yrmeans % see what we got...
yrmeans =
29×2 timetable
Date MaxLev MinLev
____ ______ _________
1990 2.0609 0.090364
1991 1.8643 -0.094959
1992 1.876 -0.056967
1993 1.9142 -0.013014
1994 1.8547 -0.12948
1995 1.8788 -0.066192
1996 1.8744 -0.076093
1997 1.7816 -0.18792
1998 1.8968 0.025753
1999 2.0535 -0.046767
2000 2.0557 -0.028197
2001 2.1407 0.11874
2002 2.0523 -0.097019
2003 2.3064 0.18219
2004 1.6076 0.30488
2005 1.6687 0.53648
2006 1.6655 0.36096
2007 1.805 0.31266
2008 1.8197 0.01776
2009 1.9946 0.025644
2010 2.0095 0.17868
2011 2.0188 0.096154
2012 1.8374 0.15153
2013 2.0172 0.17121
2014 2.036 0.11656
2015 1.9182 0.099836
2016 2.0025 0.25519
2017 2.033 0.14581
2018 NaN NaN
>>
Other functions can be done similarly...
  11 comentarios
Bhowmik.U
Bhowmik.U el 16 de Mzo. de 2021
what if alternative of retime in matlab earlier versions?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by