Tietokoneharjoitus 7

Data sisaltaa 2 muuttujaa:

RealGDP: Quarterly values of Real GDP for the United States in Billions of Chained (2000) Dollars Seasonally Adjusted, Annual Rate.

TBillRate: Quarterly values of the rate on 3-month Treasury Bills. Quaterterly averages of daily rates in percentage points at an annual rate.

Datan lukeminen R:aan

file<-"http://cc.oulu.fi/~jklemela/econometrics/USMacro_Quarterly.csv"
data<-read.table(file,skip=1,sep=",")

Datan lukeminen SAS:iin

FILENAME myurl URL 'http://cc.oulu.fi/~jklemela/econometrics/USMacro_Quarterly.txt';

DATA USmacro;
   INFILE myurl firstobs=2;
   INPUT time $ gdpq tbill;
RUN;

Tehtävä 5

Olkoon Y(t) = log(RealGDP(t)) - log(RealGDP(t-1)) ja X(t) = TbillRate(t) - TbillRate(t-1)

Lasketaan Newey-West estimaatti virheiden kovarianssimatriisille.

# Luetaan data ja muokataan se

file<-"http://cc.oulu.fi/~jklemela/econometrics/USMacro_Quarterly.csv"
data<-read.table(file,skip=1,sep=",")

gdp<-data[,2]
r<-data[,3]
n<-length(r)

y0<-log(gdp[2:n])-log(gdp[1:(n-1)])
plot(y0,type="l")
x0<-r[2:n]-r[1:(n-1)]
plot(x0,type="l")

T1<-length(x0)
y<-y0[2:T1]
x<-x0[1:(T1-1)]
plot(x,y)

#####################

K<-2
T<-length(y)
Y<-matrix(y,length(y),1)
X<-matrix(0,length(x),K)
X[,1]<-1
X[,2]<-x

xtx<-t(X)%*%X
inv.xtx<-solve(xtx,diag(K))
bls<-inv.xtx%*%t(X)%*%Y
e<-Y-X%*%bls

L<-6
w<-pmax(1-seq(0,(T-1))/(L+1),0)
#w<-pmax(1-(seq(0,(T-1))/(L+1))^2,0)
Omega<-matrix(0,T,T)
for (i in 1:T){
   for (j in 1:T){
       Omega[i,j]<-w[abs(i-j)+1]*e[i]*e[j]
   }
}
Q<-t(X)%*%X/T
invQ<-solve(Q,diag(K))
P<-t(X)%*%Omega%*%X/T
HAC<-invQ%*%P%*%invQ/T
HAC

             [,1]         [,2]
[1,] 6.683422e-07 1.141122e-08
[2,] 1.141122e-08 8.072963e-07


##############################

library(sandwich)
model<-lm(y ~ x)

bwNeweyWest(model)
#[1] 6.092293

NeweyWest(model)

            (Intercept)            x
(Intercept) 7.571355e-07 2.400810e-08
x           2.400810e-08 7.921764e-07


NeweyWest(model,prewhite=FALSE)

              (Intercept)             x
(Intercept)  6.904282e-07 -2.529516e-10
x           -2.529516e-10  7.668061e-07

vcovHAC(model) 

             (Intercept)            x
(Intercept) 5.219359e-07 1.698986e-08
x           1.698986e-08 7.550875e-07


################################

library(car)

hccm()
vcov()

Kokeillaan SAS:ia.


FILENAME myurl URL 'http://cc.oulu.fi/~jklemela/econometrics/USMacro_Quarterly.txt';
DATA USmacro;
   INFILE myurl firstobs=2;
   INPUT time $ gdpq tbill;
   loggdpq = log(gdpq);
   y = loggdpq - lag1(loggdpq);
   x0 = tbill - lag1(tbill);
   x = lag1(x0);
RUN;

PROC MODEL;
   parms b0 b1;
   y = b0 + b1*x;
   fit y / GMM kernel=(bart,0,);
run;


 The SAS System               11:32 Thursday, February 20, 2014   3

                                             The MODEL Procedure

                                  Nonlinear GMM Summary of Residual Errors

                             DF       DF                                                        Adj
          Equation        Model    Error         SSE         MSE    Root MSE    R-Square       R-Sq

          y                   2      228      0.0224    0.000098     0.00992      0.0166     0.0123


                                     Nonlinear GMM Parameter Estimates

                                                      Approx                  Approx
                        Parameter       Estimate     Std Err    t Value     Pr > |t|

                        b0              0.008457    0.000654      12.93       <.0001
                        b1              0.001782    0.000887       2.01       0.0457


                             Number of Observations     Statistics for System

                             Used               230    Objective      1.289E-32
                             Missing              2    Objective*N    2.966E-30