[18937] in s-news-athena
Re: [S] moving median
daemon@ATHENA.MIT.EDU (Alan Zaslavsky)
Fri Aug 13 13:11:13 1999
From: Alan Zaslavsky <zaslavsk@hcp.med.harvard.edu>
Date: Fri, 13 Aug 1999 12:58:24 -0400 (EDT)
Message-Id: <199908131658.MAA17443@nightingale.hcp.med.harvard.edu>
To: fan@katan.cybercom.net
Cc: s-news@wubios.wustl.edu
From: fan@katan.cybercom.net
> This question is related to the previous moving average question:
> Instead of mean, I want to compute the 30-term moving median.
> The filter() and cumsum() methods will not work. There might be some
> S usage which works on this directly, other than using loop. If you have an
> idea, let me know.
If you blow up your 1e4+29 data points to a 30 X 1e4 matrix, where col
1 is the first 30 obs, col 2 is obs 2 to 31, etc. then you can do something
like this:
suppose X is the blown-up data matrix, and that all observations are between
-1 and 1 (you don't really need this).
XX_X+col(X)*3
XX[]_sort(XX)
running.median_(XX[15,]+XX[16,])/2-1:1e4*3
The col(X)*3 term in the first line guarantees that when you sort, each
column stays together and goes back into the same column.
The complexity of the last line is because you have an even number of
observations in the median.
If you want something that does not depend on a limitation on the range
of the variable, you can work with ranks.
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news