Tietokoneharjoitus 5

              storage  display     value
variable name   type   format      label      variable label
-------------------------------------------------------------------------------
county          int    %9.0g                  county identifier
year            byte   %9.0g                  81 to 87
crmrte          float  %9.0g                  crimes committed per person
prbarr          float  %9.0g                  'probability' of arrest
prbconv         float  %9.0g                  'probability' of conviction
prbpris         float  %9.0g                  'probability' of prison sentenc
avgsen          float  %9.0g                  avg. sentence, days
polpc           float  %9.0g                  police per capita
density         float  %9.0g                  people per sq. mile
taxpc           float  %9.0g                  tax revenue per capita
west            byte   %9.0g                  =1 if in western N.C.
central         byte   %9.0g                  =1 if in central N.C.
urban           byte   %9.0g                  =1 if in SMSA
pctmin80        float  %9.0g                  perc. minority, 1980
wcon            float  %9.0g                  weekly wage, construction
wtuc            float  %9.0g                  wkly wge, trns, util, commun
wtrd            float  %9.0g                  wkly wge, whlesle, retail trade
wfir            float  %9.0g                  wkly wge, fin, ins, real est
wser            float  %9.0g                  wkly wge, service industry
wmfg            float  %9.0g                  wkly wge, manufacturing
wfed            float  %9.0g                  wkly wge, fed employees
wsta            float  %9.0g                  wkly wge, state employees
wloc            float  %9.0g                  wkly wge, local gov emps
mix             float  %9.0g                  offense mix: face-to-face/other
pctymle         float  %9.0g                  percent young male
d82             byte   %9.0g                  =1 if year == 82
d83             byte   %9.0g                  =1 if year == 83
d84             byte   %9.0g                  =1 if year == 84
d85             byte   %9.0g                  =1 if year == 85
d86             byte   %9.0g                  =1 if year == 86
d87             byte   %9.0g                  =1 if year == 87
lcrmrte         float  %9.0g                  log(crmrte)
lprbarr         float  %9.0g                  log(prbarr)
lprbconv        float  %9.0g                  log(prbconv)
lprbpris        float  %9.0g                  log(prbpris)
lavgsen         float  %9.0g                  log(avgsen)
lpolpc          float  %9.0g                  log(polpc)
ldensity        float  %9.0g                  log(density)
ltaxpc          float  %9.0g                  log(taxpc)
lwcon           float  %9.0g                  log(wcon)
lwtuc           float  %9.0g                  log(wtuc)
lwtrd           float  %9.0g                  log(wtrd)
lwfir           float  %9.0g                  log(wfir)
lwser           float  %9.0g                  log(wser)
lwmfg           float  %9.0g                  log(wmfg)
lwfed           float  %9.0g                  log(wfed)
lwsta           float  %9.0g                  log(wsta)
lwloc           float  %9.0g                  log(wloc)
lmix            float  %9.0g                  log(mix)
lpctymle        float  %9.0g                  log(pctymle)
lpctmin         float  %9.0g                  log(pctmin)
clcrmrte        float  %9.0g                  lcrmrte - lcrmrte[_n-1]
clprbarr        float  %9.0g                  lprbarr - lprbarr[_n-1]
clprbcon        float  %9.0g                  lprbconv - lprbconv[_n-1]
clprbpri        float  %9.0g                  lprbpri - lprbpri[t-1]
clavgsen        float  %9.0g                  lavgsen - lavgsen[t-1]
clpolpc         float  %9.0g                  lpolpc - lpolpc[t-1]
cltaxpc         float  %9.0g                  ltaxpc - ltaxpc[t-1]
clmix           float  %9.0g                  lmix - lmix[t-1]

Tehtävä 5a

Estimoi lineaarinen malli POLS estimaattorilla (pooled ordinary least squares) käyttäen kaikkia vuosia 81-87, kun mallissa on mukana koostettu aikavaikutus (aggregate time effect). Käytä indikaattorimuuttujia (dummy variables) koostetun aikavaikutuksen estimoimiseen. Mallissa vastemuuttuja on log(crmrte) ja aikavaikutuksen lisäksi selittävät muuttujat ovat log(prbarr), log(prbconv), log(prbpris), log(avgsen) ja log(polpc).

file<-"http://cc.oulu.fi/~jklemela/panel/cornwell.raw"
data<-read.table(file=file)

y<-log(data[,3])

x1<-log(data[,4])
x2<-log(data[,5])
x3<-log(data[,6])
x4<-log(data[,7])
x5<-log(data[,8])

year<-data[,2]

d1<-as.numeric(year==81)
d2<-as.numeric(year==82)
d3<-as.numeric(year==83)
d4<-as.numeric(year==84)
d5<-as.numeric(year==85)
d6<-as.numeric(year==86)
d7<-as.numeric(year==87)


