how can I Replace outliers with median of previous observations?

조회 수: 5 (최근 30일)
Nicolas
Nicolas 2012년 7월 19일
Hello i have some outliers in a 206*174 dataset matrix.. I want to replace them with the median of the 5 previous observaitons using a loop..
how can i do that?
[EDITED, copied from Answer section, Jan]
i will be more clear.. outliers are observations of stationary series with absolute deviations from the median which exceed six times the interquartile range. I want to replace them with the median of the preceding five observations. thanks
  댓글 수: 7
Nicolas
Nicolas 2012년 7월 19일
So i will repeat.. I have a data of 206*174 observations... rows is time observations and columns is variables.. i want to find the outliers that are defined the the median absolute deviations to be greater 6 times the interquartile range in each variable series.
after that i want to replace each outlier with the median of previous 5 rows. thanks
Nicolas
Nicolas 2012년 7월 19일
% %Now we remove outliers like the paper of Stock and Watson 2005(num=data)
[t n]=size(NUM)% row size of data
X=median(NUM) %find the median of each column of NUM
X1=repmat(X,t,1)% creates a large matrix that
% each column has n times the median value of the column
NUM1=NUM-X1 %substract each row to find the Mean absolute deviation
NUM1=abs(NUM(:,:))%take the absolute value
for j=1:n
Y(:,j)=iqr(NUM(:,j))% find the value of the difference between
%3 and 1 quartile.
end
Y1=repmat(Y,t,1)
NUM2=6*Y1% multiply each value x6
outliers=NUM1-NUM2 %an outlier is when the MAD>6*Diff inquartiles
[x w]=find(outliers>0)%x is the row and w the column of each outlier
v=ones(t,n)
v(outliers>0)=0
%Note here that some problems arise for very smooth series so we remove %them for further analysis v(:,[39;84;86;92;95])=1 [x w]=find(v==0)
NUM1=zeros(size(data_st)) j=1
for i=1:t
if v(i,j)==0
NUM1(i,:)=NUM(median(NUM(i-6:i-1,:),1))
elseif v(i,:)==1
NUM1(i,:)=data_st(i,:)
end
j=j+1
if j==175
break
end
end
disp('Done')

댓글을 달려면 로그인하십시오.

답변 (1개)

Miro
Miro 2012년 7월 19일
편집: Miro 2012년 7월 19일
something like this should work
yourthreshold = 10;
Data(Data>yourthreshold) = median(median(Data));
this replaces all values being greater than 10.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

제품

Community Treasure Hunt

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

Start Hunting!

Translated by