Programming & Codes

coming soon ...

 
 

Miscellaneous things which I came across as some useful stuff  in programming mostly in Matlab, Stata, ...

Matlab

Creating dummies for a discrete variable

%*** creating dummies for a discrete variable
temp = sort(firm_id);
temp1 = [1;diff(temp)];
temp2 = temp(temp1>0);
id_dum = zeros(size(firm_id,1), size(temp2,1)); % can make it sparse
for i = 1:size(temp2,1)
id_dum(:,i) = (firm_id==temp2(i)); % can make it sparse
end;
 

Stata

1.jpg

Difference in Difference table display with output in Latex

% before differencing across groups
gen var1 = y if time_d88 ==0
qui ttest var1, by(treated)
mat T[1,1] = r(mu_1)
mat T[1,2] = r(mu_2)
mat T[1,3] = r(mu_2) - r(mu_1)
mat T[1,4] = r(p)
% after differencing across groups
gen var2 = y if d88 ==1
qui ttest var2, by(treated)
mat T[2,1] = r(mu_1)
mat T[2,2] = r(mu_2)
mat T[2,3] = r(mu_2) - r(mu_1)
mat T[2,4] = r(p)
% control differencing across time
gen var3 = y if treated ==0
qui ttest var3, by(d88)
mat T[3,1] = r(mu_1) - r(mu_2)
mat T[4,1] = r(p)
% treatment differencing across time
gen var4 = y if treated ==1
qui ttest var4, by(d88)
mat T[3,2] = r(mu_1) - r(mu_2)
mat T[4,2] = r(p)
mat T[3,3] = T[2,3] - T[1,3]
mat rownames T = Before After Difference p-value
frmttable using Q2-Ai-DIDtab, tex statmat(T) varlabels replace ///
ctitles("", "Control", "Treated", "Difference", "p-value")


Visually testing the common trend assumption: scatter using only mean values

 
preserve
collapse (mean) frac_treated frac_control, by(year)
twoway (scatter frac_treated year , connect(l) msymbol(X) tline(1985) sort) (scatter frac_control year , connect(l) msymbol(0) sort) , /// legend(label(1 "Treated group") label(2 "Control group")) saving(filename, replace)
restore
12.jpg

13.png

Difference in Difference with year interactions: Scatter plot the estimated coefficients of interaction terms

  • Visualizing the pre and post-treatment effects

 

* Storing the interaction terms or variables 
* hear, treat is dummy, but year is not. We gen year dummies by i.year 
qui reg y i.year##c.treat , robust 
fvexpand i.year#c.treat 
global vars=r(varlist) 
* Saving the estimated coefficients of interactions terms and their SE 
gen b=. 
gen se=. 
glo i=1 
foreach x in $vars { 
 replace b=_b[`x'] in $i 
 replace se=_se[`x'] in $i 
 glo i=$i + 1 
} 
* creating the %95 confidence interval 
gen bu=b+1.96*se 
gen bl=b-1.96*se 
* Generating the x axis variable, here year, for coefficients: 
* This example uses a discontinuous period: 10 years which gives 9 estimation from regression 
numlist "1976/1980 1985/1989" 
glo numvar = r(numlist) 
glo j =1 
gen yr =. 
foreach x in $numvar { 
 replace yr =`x' in $j glo j=$j+ 1 
} 
*Scatter plot the result 
twoway (scatter b yr in 2/10, cmissing(n) msymbol(0) tline(1985) sort)/// 
(scatter bu yr in 2/10, cmissing(n) msymbol(X) yline(0) tline(1985) sort) /// 
(scatter bl yr in 2/10 , cmissing(n) msymbol(X) yline(0) tline(1985) sort), /// 
legend(off) saving(filename,replace)