Detrend - Maple Help

Statistics

 Detrend
 remove any trend from a set of data

 Calling Sequence Detrend( XY, options ) Detrend( Y, options ) Detrend( X, Y, options )

Parameters

 XY - data set, DataFrame; Matrix or DataFrame of values with 2 columns X, Y - data set, DataSeries; numeric list of values options - (optional) equation(s) of the form option=value where option can be mode

Options

 • mode    : one of the names mean or linear; specifies the method used to detrend the data. The mode mean removes the mean value of the data from the data. The mode linear removes the line of best-fit from the data. The default is linear.

Description

The Detrend command is used to remove any trend from a set of data. The Detrend command returns a Vector containing the detrended values.

 • If the argument XY is a 2-column rtable or DataFrame, the first column is used as the independent variables and the Detrend command is applied to the dependent values in the second column. Note that values in the first column do not need to be equally spaced.
 • If the argument Y is a 1-column rtable or DataSeries and there is no specified value for X, the Detrend command uses the index positions for each value in Y as the independent values and the Detrend command is applied to the dependent values in Y.
 • If the argument X and argument Y are 1-column rtables or DataSeries, the values in X are treated as the independent variables and the Detrend command is applied to the dependent values in Y.
 Notes To print the model used by the Detrend command, set infolevel[Statistics] to 1.

Examples

 > $\mathrm{with}\left(\mathrm{Statistics}\right):$

Specify some data:

 > $\mathrm{data}≔\mathrm{Matrix}\left(\left[\left[0,1.8\right],\left[1,0.7\right],\left[2.5,2.8\right],\left[4,4.2\right],\left[6.2,3\right]\right]\right)$
 $\left[\begin{array}{cc}0& 1.8\\ 1& 0.7\\ 2.5& 2.8\\ 4& 4.2\\ 6.2& 3\end{array}\right]$ (1)

Fit a linear model to the data:

 > $\mathrm{lm}≔\mathrm{LinearFit}\left(a+bx,\mathrm{data},x\right)$
 ${\mathrm{lm}}{≔}{1.49598376946009}{+}{0.366429281218947}{}{x}$ (2)

It can be observed that from the plot of the data and the linear model that there is some upward trend.

 > $\mathrm{plots}:-\mathrm{display}\left(\left[\mathrm{ScatterPlot}\left(\mathrm{data},\mathrm{symbol}=\mathrm{solidcircle},\mathrm{symbolsize}=15,\mathrm{color}="Black",\mathrm{legend}="Original Data"\right),\mathrm{plot}\left(\mathrm{lm},\mathrm{color}="Black",\mathrm{legend}="Trend",\mathrm{linestyle}=\mathrm{dash}\right)\right],\mathrm{view}=\left[\mathrm{min}\left(\mathrm{data}\left[..,1\right]\right)-0.1..\mathrm{max}\left(\mathrm{data}\left[..,1\right]\right)+0.1,\mathrm{min}\left(\mathrm{data}\left[..,2\right]\right)-0.1..\mathrm{max}\left(\mathrm{data}\left[..,2\right]\right)+0.1\right]\right)$

The Detrend command removes any trend from the data.

 > $\mathrm{detrend_data}≔\mathrm{Detrend}\left(\mathrm{data}\right)$
 $\left[\begin{array}{c}0.30401623053991367\\ -1.162413050679033\\ 0.38794302749254683\\ 1.2382991056641273\\ -0.7678453130175553\end{array}\right]$ (3)

This can be observed in the following plot:

 > $\mathrm{plots}:-\mathrm{display}\left(\left[\mathrm{ScatterPlot}\left(\mathrm{data},\mathrm{color}="Black",\mathrm{legend}="Original Data",\mathrm{symbol}=\mathrm{solidcircle},\mathrm{symbolsize}=15\right),\mathrm{plot}\left(\mathrm{lm},\mathrm{color}="Black",\mathrm{legend}="Trend",\mathrm{linestyle}=\mathrm{dash}\right),\mathrm{ScatterPlot}\left(\mathrm{data}\left[..,1\right],\mathrm{detrend_data},\mathrm{color}="Red",\mathrm{legend}="Detrended Data",\mathrm{symbol}=\mathrm{diamond},\mathrm{symbolsize}=15\right),\mathrm{plot}\left(\mathrm{Mean}\left(\mathrm{detrend_data}\right),\mathrm{color}="Red",\mathrm{legend}="Mean of Detrended Data",\mathrm{linestyle}=\mathrm{dot}\right)\right],\mathrm{view}=\left[\mathrm{min}\left(\mathrm{data}\left[..,1\right]\right)-0.1..\mathrm{max}\left(\mathrm{data}\left[..,1\right]\right)+0.1,\mathrm{default}\right]\right)$

