Page 1 of 1

Simple loop to eliminate outliers

Posted: Wed Jan 02, 2008 11:06 am
by wgavin
I am trying to eliminate outliers in a factor analysis to replicate a procedure used by Stock and Watson.

I get the upper value for all observations--I suspect that it is using the number (rather that the period value) of the series in the logical statement.

faulty code:

cal 1983 1 12
all 0 2007:9
open data gavin-kliesen_data.rat
data(org=col,for=rat) /
close data
smpl 1983:1 1983:12

Print / d1 d2 d3 d4
smpl 1983:1 2007:9

dofor variable = 1 to 4
stat(fractiles,noprint) variable /
compute up_val = %median + 6*(%fract75-%median)
compute low_val = %median - 6*(%median-%fract25)

do i = 1983:1,2007:9,1
smpl i i

if variable > up_val
{
set variable = up_val
}
else if variable < low_val
{
set variable = low_val
}
else
{
set variable = variable
}
end do
smpl 1983:1 2007:9
end dofor x


smpl 1983:1 1983:12
Print / d1 d2 d3 d4


Any suggestions would be appreciated.

Bill

%if

Posted: Wed Jan 02, 2008 5:01 pm
by wgavin
Found the %if command.

Posted: Fri Jan 04, 2008 1:37 pm
by moderator
Sounds like you've already found the solution, but for others who might see this, your guess as to the problem:

"number (rather that the period value) " is exactly right. Because you weren't referencing a specific entry of the "variable" series, RATS was just comparing the series number. If you need to use loops, include the entry reference:

if variable(i) > up_val
...

But a single SET instruction with one or more %IF() functions on the right hand side is almost always the better way to go.