I am struggeling rewriting matlab code in R as the hampel implementation in matlab seems to be different to the pracma package in R which, according to the window length leaves the lower and upper ends of times series untouched, whicle matlab seems to modify them. I have highlighted the problem already on stackoverflow: https://stackoverflow.com/questions/64631709/hampel-matlab-vs-r
As I would really like to know where the discrepancies are coming from, help is highly appreciated!
With kind regards
CV

Respuestas (1)

Bjorn Gustavsson
Bjorn Gustavsson el 9 de Nov. de 2020

0 votos

You seem to use different filter lengths in your call to hampel in matlab (30 - which I take to mean you will get a window of 2*30+1 samples, i.e. 30 samples before and 30 samples after the central point) and R (15 which I find very hard to believe gives you a total filter-length of 61.) Since the matlab-code is available to inspect you can simply go to the code to see what's done, R is an open source project so that source-code should also be available. Perhaps R uses median absolute deviation an matlab uses mean absolute deviation? I cant recall what the default of mad in matlab is at the moment.
HTH

4 comentarios

Claudio Viecelli
Claudio Viecelli el 9 de Nov. de 2020
Thank you very much for your answer. I corrected the windows length but still experiencing differences :(
Bjorn Gustavsson
Bjorn Gustavsson el 9 de Nov. de 2020
Have you compared the source-codes?
Claudio Viecelli
Claudio Viecelli el 9 de Nov. de 2020
Examining the differences between the two implementations, I found out that R leaves the window-length at the beginning and ending of the time series untouched while matlab is modifying it. How exactly, I havent found out yet.
Bjorn Gustavsson
Bjorn Gustavsson el 9 de Nov. de 2020
My guess for the matlab-hampel's handling is that it simply cuts out all indices that fall outside your array-limits, something like this:
idxWindow = [max(1,idxCentre-K):min(array_length,idxCentre+K)]
and then calculates the mad on that array. You'll have to investigate using a suitably small array where you can change the values at the ends and compare the output with what you'd calculate "by hand", using sensibly small K (3, 4, 5?)...
How R can use the full window-length when it runs outside the array-limits, is a question for someone else, perhaps simply repicating the last element?

Iniciar sesión para comentar.

Etiquetas

Preguntada:

el 9 de Nov. de 2020

Comentada:

el 9 de Nov. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by