Simple loop to eliminate outliers

For questions and discussion related to reading in and working with data.
wgavin
Posts: 2
Joined: Fri Dec 28, 2007 9:33 am

Simple loop to eliminate outliers

Unread post 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
Bill Gavin
(314) 444-8578
wgavin
Posts: 2
Joined: Fri Dec 28, 2007 9:33 am

%if

Unread post by wgavin »

Found the %if command.
Bill Gavin
(314) 444-8578
moderator
Site Admin
Posts: 269
Joined: Thu Oct 19, 2006 4:33 pm

Unread post 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.
Post Reply