reg.model<-lm(y ~ d2+d3+d4+d5+d6+d7+x1+x2+x3+x4+x5)

summary(reg.model)

Call:
lm(formula = y ~ d2 + d3 + d4 + d5 + d6 + d7 + x1 + x2 + x3 + 
    x4 + x5)

Residuals:
     Min       1Q   Median       3Q      Max 
-1.89966 -0.18748  0.02896  0.23189  1.31319 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept) -2.082303   0.251625  -8.275  7.9e-16 ***
d2           0.005137   0.057931   0.089 0.929370    
d3          -0.043503   0.057624  -0.755 0.450575    
d4          -0.108753   0.057923  -1.878 0.060914 .  
d5          -0.078042   0.058324  -1.338 0.181365    
d6          -0.042077   0.057822  -0.728 0.467068    
d7          -0.027042   0.056899  -0.475 0.634771    
x1          -0.719503   0.036766 -19.570  < 2e-16 ***
x2          -0.545659   0.026368 -20.694  < 2e-16 ***
x3           0.247551   0.067227   3.682 0.000251 ***
x4          -0.086755   0.057920  -1.498 0.134686    
x5           0.365988   0.030025  12.189  < 2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.3789 on 618 degrees of freedom
Multiple R-squared:   0.57,	Adjusted R-squared:  0.5624 
F-statistic: 74.49 on 11 and 618 DF,  p-value: < 2.2e-16

---------------------------------------------------------------

reg.model<-lm(y ~ 0+d1+d2+d3+d4+d5+d6+d7+x1+x2+x3+x4+x5)

summary(reg.model)
 
Call:
lm(formula = y ~ 0 + d1 + d2 + d3 + d4 + d5 + d6 + d7 + x1 + 
    x2 + x3 + x4 + x5)

Residuals:
     Min       1Q   Median       3Q      Max 
-1.89966 -0.18748  0.02896  0.23189  1.31319 

Coefficients:
   Estimate Std. Error t value Pr(>|t|)    
d1 -2.08230    0.25162  -8.275  7.9e-16 ***
d2 -2.07717    0.24508  -8.476  < 2e-16 ***
d3 -2.12581    0.24544  -8.661  < 2e-16 ***
d4 -2.19106    0.24265  -9.030  < 2e-16 ***
d5 -2.16035    0.24238  -8.913  < 2e-16 ***
d6 -2.12438    0.24582  -8.642  < 2e-16 ***
d7 -2.10935    0.24814  -8.501  < 2e-16 ***
x1 -0.71950    0.03677 -19.570  < 2e-16 ***
x2 -0.54566    0.02637 -20.694  < 2e-16 ***
x3  0.24755    0.06723   3.682 0.000251 ***
x4 -0.08676    0.05792  -1.498 0.134686    
x5  0.36599    0.03003  12.189  < 2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.3789 on 618 degrees of freedom
Multiple R-squared:  0.9895,	Adjusted R-squared:  0.9892 
F-statistic:  4831 on 12 and 618 DF,  p-value: < 2.2e-16

Tehtävä 5b

Estimoi matriisi Omega = Euiui', kun ui on T kertaa 1 vektori, joka sisältää residuaalit lineaarisesta mallista, kun vastemuuttuja on log(crmrte) ja selittävät muuttujat ovat log(prbarr), log(prbconv), log(prbpris), log(avgsen) ja log(polpc). Kaikki vuodet 81-87 sisältyvät malliin. Käytä POLS-residuaaleja

y<-log(data[,3])

x1<-log(data[,4])
x2<-log(data[,5])
x3<-log(data[,6])
x4<-log(data[,7])
x5<-log(data[,8])

reg.model<-lm(y ~ x1+x2+x3+x4+x5)

summary(reg.model)

resi<-residuals(reg.model)

county<-data[,1]
year<-data[,2]
uc<-unique(county)
uy<-unique(year)
N<-length(uc)
T<-length(uy)

U<-matrix(0,T,N)
for (i in 1:N){
   now<-uc[i]
   ind<-(county==now)
   U[,i]<-resi[ind]
}

omegahat<-U%*%t(U)/N

          [,1]       [,2]       [,3]       [,4]       [,5]       [,6]
[1,] 0.13295214 0.10691554 0.08725805 0.09418204 0.09834043 0.07883118
[2,] 0.10691554 0.13246877 0.09984562 0.10790282 0.10569842 0.08345536
[3,] 0.08725805 0.09984562 0.12093801 0.10635122 0.10554241 0.08992647
[4,] 0.09418204 0.10790282 0.10635122 0.15925729 0.12980230 0.10205059
[5,] 0.09834043 0.10569842 0.10554241 0.12980230 0.16121446 0.10989033
[6,] 0.07883118 0.08345536 0.08992647 0.10205059 0.10989033 0.12945747
[7,] 0.09169674 0.10168747 0.08737220 0.10057430 0.10401350 0.10791154
           [,7]
