1/70
Looks like no tags are added yet.
common/general time trend
the trajectory of the variable regardless of treatment
parallel-trends assumption
absent the reform, the treated group would have followed the same trend as the control group; check that the two groups moved in parallel in the years before the reform, parallel pre-trends make the assumption credible; diverging pre-trends make doubt
counterfactual
dashed line on diff-in-diff graph, where the treated group would have been without the reform (its before level, carried forward along the control’s trend)
longitudinal data (or panel or cross-sectional time-series)
data with several units followed over several periods
why is a single country bad as a control group?
A single control is
1. sensitive to whatever happens in that one country (a drought, a crisis, a data revision there would masquerade as a treatment effect)
2. open to the charge of cherry-picking, choosing the one control that gives the answer you want
a convincing control group of countries
several logical, natural control countries, or a regional group that excludes the treated country, and present 3–4 control groups
does a DID solve third variable problem
mostly, but not when third variable drove the reform. defences: choose as-exogenous reforms, always show pre-trends, use a triple difference, and keep the time horizon short
difference-in-difference
(Y_Tafter − Y_Tbefore) − (Y_Cafter − Y_Cbefore)
triple difference
two groups in same country, one affected by reform and one unaffected, if the unaffected group moves there is evidence other things are moving
short vs long time horizons
in the long run many other things can happen, but in short time we miss the long-run effects of the reform
falsification
use a variable knowingly not affected by the reform, should not change, otherwise might indicate something like good government
true effect of t with bias
yi=a+bTi+cAi
from which you find
yt-yc=b+c(At-Ac)
where b is the true effect and c(At-Ac) is bias
why RTC works
If treatment is assigned at random, then who is treated has nothing to do with ability, so on average A_T = A_C and the bias term vanishes. Nothing causes the treatment (it is random), so the outcome cannot be causing it either: the arrow can only run treatment → outcome
Parametric vs nonparametric
t-test is parametric (normal distribution, summarized by parameters (the mean and the variance), permutation test is nonparametric (t needs no normality and no variance formula, and no t-table)
ttest
ttest Y, by(varT) [reverse] [level (#)]
whisker plot
preserve
ttest Y, by(T)
generate diff = r(mu_2) - r(mu_1)
generate hi = diff + invttail(r(df_t), 0.025) * r(se)
generate lo = diff - invttail(r(df_t), 0.025) * r(se)
keep in 1
generate x = 1
twoway (scatter diff x) ///
(rcap hi lo x), ///
yline(0, lpattern(dash)) ylabel(0(1)5) legend(off) ///
xlabel(0.5 " " 1 " " 1.5 " ", noticks) xtitle("") ///
title(" ")
graph export " .ghp", replace
restore
the sample mean is unbiased: E( Y_) = μ. Explain in words what this means
If we redid the sample many times (a million times), the average of all the Y_’s would be exactly the true population mean μ. One sample can miss, but Y_ is not off on average. If we drew many samples, the average of all the Y_ would be the true mean
pooled ttest requires
assuming equal variances in both groups, if not we can use the welch test, but it’s not ideal bc formula for df is complicated
efficiency
Y has a small variance, so each Y_ is close to μ
why we use n-1
To compute the variance we first need ¯Y, estimated from the same data. This uses up one degree of freedom: it is as if we had only n − 1 observations.
reasons why there can be correlation without causation
third variable problem, directionality (reverse causation), coincidence
standard deviation intervals
if normal, 34.1 is 1SD 13.6 is 2SD 2.3 is 3SD
z-score, units
(yᵢ-ȳ)/SD, unitless
shaky
lots of spread
ū
average deviation
covariance
2E(x-μx)(y-μy)
rename a variable
ren varog varnew
label a variable
label variable var1 “"
get rid of rows of data
drop in #/#
revalue variable
replace var=”” if var==””
convert variable from strings to numbers
destring var, replace
find median
centile var, centile (50)
sort variable into above and below median
gen var2=# if var1>= `r(c_1)’&var1!=.
replace var2=# if var1<`r(c_1)’
Tell me the quantities of each value and percents
tab var
Tell me the mean, min/max, SD, # of observations, variance, median, quarters, skew
sum var, detail
save something
save “\” , replace
make a graph
collapse (mean) mean_var1=var1, by var2
graph twoway (bar mean_var1 var2, barw(#)), xlabel(# ““ etc) xtitle(““) ylabel (# ““ etc) ytitle(““) title (““)
graph save
make a table
eststo var1:estpost summarize var2 if var1==#
esttab var1 var2 using “\.rtf”, plain cells(“mean(label(Mean) fmt (0))” “sd(label(SD) fmt(0))” “count(label(N) fmt(0))”) mtitles(“ “ “ “) noobs replace label title()
import first row as variables
import ““, firstrow
how to import sas
import sas using ““
number of observations stata
count var
general stats about data (string/numbers, missing)
codebook var
directory
cd, use, save
look at distribution
hist var
sort variable from smallest to largest
sort var
bias directionality
can be product positive if all possible third variables end up positive or vice versa, or product neutral, it is positive and overstates bias if a third variable causes both things to happen more, negative understates bias and causes one, but reduces the other
Yᵢ = μ + uᵢ: what is it, and why is E(uᵢ) = 0?
Each observation = truth (μ) + disturbance/noise (uᵢ). E(uᵢ) = 0 because with a symmetric distribution centered on μ, noise is not systematically positive or negative.
Variance of a sum: Var(X+Y)
Var(X) + Var(Y) + 2Cov(X,Y)
Var(a + bX)
b² Var X
Correlation: formula, unit, range
corr(X,Y) = Cov(X,Y) / √(Var X · Var Y). Unitless, between −1 and 1. Independence gives Cov = 0 so corr = 0.
Y = a + bX gives corr
1
Y = a - bX gives corr
-1
σ²_Ȳ = ? + assumptions
σ²/n, 1) Independence between observations (Cov = 0). Can fail with peer effects (students working together). 2) Equal variance σ² for every observation. (Normality is NOT needed.)
Consistency (plim)
plim Ȳ = μ (or lim Ȳ = μ as n → ∞). Since σ²/n → 0, Ȳ converges to μ. Large-sample (asymptotic) property. "Probability limit": P(|Ȳ − μ| > ε) → 0 for any small ε.
Standard error (SE): formula and meaning
SE = √(Var Y / n) = SD/√n. It is the SD of the SAMPLE MEAN Ȳ (not of Y). Shrinks with 1/√n.
Why use a t-distribution instead of the normal?
With a small sample, the SE is itself estimated from few observations and is noisy. The ratio then follows the t-distribution (fatter tails than normal).
Confidence interval recipe
CI = estimate ± critical value × SE. Critical value comes from the t-table (significance level + df).
p-value: definition
The probability of getting a t as large as ours if the true difference were 0. Equivalently, the exact significance level at which the result flips from not significant to significant. Smaller p = stronger evidence.
Significance stars
p < 1%: *** p < 5%: ** p < 10%: * p ≥ 10%: no stars
statistically significant
Significance needs the standard error: it compares the gap to how precisely each mean is estimated
why do we bother dividing the covariance by √Var X Var Y to get a correlation
We divide by √Var X Var Y to get a unitless number that always lies between −1 and +1, so correlations are comparable across variables with different units and scales
p-value
the probability of getting a t as large as ours if the true difference were 0, also the exact significance level
if 0 falls within the confidence interval
It does not prove that the treatment has no effect. We simply cannot reject a difference of 0. The true effect could be as large as upper bound: maybe the sample is just too small.
stata find t-values at 10%, 5%, 2.5% with 8 df
display invttail(8, 0.025), invttail(8, 0.05), invttail(8, 0.005)
scatter plot stata
twoway scatter VarA VarB
correlation stata
correlate VarA VarB
results table with star
* estpost does group 1 minus group 2: recode so that Bachelor+ = 1, No Bachelor = 2
gen U2 = University
replace U2 = 2 if University==0
eststo ttest: estpost ttest VerySatisfied, by(U2)
esttab ttest using "CSS_ttest.rtf", plain ///
cells("mu_1(fmt(3)) mu_2(fmt(3)) b(star fmt(3)) p(fmt(3))") ///
collabels("Bachelor+" "No Bachelor" "Difference" "p-value") ///
star(* 0.10 0.05 * 0.01) noobs replace label title(Very satisfied with life) ///
addnotes("* p<0.10, p<0.05, * p<0.01."
We recode because estpost ttest always computes group 1 minus group 2, and group 1 is the lower value of the variable (here 0 = No Bachelor). With U2, Bachelor+ is group 1, so the difference reads Bachelor+ minus No Bachelor and comes out positive.
permutation test stata
sum Y if T==0
return list
local Control_mean=r(mean)
sum Y if T==1
local Treatment_mean=r(mean)
gen Treatment_Effect= `Treatment_mean’-’Control_mean'
gen real=1
keep real Treatment_Effect
keep in 1
save "Guinness Permutation Test.dta", replace
forvalues i=1(1) 99 {
clear
use "Guinness Full Sample_Cleaned.dta"
gen id=_n
gen number=runiform()
sort number
gen new_T=T[id]
sum Y if new_T==0
local Control_mean=r(mean)
sum Y if new_T==1
local Treatment_mean=r(mean)
gen Treatment_EffectTreatment_mean'-Control_mean'
gen real=0
keep real Treatment_Effect
keep in 1
append using "Guinness Permutation Test.dta"
save "Guinness Permutation Test.dta", replace
}
sort Treatment_Effect
*It is tied at the top with 20 other observations, so not exceptional
tab Treatment_Effect
pre/post-trends + scatter plot
sum Y if T==1&Post==1
local Treatment_Post=r(mean)
sum Y if T==1&Post==0
local Treatment_Pre=r(mean)
sum Y if T==0&Post==1
local Control_Post=r(mean)
sum Y if T==0&Post==0
local Control_Pre=r(mean)
gen Treatment_EffectTreatment_Post'-Treatment_Pre'-Control_Post'-Control_Pre')
keep Treatment_Effect
keep in 1
save "Treatment_Effect.dta", replace
****GRAPH:
clear
use "DID_clean.dta"
collapse (mean) Y, by(T Post)
graph twoway ///
(scatter Y Post if T==1, connect(direct) lcolor(red) lwidth(thick) msymbol(O) msize(large) mcolor(red)) ///
(scatter Y Post if T==0, connect(direct) lcolor(blue) lwidth(thick) msymbol(O) msize(large) mcolor(blue)) ///
, legend(order(1 2) label(1 "Treatment") label(2 "Control") position(6)) xlabel(0 "Before" 1 "After") xline(0.5) xscale(range(-0.2 1.2)) ylabel(0 "0" 3 "3" 6 "6") xtitle("") ytitle("") title("Impact of T on Y")
what to do if duplicates
duplicates drop var, force
merge
merge 1:m Country using "DID_clean.dta"
*Why 1:m?
*The first number (1) is in the master dataset: we have 1 row per country
*The second number (m) is in the using dataset: we have multiple rows per country
*there is a new variable: _merge: the result of the merge:
*1 : in master, not using
*2: not in master but in using
*3: success
drop _merge