Capítulo 15 Sistemas de Equacoes Lineares
Modelo yi=Xiβ+ui
onde: yi é Gx1, Xi é GxK, β é Kx1 e ui é Gx1.Sendo que K=k1+...+KG
15.0.1 SUR
Exemplo para dois indivíduos, 2 equações e 2 variáveis em cada equação
Nesse contexto x possui três dimensões: xikg
[yi1yi2]=[xi11xi210000xi12xi22][β11β21β12β22]+[ui1ui2]
[yi1yi2]=[xi11β11+xi21β21xi12β12+xi22β22]+[ui1ui2]
Para uma Amostra N
Y=Xβ+U
onde: Y é um vetor NGx1, X uma matriz NGxK, β um vetor Kx1 e U um vetor NGx1.
Definir as variáveis explicativas do modelo
= 1000
n set.seed(1)
= rnorm(n)
x1 = rnorm(n) x2
Definir os termos de erros, independentes porém correlacionados entre as equações.
library(mvtnorm)
= matrix(c(4,3,3,30), ncol=2)
sigma = rmvnorm(n, mean=c(0,0), sigma=sigma)
mv = matrix(c(mv[,1]),ncol = 1)
u1 = matrix(c(mv[,2]),ncol = 1)
u2 = rbind(u1,u2) u
Vetor β
= matrix(c(5,2,-1),ncol = 1)
beta1 = matrix(c(-5,-2,1),ncol = 1)
beta2 = rbind(beta1,beta2) beta
Organizando a matrix X
= cbind(1,x1,x2)
X1 = cbind(1,x1,x2)
X2 = rbind(cbind(X1,0,0,0),cbind(0,0,0,X2)) X
Criando o vetor Y
= X%*%beta + u Y
Estimador SOLS
= solve(t(X)%*%X)%*%t(X)%*%Y
Sols Sols
## [,1]
## 5.085939
## x1 1.979317
## x2 -1.104254
## -5.068044
## -2.089895
## 1.113130
Estimar Equação por Equação
= X1%*%beta1 + u1
y1 = X2%*%beta2 + u2 y2
= solve(t(X1)%*%X1)%*%t(X1)%*%y1
ols1 ols1
## [,1]
## 5.085939
## x1 1.979317
## x2 -1.104254
= solve(t(X2)%*%X2)%*%t(X2)%*%y2
ols2 ols2
## [,1]
## -5.068044
## x1 -2.089895
## x2 1.113130
Matriz de Variância e Covariância
ˆV(ˆβSOLS=(X′X)−1X′DX(X′X)
= Y - X%*%Sols
e_hat = c(e_hat)
e = e * e
e = diag(e)
D = solve(t(X)%*%X)%*%(t(X)%*%D%*%X)%*%solve(t(X)%*%X)
V_h = (n/(n-6)) * V_h
V_h = sqrt(diag(V_h))
ep_h ep_h
## x1 x2
## 0.06478291 0.05993167 0.06459487 0.18294688 0.17507186 0.17235189
= y1 - X1%*%ols1
e_hat1 = c(e_hat1)
e1 = e1 * e1
e1 = diag(e1)
D1 = solve(t(X1)%*%X1)%*%(t(X1)%*%D1%*%X1)%*%solve(t(X1)%*%X1)
V_h1 = (n/(n-3)) * V_h1
V_h1 = sqrt(diag(V_h1))
ep_h1 ep_h1
## x1 x2
## 0.06468537 0.05984143 0.06449761
= y2 - X2%*%ols2
e_hat2 = c(e_hat2)
e2 = e2 * e2
e2 = diag(e2)
D2 = solve(t(X2)%*%X2)%*%(t(X2)%*%D2%*%X2)%*%solve(t(X2)%*%X2)
V_h2 = (n/(n-3)) * V_h2
V_h2 = sqrt(diag(V_h2))
ep_h2 ep_h2
## x1 x2
## 0.1826714 0.1748083 0.1720924
15.1 FGLS
Estimar a matriz Ω=E(uiu′i).
ˆΩ=N−1∑Ni=1uiu′i=UU′
= y1 - X1%*%Sols[c(1,2,3),1]
e_hat1 = y2 - X2%*%Sols[c(4,5,6),1]
e_hat2 = cbind(e_hat1,e_hat2)
e_hat
= t(e_hat)%*%e_hat sigma
S=(IN⊗Ω−1)
= diag(n)
I = kronecker(I,solve(sigma)) ## Wooldridge S
ˆβFgls=[X′(Ω−1⊗IN)X]−1[X′(Ω−1⊗IN)Y]
= solve(t(X)%*%(S)%*%X)%*%(t(X)%*%(S)%*%Y) ## Wooldridge
Bfgls Bfgls
## [,1]
## 4.982906
## x1 1.938422
## x2 -1.129926
## -5.211998
## -2.191069
## 1.046103
= solve(t(X)%*%S%*%X)%*%(t(X)%*%S%*%D%*%S%*%X)%*%solve(t(X)%*%S%*%X)
V = (1/(n-6)) * V
V = sqrt(diag(V))
ep ep
## x1 x2
## 0.002822832 0.002279409 0.002360631 0.007680877 0.006445076 0.006601576
ˆV(ˆβFgls)=N−1(X′(Ω−1⊗IN)X)
= solve(t(X)%*%(S)%*%X)
V_h = (1/(n-6)) * V_h
V_h = sqrt(diag(V_h))
ep_h ep_h
## x1 x2
## 0.09158073 0.07939336 0.07606356 0.09158073 0.07939336 0.07606356
15.1.0.1 PAINEL
[yi1yi2]=[xi11xi21xi12xi22][β1β2]+[ui1ui2]
= 1000
n set.seed(1)
= rnorm(n)
x1 = rnorm(n) x2
= matrix(c(4,3,3,30), ncol=2)
sigma = rmvnorm(n, mean=c(0,0), sigma=sigma)
mv = matrix(c(mv[,1]),ncol = 1)
u1 = matrix(c(mv[,2]),ncol = 1)
u2 = rbind(u1,u2) u
= matrix(c(5,2,-1),ncol = 1) beta
= cbind(1,x1,x2)
X1 = cbind(1,x1,x2)
X2
= X1%*%beta + u1
y1 = X2%*%beta + u2
y2
= rbind(X1,X2)
X
= X%*%beta + u Y
Pooled OLS
= solve(t(X)%*%X)%*%t(X)%*%Y
Pols Pols
## [,1]
## 5.0089472
## x1 1.9447113
## x2 -0.9955618
= Y - X%*%Pols
e_hat = c(e_hat)
e = e * e
e = diag(e)
D = solve(t(X)%*%X)%*%(t(X)%*%D%*%X)%*%solve(t(X)%*%X)
V_h = (n/(n-3)) * V_h
V_h = sqrt(diag(V_h))
ep_h ep_h
## x1 x2
## 0.09694305 0.09244921 0.09192699
FGLS
= y1 - X1%*%Pols
e_hat1
= y2 - X2%*%Pols
e_hat2
= cbind(e_hat1,e_hat2) e_hat
= t(e_hat)%*%e_hat
sigma = diag(n)
I = kronecker(I,solve(sigma)) ## Wooldridge S
= solve(t(X)%*%(S)%*%X)%*%(t(X)%*%(S)%*%Y) ## Wooldridge
Bfgls Bfgls
## [,1]
## 4.885780
## x1 1.873816
## x2 -1.041929
= solve(t(X)%*%(S)%*%X)
V_h = (1/(n)) * V_h
V_h = sqrt(diag(V_h))
ep_h ep_h
## x1 x2
## 0.06470610 0.05614001 0.05378698
15.2 Sistema de Equações com Variávies Instrumentais
15.2.1 2SLS
= 1000
n set.seed(1)
# Create independent variables
= rnorm(n)
z1 = rnorm(n)
z2
# Create error
= rnorm(n)
u1 = rnorm(n)
u2
# Create endogenous variable
= .5*z1 - z2 + u1 + rnorm(n)
x1 = 1.5*z1 - 2*z2 - u2 + rnorm(n)
x2 = rnorm(n)
x3
# Create dependent variable
= 5 + 2*x1 - x2 + x3 + u1*5 - u2*2
y1 = -5 - 2*x1 + x2 - 2*x3 + u2*5 - u1*1
y2 #X = cbind(x1,x2,x3,1)
#Y = cbind(y1,y2)
= rbind(cbind(1,x1,x2,x3,0,0,0,0),cbind(0,0,0,0,1,x1,x2,x3))
X
= rbind(cbind(1,x3,z1,z2,0,0,0,0),cbind(0,0,0,0,1,x3,z1,z2))
Z = matrix(c(y1,y2),ncol=1) Y
Estimar por SOLS
= solve(t(X)%*%X)%*%t(X)%*%Y
Sols Sols
## [,1]
## 5.0262660
## x1 3.8164234
## x2 -1.2994949
## x3 0.8788473
## -4.9438961
## -1.5682791
## 0.1883747
## -1.9464426
= function(n){
sols # Create independent variables
= rnorm(n)
z1 = rnorm(n)
z2 # Create error
= rnorm(n)
u1 = rnorm(n)
u2 # Create endogenous variable
= .5*z1 - z2 + u1 + rnorm(n)
x1 = 1.5*z1 - 2*z2 - u2 + rnorm(n)
x2 = rnorm(n)
x3 # Create dependent variable
= 5 + 2*x1 - x2 + x3 + u1*5 - u2*2
y1 = -5 - 2*x1 + x2 - 2*x3 + u2*5 - u1*1
y2 = rbind(cbind(1,x1,x2,x3,0,0,0,0),cbind(0,0,0,0,1,x1,x2,x3))
X = rbind(cbind(1,x3,z1,z2,0,0,0,0),cbind(0,0,0,0,1,x3,z1,z2))
Z = matrix(c(y1,y2),ncol=1)
Y = solve(t(X)%*%X)%*%t(X)%*%Y
Sols
Sols
}= replicate(10000, expr = sols(1000))
z par(mfrow = c(2, 2))
plot(density(z[2, 1, ]), xlim = c(0,4))
abline(v = 2)
plot(density(z[3, 1, ]), xlim = c(-2,0))
abline(v = -1)
plot(density(z[6, 1, ]), xlim = c(-4,0))
abline(v = -2)
plot(density(z[7, 1, ]), xlim = c(0,2))
abline(v = 1)
ˆβ(2sls)=[X′Z(Z′Z)−1Z′X]−1
= solve(t(X)%*%Z%*%solve(t(Z)%*%Z)%*%t(Z)%*%X)%*%t(X)%*%Z%*%solve(t(Z)%*%Z)%*%t(Z)%*%Y
SIV SIV
## [,1]
## 5.0540920
## x1 0.6846211
## x2 -0.4035435
## x3 0.7844303
## -4.9229999
## -2.5418566
## 1.2379796
## -1.8822475
= function(n){
sols # Create independent variables
= rnorm(n)
z1 = rnorm(n)
z2 # Create error
= rnorm(n)
u1 = rnorm(n)
u2 # Create endogenous variable
= .5*z1 - z2 + u1 + rnorm(n)
x1 = 1.5*z1 - 2*z2 - u2 + rnorm(n)
x2 = rnorm(n)
x3 # Create dependent variable
= 5 + 2*x1 - x2 + x3 + u1*5 - u2*2
y1 = -5 - 2*x1 + x2 - 2*x3 + u2*5 - u1*1
y2 = rbind(cbind(1,x1,x2,x3,0,0,0,0),cbind(0,0,0,0,1,x1,x2,x3))
X = rbind(cbind(1,x3,z1,z2,0,0,0,0),cbind(0,0,0,0,1,x3,z1,z2))
Z = matrix(c(y1,y2),ncol=1)
Y = solve(t(X)%*%Z%*%solve(t(Z)%*%Z)%*%t(Z)%*%X)%*%t(X)%*%Z%*%solve(t(Z)%*%Z)%*%t(Z)%*%Y
SIV
SIV
}= replicate(10000, expr = sols(1000))
z par(mfrow = c(2, 2))
plot(density(z[2, 1, ]), xlim = c(0,4))
abline(v = 2)
plot(density(z[3, 1, ]), xlim = c(-2,0))
abline(v = -1)
plot(density(z[6, 1, ]), xlim = c(-4,0))
abline(v = -2)
plot(density(z[7, 1, ]), xlim = c(0,2))
abline(v = 1)
= Y - X%*%SIV
e_hat = c(e_hat)
e = e * e
e = diag(e)
D = solve(t(X)%*%X)%*%(t(X)%*%D%*%X)%*%solve(t(X)%*%X)
V_h = (n/(n-8)) * V_h ## Para comparar com a estimação equação por equação usa-se k = 4
V_h = sqrt(diag(V_h))
ep_h ep_h
## x1 x2 x3
## 0.21240194 0.20748985 0.09645245 0.20761424 0.17464545 0.12252813 0.08269950
##
## 0.17908589
Equção por equação
= cbind(1,x1, x2, x3)
X = cbind(y1,y2)
Y = cbind(1,x3, z1, z2) Z
= solve(t(X)%*%Z%*%solve(t(Z)%*%Z)%*%t(Z)%*%X)%*%t(X)%*%Z%*%solve(t(Z)%*%Z)%*%t(Z)%*%y1
IV1 IV1
## [,1]
## 5.0540920
## x1 0.6846211
## x2 -0.4035435
## x3 0.7844303
= solve(t(X)%*%Z%*%solve(t(Z)%*%Z)%*%t(Z)%*%X)%*%t(X)%*%Z%*%solve(t(Z)%*%Z)%*%t(Z)%*%y2
IV2 IV2
## [,1]
## -4.923000
## x1 -2.541857
## x2 1.237980
## x3 -1.882247
15.2.2 FGLS - 3SLS
= cbind(1,x1,x2,x3)
X1 = cbind(1,x1,x2,x3)
X2
= rbind(cbind(1,x1,x2,x3,0,0,0,0),cbind(0,0,0,0,1,x1,x2,x3))
X
= rbind(cbind(1,x3,z1,z2,0,0,0,0),cbind(0,0,0,0,1,x3,z1,z2))
Z = matrix(c(y1,y2),ncol=1) Y
= y1 - X1%*%SIV[c(1,2,3,4),1]
e_hat1 = y2 - X2%*%SIV[c(5,6,7,8),1]
e_hat2 = cbind(e_hat1,e_hat2) e_hat
= t(e_hat)%*%e_hat
sigma = diag(n)
I = kronecker(I,solve(sigma)) S1
= solve(t(X)%*%Z%*%solve(t(Z)%*%S1%*%Z)%*%t(Z)%*%X)%*%t(X)%*%Z%*%solve(t(Z)%*%S1%*%Z)%*%t(Z)%*%Y
Bfgls1 Bfgls1
## [,1]
## 5.0540920
## x1 0.6846211
## x2 -0.4035435
## x3 0.7844303
## -4.9229999
## -2.5418566
## 1.2379796
## -1.8822475
= kronecker(I,sigma)
S2
= solve(t(X)%*%Z%*%solve(t(Z)%*%S2%*%Z)%*%t(Z)%*%X)
V_h = (1/(n)) * V_h
V_h = sqrt(diag(V_h))
ep_h ep_h
## x1 x2 x3
## 0.1669931 1.1803355 0.5236078 0.1840325 0.1669931 1.1803355 0.5236078 0.1840325