[1,] 0.09169674
[2,] 0.10168747
[3,] 0.08737220
[4,] 0.10057430
[5,] 0.10401350
[6,] 0.10791154
[7,] 0.15928928


--------------------------------------------------

y<-matrix(y,length(y),1)
K<-6
x<-matrix(1,length(y),K)
x[,2]<-x1
x[,3]<-x2
x[,4]<-x3
x[,5]<-x4
x[,6]<-x5

I<-diag(1,K)
xtxinv<-solve(t(x)%*%x,I)
betahat<-xtxinv%*%t(x)%*%y

county<-data[,1]
year<-data[,2]
uc<-unique(county)
uy<-unique(year)
N<-length(uc)
T<-length(uy)

U<-matrix(0,T,N)
for (i in 1:N){
   now<-uc[i]
   ind<-(county==now)
   yi<-y[ind]
   xi<-x[ind,]
   U[,i]<-yi-xi%*%betahat
}

omegahat<-U%*%t(U)/N

          [,1]       [,2]       [,3]       [,4]       [,5]       [,6]
[1,] 0.13295214 0.10691554 0.08725805 0.09418204 0.09834043 0.07883118
[2,] 0.10691554 0.13246877 0.09984562 0.10790282 0.10569842 0.08345536
[3,] 0.08725805 0.09984562 0.12093801 0.10635122 0.10554241 0.08992647
[4,] 0.09418204 0.10790282 0.10635122 0.15925729 0.12980230 0.10205059
[5,] 0.09834043 0.10569842 0.10554241 0.12980230 0.16121446 0.10989033
[6,] 0.07883118 0.08345536 0.08992647 0.10205059 0.10989033 0.12945747
[7,] 0.09169674 0.10168747 0.08737220 0.10057430 0.10401350 0.10791154
           [,7]
[1,] 0.09169674
[2,] 0.10168747
[3,] 0.08737220
[4,] 0.10057430
[5,] 0.10401350
[6,] 0.10791154
[7,] 0.15928928

Tehtävä 5c

Estimoi matriisi Omega = Euiui', kun ui on T kertaa 1 vektori, joka sisältää residuaalit lineaarisesta mallista, kun vastemuuttuja on log(crmrte) ja selittävät muuttujat ovat log(prbarr), log(prbconv), log(prbpris), log(avgsen) ja log(polpc). Kaikki vuodet 81-87 sisältyvät malliin. Käytä POLS-residuaaleja

y<-matrix(y,length(y),1)
K<-6
x<-matrix(1,length(y),K)
x[,2]<-x1
x[,3]<-x2
x[,4]<-x3
x[,5]<-x4
x[,6]<-x5

I<-diag(1,K)
xtxinv<-solve(t(x)%*%x,I)

betahat<-xtxinv%*%t(x)%*%y

county<-data[,1]
year<-data[,2]
uc<-unique(county)
uy<-unique(year)
N<-length(uc)
T<-length(uy)

U<-matrix(0,T,N)
for (i in 1:N){
   now<-uc[i]
   ind<-(county==now)
   yi<-y[ind]
   xi<-x[ind,]
   U[,i]<-yi-xi%*%betahat
}

sigmav2<-sum(U^2)/(N*T)
sigmac2<-0
for (i in 1:N){
    for (t in 1:(T-1)){
        for (s in (t+1):T){
            sigmac2<-sigmac2+U[t,i]*U[s,i]
        }
     }
}
sigmac2<-2*sigmac2/(N*T*(T-1))
sigmau2<-sigmav2-sigmac2

omegahat.re<-sigmav2*diag(1,T)+sigmac2*matrix(1,T,T)
omegahat.re

           [,1]       [,2]       [,3]       [,4]       [,5]       [,6]
[1,] 0.24218956 0.09996421 0.09996421 0.09996421 0.09996421 0.09996421
[2,] 0.09996421 0.24218956 0.09996421 0.09996421 0.09996421 0.09996421
[3,] 0.09996421 0.09996421 0.24218956 0.09996421 0.09996421 0.09996421
[4,] 0.09996421 0.09996421 0.09996421 0.24218956 0.09996421 0.09996421
[5,] 0.09996421 0.09996421 0.09996421 0.09996421 0.24218956 0.09996421
[6,] 0.09996421 0.09996421 0.09996421 0.09996421 0.09996421 0.24218956
[7,] 0.09996421 0.09996421 0.09996421 0.09996421 0.09996421 0.09996421
           [,7]
[1,] 0.09996421
[2,] 0.09996421
[3,] 0.09996421
[4,] 0.09996421
[5,] 0.09996421
[6,] 0.09996421
[7,] 0.24218956