function y = deseas2(x,freq,p);
%DESEAS2   seasonal adjustment using dummy variables
%          y = deseas2(x,freq,p) seasonally adjusts the series x which
%          is sampled at frequency freq times per year (e.g., monthly 
%          data has freq=12, quarterly data has freq=4).   The 
%          deseasonalized series y is given by:
%
%               y = x - D*b 
%
%          where b is a matrix of coeficients from the regression:
%
%               x  = P*a + D*b + e, 
%
%          D is a matrix of dummy variables, and P is a polynomial in time:
%
%               D(t,i) =  1    if t occurs in subperiod i,   i=1,2,...,freq 
%                         0    otherwise
%             
%               P(t,i) =  t^i, i=1,2,...,p
%           

%          Ellen McGrattan, 10-6-97
%          Revised, ERM, 10-8-97

T          = length(x);
I          = eye(freq);
m          = fix(T/freq);
n          = rem(T,freq);
D          = kron(ones(m,1),I);
D          = [D;I(1:n,:)];
P          = ([1:T]'*ones(1,p)) .^ (ones(T,1)*[1:p]);
b          = ols([D,P],x);
b          = b(1:freq);
d          = ols([ones(T,1),P],x);
y          = x - D*b + d(1);