Generate a random signal with an added trend.

 > $\mathrm{with}\left(\mathrm{SignalProcessing}\right):$
 > $\mathrm{signal_data}≔\mathrm{GenerateGaussian}\left(100,0.5,0.3\right)+⟨\mathrm{seq}\left(i,i=0.01..1,0.01\right)⟩$
 $\left[\begin{array}{cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc}0.5595371873412779& 0.45389430253864205& 0.8848753851846999& -0.47149036815707185& 0.8389029078187195& 0.47877229884860206& 1.1579020826194304& 0.9648531907159085& 0.6784179640720818& 0.4944664212889266& 0.2684350340055683& 0.2331441814045777& 0.7961879085760813& 1.4923335418697676& 0.41784844003340504& 0.41586887525427263& 0.7994512485834256& 0.9189902991795815& 0.7477827426178085& 0.5595947461678564& 0.8456515648990741& 0.43385597843666607& 0.21410259055455141& 0.681131964717453& 0.6411582723587601& 0.9288535123912929& 0.6474902506617086& 0.5043108274126352& 0.9827530477978295& 0.6765821635087408& 0.9524507828985362& 0.5987918066401041& 0.5135102804094569& 1.1137169060662873& 0.6637887196861881& 0.8287372676763043& 0.9828061389103757& 0.8033191354509455& 0.8125066283808304& 1.020236676992773& 1.0475575699239938& 0.6732770324610202& 1.3251975110948568& 0.620965168568038& 0.6704096894008563& 1.5824916572278682& 0.5225858466123751& 1.186895114183928& 0.9282115932830959& 0.6948168413356008& 0.36968181976413717& 0.608428908432865& 1.2661104532798322& 0.7820333574795313& 1.0248193914065142& 0.9060860832633068& 0.8805757416108783& 0.9137552089298038& 0.8285159060356576& 1.3394702950470418& 1.2508329627354744& 1.1832266243502314& 0.9186525844724822& 0.8378516601196335& 1.1615353135151898& 1.2013914427980636& 1.3765861283576561& 1.335684371153829& 1.0226417274459687& 1.3064270035335612& 1.136547269117135& 1.3351100446290296& 1.3780577047464586& 1.8816505512289048& 0.8672180251437505& 1.0104551922973328& 1.393984290873147& 1.4043262984189027& 1.2342347845980983& 1.434497354682656& 1.4546627193121924& 1.1322719229440406& 1.474937377043506& 1.0771768026141457& 1.457820694449464& 1.0126629278783643& 1.1687681349363668& 1.17175165733169& 2.090125104727279& 1.466201478690468& 1.2358938622209559& 1.8375665498300284& 1.1519829274283875& 1.1963758244926963& 1.2900141935977454& 1.0510595917792755& 1.7189748894859262& 1.865054954656632& 1.552648819618047& 1.2634576295297608\end{array}\right]$ (4)

Plot the signal and the trend line.

 > $\mathrm{plots}:-\mathrm{display}\left(\left[\mathrm{SignalPlot}\left(\mathrm{signal_data},\mathrm{legend}="Data"\right),\mathrm{plot}\left(\mathrm{LinearFit}\left(a+bx,⟨\mathrm{seq}\left(1..100\right)⟩,\mathrm{signal_data},x\right),x=1..100,\mathrm{color}="Black",\mathrm{legend}="Trend",\mathrm{linestyle}=\mathrm{dash}\right)\right]\right)$

Plot the signal, the detrended signal, the trend line and the mean of the detrended data.

 > $\mathrm{plots}:-\mathrm{display}\left(\left[\mathrm{SignalPlot}\left(\mathrm{signal_data},\mathrm{legend}="Original Data"\right),\mathrm{plot}\left(\mathrm{LinearFit}\left(a+bx,⟨\mathrm{seq}\left(1..100\right)⟩,\mathrm{signal_data},x\right),x=1..100,\mathrm{color}="Black",\mathrm{legend}="Trend",\mathrm{linestyle}=\mathrm{dash}\right),\mathrm{SignalPlot}\left(\mathrm{signal_data},\mathrm{detrend},\mathrm{color}="Red",\mathrm{legend}="Detrended Data"\right),\mathrm{plot}\left(\mathrm{Mean}\left(\mathrm{Detrend}\left(\mathrm{signal_data}\right)\right),x=1..100,\mathrm{color}="Red",\mathrm{legend}="Mean of Detrended Data",\mathrm{linestyle}=\mathrm{dot}\right)\right]\right)$
 > 

Compatibility

 • The Statistics[Detrend] command was introduced in Maple 2019.