# Tietokoneharjoitus 5

These data are taken from the 1980 Census. These data were provided by Professor William Evans of the University of Maryland and were used in his paper with Joshua Angrist Children and Thier Parentsâ€™ Labor Supply: Evidence from Exogenous Variation in Family Size, American Economic Review, June 1998, Vol. 88, No. 3, 450-477. The file Fertility.dta (in STATA format) contains data on 254,654 women between the age of 21 and 35. The data in Fertility are a subset of the data used in the Angrist-Evans paper. (The file Fertility_Small contains data on a 30,000 randomly selected women from the Fertility data set. This smaller dataset is provided for students with memory limitations on their computer software.)

• V1 = morekids = 1 if mom had more than 2 children
• V2 = boy1st = 1 if 1st child was a boy
• V3 = boy2nd = 1 if 2nd child was a boy
• V4 = samesex = 1 if 1st two children same sex
• V5 = agemom = age of mom at census
• V6 = black = 1 if mom is black
• V7 = hispan = 1 if mom is Hispanic
• V8 = othrace = 1 if mom is not black, Hispanic or white
• V9 = weeksmworked = mom's weeks worked in 1979

IV regressio kun y-muuttuja on weeksworked, x-muuttuja on morekids, instrumenttimumuuttuja on samesex seka agemom, black, hispan ja othrace ovat eksogeenisia selittavia muuttujia.

```file<-"http://cc.oulu.fi/~jklemela/econometrics/Fertility_small.csv"

y<-data[,9]   #weeksworked
x<-data[,1]   #morekids
z<-data[,4]   #samesex
w1<-data[,5]  #agemom
w2<-data[,6]  #black
w3<-data[,7]  #hispan
w4<-data[,8]  #othrace

# IV-estimaattori

n<-length(x)
K<-6
Z<-matrix(0,n,K)
Z[,1]<-1
Z[,2]<-z
Z[,3]<-w1
Z[,4]<-w2
Z[,5]<-w3
Z[,6]<-w4

X<-matrix(0,n,K)
X[,1]<-1
X[,2]<-x
X[,3]<-w1
X[,4]<-w2
X[,5]<-w3
X[,6]<-w4

ztx<-t(Z)%*%X
invztx<-solve(ztx,diag(K))
b<-invztx%*%t(Z)%*%y
b

[,1]
[1,] -4.3703424
[2,] -5.7807463
[3,]  0.8234973
[4,] 11.4262797
[5,] -0.4117677
[6,]  3.3077888

Q<-1
r<-0
R<-matrix(c(0,0,0,0,1,-1),Q,K)

e<-y-X%*%b
s2<-sum(e^2)/(n-K)

A<-t(X)%*%Z/n
B<-s2*t(Z)%*%Z/n
invA<-solve(A,diag(1,K))
Avar<-t(invA)%*%B%*%invA/n

C<-R%*%Avar%*%t(R)
invC<-solve(C,diag(1,Q))

W<-t(R%*%b-r)%*%invC%*%(R%*%b-r)
W
1-pchisq(W,df=Q)

[1,] 12.8489
[1,] 0.0003376785

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

Q<-2
matrix(0,Q,1)
R<-matrix(0,Q,K)
R[1,]<-c(0,0,0,1,-1,0)
R[2,]<-c(0,0,0,0,1,-1)

e<-y-X%*%b
s2<-sum(e^2)/(n-K)

A<-t(X)%*%Z/n
B<-s2*t(Z)%*%Z/n
invA<-solve(A,diag(1,K))
Avar<-t(invA)%*%B%*%invA/n

C<-R%*%Avar%*%t(R)
invC<-solve(C,diag(1,Q))

W<-t(R%*%b-r)%*%invC%*%(R%*%b-r)
W
1-pchisq(W,df=Q)

[1,] 289.8421
[1,]    0

```