* Lab 7 - Gabarito use "C:\Users\6977515\Dropbox\FLS 6183\Lista Aula 6_Natalia e Murilo\Lista\nes1996_modif.dta", clear * Exercício 1 * A) Recodificando as variáveis X e Z em dicotômicas * Analisando o banco de dados percebemos que há um número significativo de * missings tanto para wmnmvt quanto para faminc. Nesse sentido, quando iremos criar * dummies temos que estar atentos para deixar as observações missings como missings. *Women's Movement sum wmnmvt, detail kdensity wmnmvt tab wmnmvt gen wmnmvtdummy = 0 replace wmnmvtdummy = 1 if wmnmvt>=62.74 replace wmnmvtdummy = . if missing(wmnmvt) tab wmnmvtdummy *Income sum faminc, detail kdensity faminc tab faminc gen incomedummy = 0 replace incomedummy = 1 if faminc>=20 replace incomedummy = . if missing(faminc) tab incomedummy *B - Estime o modelo * OBS.Dado que o modelo utiliza uma variável de interação, precisamos criá-la. *Interação gen wmnincome_dummy = wmnmvtdummy*incomedummy *O modelo de regressão com as variáveis dicotômicas regress hillary wmnmvtdummy incomedummy wmnincome_dummy *C - Calcular os Valores Preditos *Pouco Apoia Women's Moviment e tem Baixa Renda lincom 1*_cons *Apoia Women's Moviment e tem Baixa Renda lincom 1*_cons + 1*wmnmvtdummy *Pouco Apoia Women's Moviment e tem Alta Renda lincom 1*_cons + 1* incomedummy *Apoia Women's Moviment e tem Alta Renda lincom 1*_cons + 1* wmnmvtdummy +1*incomedummy + 1*wmnincome_dummy // Alternativamente, o exercício inteiro pode ser resolvido usando o comando margins *OBS. Para calcular o efeito marginal utilizando o comando margins, temos que ficar atentos a * indicar a interação com o símbolo # e antes das variáveis explicativas precisamos indicar se * cada uma delas é contínua c. ou dicotômica i. regress hillary i.wmnmvtdummy##i.incomedummy *Pouco Apoia Women's Moviment e tem Baixa Renda margins, at( wmnmvtdummy = (0) incomedummy = (0) ) *Apoia Women's Moviment e tem Baixa Renda margins, at( wmnmvtdummy = (1) incomedummy = (0) ) *Pouco Apoia Women's Moviment e tem Alta Renda margins, at( wmnmvtdummy = (0) incomedummy = (1) ) *Apoia Women's Moviment e tem Alta Renda margins, at( wmnmvtdummy = (1) incomedummy = (1) ) ******************************************************************************************** * Exercício 2 *A - Estime o modelo * OBS.Dado que o modelo utiliza uma variável de interação, precisamos criá-la. *Interação gen wmnfaminc = wmnmvt*faminc *O modelo de regressão com as variáveis contínuas regress hillary wmnmvt faminc wmnfaminc *B - Calcular o efeito marginal de Women's Movement e Income sobre Hillary usando Margins *OBS. Para calcular o efeito marginal utilizando o comando margins, temos que ficar atentos a * indicar a interação com o símbolo # e antes das variáveis explicativas precisamos indicar se * cada uma delas é contínua c. ou dicotômica i. regress hillary c.wmnmvt##c.faminc margins, dydx(wmnmvt) at(faminc=(1(1)24)) // O comando dydx calcula a derivada parcial do modelo em relação a wmnmvt regress hillary c.wmnmvt##c.faminc margins, dydx(faminc) at(wmnmvt=(0(10)100)) // O comando dydx calcula a derivada parcial do modelo em relação a faminc ******************************************************************************************** * Exercício 3 *Caso 1 regress hillary faminc wmnmvt wmnfaminc * Here is where you set the MV equal to the Z variable: generate MV=wmnmvt matrix b=e(b) matrix V=e(V) scalar b1=b[1,1] scalar b2=b[1,2] scalar b3=b[1,3] scalar varb1=V[1,1] scalar varb2=V[2,2] scalar varb3=V[3,3] scalar covb1b3=V[1,3] scalar covb2b3=V[2,3] scalar list b1 b2 b3 varb1 varb2 varb3 covb1b3 covb2b3 * This calculates the marginal effect of X on Y: gen conb=b1+b3*MV * This calculates the standard error of the estimate of the effect that we just calculated: gen conse=sqrt(varb1+varb3*(MV^2)+2*covb1b3*MV) * This generates the upper and lower bounds although these should be modified according to the * number of degrees of freedom if you do not have as many as we have in this model: gen a=1.96*conse gen upper=conb+a gen lower=conb-a * Now for the graph: sort MV graph twoway rarea upper lower MV /* */ || line conb MV /* */ || , /* */ legend(col(1) order(1 2) label(1 "95% Confidence Interval") /* */ label(2 "Marginal Effect of X")) /* */ yline(0, lcolor(black)) /* */ title("Marginal Effect of X on Y As Z Changes") /* */ subtitle(" " "Dependent Variable: Y" " ", size(3)) /* */ xtitle(Z) /* */ ytitle("Marginal Effect of X", size(3)) * Here is another version of the same graph, but in black and white. This would probably * be a more appropriate version for journal submission sort MV graph twoway line conb MV, clwidth(medium) clcolor(blue) clcolor(black) /* */ || line upper MV, clpattern(dash) clwidth(thin) clcolor(black) /* */ || line lower MV, clpattern(dash) clwidth(thin) clcolor(black) /* */ || , /* */ legend(col(1) order(1 2) label(1 "Marginal Effect of X") /* */ label(2 "95% Confidence Interval") /* */ label(3 " ")) /* */ yline(0, lcolor(black)) /* */ title("Marginal Effect of X on Y As Z Changes", size(4)) /* */ subtitle(" " "Dependent Variable: Y" " ", size(3)) /* */ xtitle( Z, size(3) ) /* */ xsca(titlegap(2)) /* */ ysca(titlegap(2)) /* */ ytitle("Marginal Effect of X", size(3)) /* */ scheme(s2mono) graphregion(fcolor(white)) * This last line drops all of the variables that were constructed for this graph so that these code * can be copied and pasted and used again without problems. drop MV conb conse a upper lower // Usando margins e marginsplot regress hillary c.faminc##c.wmnmvt * (Caso 1) margins, dydx( faminc ) continuous at( wmnmvt = (0(10)100) ) marginsplot, xdimension(at(wmnmvt)) recast(line) recastci(rline) /// ciopts(lpattern(dash)) ytitle(Efeito Marginal de Income) /// xtitle(Valores de Women’s Movement) /// title(Efeito Marginal (TOTAL) de Income sobre Hillary) /// subtitle((Conforme Women’s Movement Varia)) ***************************************************************************************************** *Caso 2 regress hillary wmnmvt faminc wmnfaminc * Here is where you set the MV equal to the Z variable: generate MV=faminc matrix b=e(b) matrix V=e(V) scalar b1=b[1,1] scalar b2=b[1,2] scalar b3=b[1,3] scalar varb1=V[1,1] scalar varb2=V[2,2] scalar varb3=V[3,3] scalar covb1b3=V[1,3] scalar covb2b3=V[2,3] scalar list b1 b2 b3 varb1 varb2 varb3 covb1b3 covb2b3 * This calculates the marginal effect of X on Y: gen conb=b1+b3*MV * This calculates the standard error of the estimate of the effect that we just calculated: gen conse=sqrt(varb1+varb3*(MV^2)+2*covb1b3*MV) * This generates the upper and lower bounds although these should be modified according to the * number of degrees of freedom if you do not have as many as we have in this model: gen a=1.96*conse gen upper=conb+a gen lower=conb-a * Now for the graph: sort MV graph twoway rarea upper lower MV /* */ || line conb MV /* */ || , /* */ legend(col(1) order(1 2) label(1 "95% Confidence Interval") /* */ label(2 "Marginal Effect of X")) /* */ yline(0, lcolor(black)) /* */ title("Marginal Effect of X on Y As Z Changes") /* */ subtitle(" " "Dependent Variable: Y" " ", size(3)) /* */ xtitle(Z) /* */ ytitle("Marginal Effect of X", size(3)) * Here is another version of the same graph, but in black and white. This would probably * be a more appropriate version for journal submission sort MV graph twoway line conb MV, clwidth(medium) clcolor(blue) clcolor(black) /* */ || line upper MV, clpattern(dash) clwidth(thin) clcolor(black) /* */ || line lower MV, clpattern(dash) clwidth(thin) clcolor(black) /* */ || , /* */ legend(col(1) order(1 2) label(1 "Marginal Effect of X") /* */ label(2 "95% Confidence Interval") /* */ label(3 " ")) /* */ yline(0, lcolor(black)) /* */ title("Marginal Effect of X on Y As Z Changes", size(4)) /* */ subtitle(" " "Dependent Variable: Y" " ", size(3)) /* */ xtitle( Z, size(3) ) /* */ xsca(titlegap(2)) /* */ ysca(titlegap(2)) /* */ ytitle("Marginal Effect of X", size(3)) /* */ scheme(s2mono) graphregion(fcolor(white)) * This last line drops all of the variables that were constructed for this graph so that these code * can be copied and pasted and used again without problems. drop MV conb conse a upper lower ***************************************************************************************************** * EXTRA - Como fazer marginsplot com o histograma da variável Z. *Examinando se o efeito marginal é substantivamente significativo *1) para Z sendo faminc regress hillary c.wmnmvt##c.faminc qui margins, dydx(wmnmvt) at(faminc=(1(1)24)) *Gráfico composto: Efeitos de faminc (Escala: direita) e Histograma de wmnmvt (Escala: esquerda) marginsplot, yline(0) recast(line) recastci(rline) ciopts(lpattern(dash)) /// addplot( (histogram faminc, percent yaxis(2) fcolor(none) lcolor(gs10) ), below) yscale(alt axis(1)) /// ytitle(Average Maginal Effects of Income on Hillary) xtitle(Women's Movement) /// legend(off) /// title(Marginal Effects of Income on Hillary) subtitle((as Women's Movement Changes)) *Densidade Empírica de faminc kdensity faminc *2) para Z sendo wmnmvt regress hillary c.wmnmvt##c.faminc qui margins, dydx(faminc) at(wmnmvt=(0(10)100)) *Gráfico composto: Efeitos de wmnmvt (Escala: direita) e Histograma de faminc (Escala: esquerda) marginsplot, yline(0) recast(line) recastci(rline) ciopts(lpattern(dash)) /// addplot( (histogram wmnmvt, percent yaxis(2) fcolor(none) lcolor(gs10) ), below) yscale(alt axis(1)) /// ytitle(Average Maginal Effects of Women's Movement on Hillary) xtitle(Income) /// legend(off) /// title(Marginal Effects of Women's Movement on Hillary) subtitle((as Income Changes)) *Densidade Empírica de wmnmvt kdensity wmnmvt