Showing posts with label esttab matrix. Show all posts
Showing posts with label esttab matrix. Show all posts

Mar 24, 2023

Clausen (1998): Applied Correspondence Analysis

clear all

// Table 1.1
  // Create Table -- the -camat- command allows working with tabular data right 
  //                 away
input leisure class weight
 1 1 301
 1 2 497
 1 3 208
 1 4  50
 1 5 254
 1 6 187
 2 1 261
 2 2 550
 2 3 250
 2 4  27
 2 5 339
 2 6 157
 3 1 361 
 3 2 534
 3 3 204
 3 4  59
 3 5 324
 3 6 216
 4 1 463 
 4 2 766
 4 3 334
 4 4  72
 4 5 350
 4 6 601
 5 1  89
 5 2 350
 5 3 195
 5 4  12
 5 5 143
 5 6 167
 6 1  23 
 6 2 182
 6 3 124
 6 4  10
 6 5  60
 6 6 110
 7 1 117
 7 2 298
 7 3 145
 7 4  11
 7 5 184
 7 6  56
 8 1 104
 8 2 379
 8 3 219
 8 4  21
 8 5 152
 8 6 213
 9 1 130
 9 2 352
 9 3 153
 9 4  17
 9 5 272
 9 6 264
10 1 168
10 2 370
10 3 187
10 4  51
10 5 162
10 6 424
end

label define leisure 1 "Sport events" ///
                     2 "Cinema" ///
                     3 "Dance/disco" ///
                     4 "Cafe/restaurant" ///
                     5 "Theater" ///
                     6 "Classical concert" ///
					 7 "Pop concert" ///
					 8 "Art exhibition" ///
					 9 "Library" ///
					10 "Church service"
					 
label define class 1 "Manual" ///
                   2 "Low nonmanual" ///
                   3 "High nonmanual" ///
                   4 "Farmer" ///
                   5 "Student" ///
                   6 "Retired" 

label values leisure leisure
label values class class

label var leisure "Leisure activities"
label var class "Occupational class"

expand weight

estpost tabulate leisure class
esttab, unstack nonumber varwidth(20) compress nonote noobs

// Figure 1.1
ca leisure class, plot

// Make a nicer version of Figure 1.1 
capture frame drop biplotc
frame create biplotc
frame biplotc: matrix dim = e(TC)
frame biplotc: svmat2 dim, rname(varname) name(col)
frame biplotc: generate variable = "Social class"

capture frame drop biplotr
frame create biplotr
frame biplotr: matrix dim = e(TR)
frame biplotr: svmat2 dim, rname(varname) name(col)
frame biplotr: generate variable = "Leisure activities"

frame biplotc: save deleteme, replace
frame biplotr: append using deleteme
frame biplotc: erase deleteme.dta
frame drop biplotc
capture frame drop biplot
frame rename biplotr biplot

frame biplot: drop dim3-dim5
frame biplot: replace varname = subinstr(varname, "_", " ", .)
frame biplot: twoway (scatter dim2 dim1 if variable == "Leisure activities", mlabel(varname)) ///
                     (scatter dim2 dim1 if variable == "Social class", mlabel(varname)) ///
                     , legend(order(1 "Leisure activities" 2 "Social classes")) ///
                       xtitle(Dimension 1: Young versus old) ///
                       ytitle(Dimension 2: Art versus light entertainment) ///
                       name(figure11, replace) xscale(range(-1 1)) yscale(range(-1 1))

// Table 2.1
clear all

input region crime weight
1 1  395
1 2 2456
1 3 1758
2 1  147
2 2  153
2 3  916
3 1  694
3 2  327
3 3 1347
end 

label define region 1 "Oslo area" 2 "Mid-Norway" 3 "North-Norway"
label define crime 1 "Burglary" 2 "Fraud" 3 "Vandalism"

label val region region 
label val crime crime

label var region "Region"
label var region "Type of crime"
expand weight

// Table 2.2
table region crime, statistic(proportion, across(crime)) nformat(%6.3f)
table region crime, statistic(proportion, across(region)) nformat(%6.3f)

// Figure 2.2
ca region crime, plot

// Make a nicer version of Figure 2.2 
capture frame drop biplotc
frame create biplotc
frame biplotc: matrix dim = e(TC)
frame biplotc: svmat2 dim, rname(varname) name(col)
frame biplotc: generate variable = "Region"

capture frame drop biplotr
frame create biplotr
frame biplotr: matrix dim = e(TR)
frame biplotr: svmat2 dim, rname(varname) name(col)
frame biplotr: generate variable = "Type of crime"

frame biplotc: save deleteme, replace
frame biplotr: append using deleteme
frame biplotc: erase deleteme.dta
frame drop biplotc
capture frame drop biplot
frame rename biplotr biplot

frame biplot: list
frame biplot: replace varname = subinstr(varname, "_", " ", .)
frame biplot: twoway (scatter dim2 dim1 if variable == "Type of crime", mlabel(varname)) ///
                     (scatter dim2 dim1 if variable == "Region", mlabel(varname)) ///
                     , legend(order(1 "Type of crime" 2 "Region")) ///
                       xtitle(Dimension 1) ytitle(Dimension 2) ///
                       name(figure22, replace) xscale(range(-1 1)) yscale(range(-1 1))

// Table 2.4
// Eigenvalues
di "           " _skip(5) "Dim. 1    "       _skip(5) "Dim. 2   "_skip(5)  "Sum"
di "Eigenvalues" _skip(5) (e(Sv)[1,1])^2 _skip(5) (e(Sv)[1,2])^2 _skip(5) e(inertia)

// Figure 2.4
matrix dense = (749, 66 \ 235, 135 \ 283, 185)
matrix colnames dense = "Dense" "Sparse"
matrix rownames dense = oslo midnorway northnorway
matrix norway = (4558, 5129, 10842)
matrix rowname norway = Norway
ca region crime, plot colsupp(dense) rowsupp(norway)


// Make a nicer version of Figure 2.4
capture frame drop biplotc
frame create biplotc
frame biplotc: matrix dim = e(TC)
frame biplotc: svmat2 dim, rname(varname) name(col)
frame biplotc: generate variable = "Region"

capture frame drop biplotr
frame create biplotr
frame biplotr: matrix dim = e(TR)
frame biplotr: svmat2 dim, rname(varname) name(col)
frame biplotr: generate variable = "Type of crime"

capture frame drop biplotsuppc
frame create biplotsuppc
frame biplotsuppc: matrix dim = e(TC_supp)
frame biplotsuppc: svmat2 dim, rname(varname) name(col)
frame biplotsuppc: generate variable = "Population density"
frame biplotsuppc: list

capture frame drop biplotsuppr
frame create biplotsuppr
frame biplotsuppr: matrix dim = e(TR_supp)
frame biplotsuppr: svmat2 dim, rname(varname) name(col)
frame biplotsuppr: generate variable = "Nat'l average"
frame biplotsuppr: list

frame biplotc: save deleteme, replace
frame biplotr: append using deleteme
frame biplotc: erase deleteme.dta
frame biplotsuppc: save deleteme, replace
frame biplotr: append using deleteme
frame biplotsuppc: erase deleteme.dta
frame biplotsuppr: save deleteme, replace
frame biplotr: append using deleteme
frame biplotsuppr: erase deleteme.dta

frame drop biplotc
capture frame drop biplot
frame rename biplotr biplot

frame biplot: list
frame biplot: replace varname = subinstr(varname, "_", " ", .)
frame biplot: twoway (scatter dim2 dim1 if variable == "Type of crime", mlabel(varname)) ///
                     (scatter dim2 dim1 if variable == "Region", mlabel(varname)) ///
                     (scatter dim2 dim1 if variable == "Population density", mlabel(varname)) ///
                     (scatter dim2 dim1 if variable == "Nat'l average", mlabel(varname)) ///
                     (line dim2 dim1 if variable == "Population density")  ///
                     , legend(order(1 "Type of crime" 2 "Region" 3 "Population density")) ///
                       xtitle(Dimension 1) ytitle(Dimension 2) ///
                       name(figure24, replace) xscale(range(-1 1)) yscale(range(-1 1))

// Table 3.1

clear all
input disease age freq
 1 1  12
 1 2  22
 1 3  35
 1 4  68
 1 5 102
 1 6 147
 2 1   7
 2 2  11
 2 3  35
 2 4  45
 2 5  49
 2 6  33
 3 1  44
 3 2  47
 3 3  45
 3 4  42
 3 5  68
 3 6 155
 4 1  12
 4 2   6
 4 3   5
 4 4  38
 4 5 222
 4 6 469
 5 1  63
 5 2  70
 5 3  69
 5 4  74
 5 5  80
 5 6  84
 6 1   0
 6 2   1
 6 3   8
 6 4  14
 6 5  36
 6 6  37
 7 1   9
 7 2   5
 7 3   5
 7 4  15
 7 5  32
 7 6  64
 8 1   8
 8 2   9
 8 3  30
 8 4  28
 8 5  39
 8 6  56
 9 1 103
 9 2 110
 9 3 138
 9 4 124
 9 5  88
 9 6  54
10 1  22
10 2  43
10 3 105
10 4 165
10 5 314
10 6 334
11 1  30
11 2  42
11 3  42
11 4  72
11 5 126
11 6 235
12 1   7
12 2  13
12 3  38
12 4  32
12 5  48
12 6  76
end

label define age 1 "0-6" 2 "7-15" 3 "16-24" 4 "25-44" 5 "45-66" 6 "67+"
label val age age

label define disease  1 "Nervous disorders" ///
                      2 "Nervous system" ///
                      3 "Eye and ear" ///
                      4 "Cardiovascular" /// 
                      5 "Respiratory organ" ///
                      6 "Stomach ulcer" ///
                      7 "Other digestive disease"  ///
                      8 "Urinary/genital system" ///
                      9 "Skin and subcutis" ///
                     10 "Muscuskeletal dis." ///
                     11 "Other diseases" ///
                     12 "Injuries"
label val disease disease

expand freq

// Table 3.2/Figure 3.1
ca disease age, plot

// Make a nicer version of Figure 3.1 
capture frame drop biplotc
frame create biplotc
frame biplotc: matrix dim = e(TC)
frame biplotc: svmat2 dim, rname(varname) name(col)
frame biplotc: generate variable = "Age group"

capture frame drop biplotr
frame create biplotr
frame biplotr: matrix dim = e(TR)
frame biplotr: svmat2 dim, rname(varname) name(col)
frame biplotr: generate variable = "Disease"

frame biplotc: save deleteme, replace
frame biplotr: append using deleteme
frame biplotc: erase deleteme.dta
frame drop biplotc
capture frame drop biplot
frame rename biplotr biplot

frame biplot: list
frame biplot: replace varname = subinstr(varname, "_", " ", .)
frame biplot: twoway (scatter dim2 dim1 if variable == "Disease", mlabel(varname)) ///
                     (scatter dim2 dim1 if variable == "Age group", mlabel(varname)) ///
                     (line dim2 dim1 if variable == "Age group") ///
                     , legend(order(1 "Disease" 2 "Age group")) ///
                       xtitle(Dimension 1) ytitle(Dimension 2) ///
                       name(figure31, replace) xscale(range(-1 1)) yscale(range(-1 1))

					   
// Table 3.4

clear all
input disease age female freq
 1 1 0   8
 1 2 0  25
 1 3 0  23
 1 4 0  46
 1 5 0  66
 1 6 0 106
 2 1 0   4
 2 2 0  13
 2 3 0  30
 2 4 0  29
 2 5 0  29
 2 6 0  31
 3 1 0  38
 3 2 0  52
 3 3 0  41
 3 4 0  48
 3 5 0  72
 3 6 0 156
 4 1 0  10
 4 2 0   7
 4 3 0   5
 4 4 0  39
 4 5 0 226
 4 6 0 399
 5 1 0  70
 5 2 0  77
 5 3 0  72
 5 4 0  77
 5 5 0  89
 5 6 0 100
 6 1 0   0
 6 2 0   0
 6 3 0   8
 6 4 0  22
 6 5 0  43
 6 6 0  43
 7 1 0  12
 7 2 0   3
 7 3 0   3
 7 4 0  16
 7 5 0  21
 7 6 0  60
 8 1 0  10
 8 2 0  11
 8 3 0  11
 8 4 0  12
 8 5 0  22
 8 6 0  72
 9 1 0 105
 9 2 0 106
 9 3 0 119
 9 4 0 106
 9 5 0  72
 9 6 0  55
10 1 0  12
10 2 0  36
10 3 0  95
10 4 0 153
10 5 0 267
10 6 0 266
11 1 0  38
11 2 0  47
11 3 0  41
11 4 0  52
11 5 0 101
11 6 0 192
12 1 0   4
12 2 0  13
12 3 0  55
12 4 0  46
12 5 0  63
12 6 0  77
 1 1 0  17
 1 2 1  19
 1 3 1  45
 1 4 1  89
 1 5 1 135
 1 6 1 179
 2 1 1  10
 2 2 1   9
 2 3 1  41
 2 4 1  60
 2 5 1  68
 2 6 1  34
 3 1 1  50
 3 2 1  44
 3 3 1  48
 3 4 1  36
 3 5 1  64
 3 6 1 155
 4 1 1  15
 4 2 1   5
 4 3 1   5
 4 4 1  38
 4 5 1 218
 4 6 1 523
 5 1 1  56
 5 2 1  64
 5 3 1  67
 5 4 1  70
 5 5 1  72
 5 6 1  72
 6 1 1   0
 6 2 1   3
 6 3 1   8
 6 4 1   5
 6 5 1  29
 6 6 1  33
 7 1 1   6
 7 2 1   6
 7 3 1   6
 7 4 1  15
 7 5 1  42
 7 6 1  68
 8 1 1   6
 8 2 1   8
 8 3 1  48
 8 4 1  45
 8 5 1  55
 8 6 1  43
 9 1 1 100
 9 2 1 113
 9 3 1 156
 9 4 1 141
 9 5 1 104
 9 6 1  54
10 1 1  33
10 2 1  49
10 3 1 115
10 4 1 177
10 5 1 358
10 6 1 387
11 1 1  21
11 2 1  37
11 3 1  44
11 4 1  92
11 5 1 150
11 6 1 268
12 1 1  10
12 2 1  14
12 3 1  23
12 4 1  19
12 5 1  34
12 6 1  74
end

label define age 1 "0-6" 2 "7-15" 3 "16-24" 4 "25-44" 5 "45-66" 6 "67+"
label val age age

label define disease  1 "Nervous disorders" ///
                      2 "Nervous system" ///
                      3 "Eye and ear" ///
                      4 "Cardiovascular" /// 
                      5 "Respiratory organ" ///
                      6 "Stomach ulcer" ///
                      7 "Other digestive disease"  ///
                      8 "Urinary/genital system" ///
                      9 "Skin and subcutis" ///
                     10 "Muscuskeletal dis." ///
                     11 "Other diseases" ///
                     12 "Injuries"
label val disease disease

label define female 1 "Female" 0 "Male"
label val female female
expand freq

// Table 3.5, Table 3.6, Figure 3.2
ca (demo: age female) disease, plot dim(3)
// Figure 3.3
cabiplot, dim(3 2)

// Table 4.1
clear all

input classification subgroup freq
 1 1 139
 1 2  40
 1 3  40
 1 4  41
 2 1 132
 2 2  42
 2 3  37
 2 4  53
 3 1 131
 3 2  21
 3 3  16
 3 4  15
 4 1 124
 4 2  51
 4 3  64
 4 4 124
 5 1 101
 5 2  45
 5 3  49
 5 4  62
 6 1  15
 6 2  79
 6 3   5
 6 4   4
 7 1  20
 7 2  98
 7 3  34
 7 4  29
 8 1  24
 8 2  47
 8 3   1
 8 4   2
 9 1   5
 9 2  42
 9 3  10
 9 4   1
10 1   7
10 2  65
10 3  12
10 4   6
11 1 137 
11 2 114
11 3 106
11 4 159
12 1  61
12 2  67
12 3 115
12 4  62
13 1  95
13 2  44
13 3  83
13 4  86
14 1 143
14 2  83
14 3 121
14 4 149
15 1  57
15 2  97
15 3  92
15 4  98
16 1  76
16 2  32
16 3  56
16 4 195
17 1  75
17 2  49
17 3  43
17 4 194
18 1  63
18 2  45
18 3  38
18 4 171
19 1  48
19 2  46
19 3  18
19 4 143
20 1  49
20 2 113
20 3  46
20 4 105
21 1 111
21 2  11
21 3  76
21 4  95
22 1  21
22 2  38
22 3  23
22 4  34
23 1  24
23 2  36
23 3  33
23 4  60
24 1 115
24 2  50
24 3  66
24 4 106
25 1  64
25 2  90
25 3  66
25 4  69
26 1  24
26 2   8
26 3  15
26 4  72
27 1  71
27 2  26
27 3  40
27 4  58
28 1  57
28 2  32
28 3  29
28 4  82
29 1  72
29 2  53
29 3  55
29 4  65
30 1  30
30 2  37
30 3  37
30 4  52
31 1  25
31 2  44
31 3  18
31 4  28
32 1  86
32 2  15
32 3  66
32 4 149
end

expand freq

label define subgroup 1 "Sick" 2 "Deviant" 3 "Dependent" 4 "Indebted"
label val subgroup subgroup

label define classification ///
 1 "Poor mental health" ///
 2 "Poor general health" ///
 3 "Using sedatives" ///
 4 "National insurance" ///
 5 "Low education" ///
 6 "Alcohol consumption" ///
 7 "Convicted" ///
 8 "Alcohol problems" ///
 9 "Ever used narcotics" ///
10 "Debts due to penalty" ///
11 "Daily cigarette smoking" ///
12 "Long-term client" ///
13 "Trouble daily expenses" ///
14 "Trouble NOK2000" ///
15 "Unemployed" ///
16 "House debt" ///
17 "Owns a dwelling" ///
18 "Owns a car" ///
19 "High income" ///
20 "Men" ///
21 "Women" ///
22 "Age 18-24" ///
23 "Age 25-30" ///
24 "Age 30-50" ///
25 "Unmarried" ///
26 "Married" ///
27 "Divorced" ///
28 "Rural" ///
29 "Urban" ///
30 "City" ///
31 "Child not in household" ///
32 "Child in household"
label val classification classification

ca classification subgroup, plot

// Table 5.1

clear all

input female age alcohol freq
0 0 0  22
0 0 1  78
0 0 2 109
0 0 3 108
0 0 4 132
0 0 5  85
0 1 0  19
0 1 1  84
0 1 2 120
0 1 3  91
0 1 4 203
0 1 5  90
0 2 0  83
0 2 1 130
0 2 2 135
0 2 3 108
0 2 4 160
0 2 5  78
0 3 0  69
0 3 1 126
0 3 2  99
0 3 3  50
0 3 4 127
0 3 5 106
0 4 0  32
0 4 1  62
0 4 2  40
0 4 3  41
0 4 4  95
0 4 5 126
0 5 0  22
0 5 1  19
0 5 2  22
0 5 3  29
0 5 4  62
0 5 5 177
1 0 0  54
1 0 1 134
1 0 2 114
1 0 3 101
1 0 4 104
1 0 5  80
1 1 0  65
1 1 1 127
1 1 2 136
1 1 3  87
1 1 4  81
1 1 5  35
1 2 0 105
1 2 1 150
1 2 2 124
1 2 3  67
1 2 4 113
1 2 5  38
1 3 0 139
1 3 1 149
1 3 2 103
1 3 3  71
1 3 4  88
1 3 5  49
1 4 0  82
1 4 1  63
1 4 2  54
1 4 3  44
1 4 4  84
1 4 5  53
1 5 0  39
1 5 1  40
1 5 2  42
1 5 3  36
1 5 4  72 
1 5 5 100
end
 
label define female 0 "Male" 1 "Female"
label define alcohol 5 "Never"           4 "More seldom" 3 "Once a month" ///
                     2 "2-3 times/month" 1 "Once a week" 0 "Many times a week"
label define age 0 "16-25" 1 "26-35" 2 "36-45" 3 "46-55" 4 "56-66" 5 "67-100"
label val female female
label val alcohol alcohol
label val age age


// Loglinear model
eststo clear
eststo Independent: glm freq i.female  i.age               i.alcohol, fam(pois) link(log)                 // S A C

eststo Base:        glm freq i.female##i.age               i.alcohol, fam(pois) link(log)                 // SA C
eststo M1:          glm freq i.female##i.alcohol    i.female##i.age, fam(pois) link(log)                  // SC SA
eststo M2:          glm freq i.female##i.age        i.age##i.alcohol, fam(pois) link(log)                 // SA AC
eststo M3:          glm freq i.female##i.alcohol    i.age##i.alcohol, fam(pois) link(log)                 // SC AC
eststo M4:          glm freq i.female##i.alcohol    i.age##i.alcohol i.female##i.age, fam(pois) link(log) // SC AC SA
eststo Saturated:   glm freq i.female##i.age##i.alcohol, fam(pois) link(log)                              // SAC

esttab, cells(none) scalars(df deviance deviance_p p) noobs nomtitles nonumber
esttab r(stats, transpose fmt(0 1 1 3)), collabels("df" "L squared" "Pearson chi-squared" "P") ///
                            mtitle("") modelwidth(20) ///
                            labcol2("[S][A][C]" "[SA][C]" "[SC][SA]" "[SA][AC]" "[SC][AC]" "[SC][AC][SA]" "[SAC]")

// Table 5.4
ca (demo: age female) alcohol [fw = freq], plot

Oct 26, 2019

Inverse probability of treatment weighting

// Allbus 2018
use xr19 sex age isced97 eastwest german pt09 pt10 wghtpew bik using "ZA5272_v1-0-0.dta", clear

// Outcome: Trust in media
recode pt09 pt10 (-9 = .)
alpha pt09 pt10 
scores trust = mean(pt09 pt10), nv(1)
label var trust "Trust in media"
drop pt09 pt10 

// Treatment: Internet use
gen internet = (xr19 == 1) if !inlist(xr19, -9, -8)
label define internet 1 "Internet user" 0 "Internet non-user"
label val internet internet
drop xr19

// Gender
gen female = (sex == 2)
label var female "Female sex"
drop sex

// Age
recode age (-32 = .)
label var age "Age"

// Education
recode isced97 (-32 = .)
label define isced97 1 "Basic" 2 "Lower secondary" 3 "Upper secondary" 4 "Post-secondary" 5 "Tertiary first" 6 "Tertiary second", modify
label var isced97 "Education"

// East
generate east = (eastwest == 2)
label var east "Eastern Germany"
drop eastwest

// German
recode german (1 = 0) (2 = 0) (3 = 1) (-50 = 1), gen(foreign)
label var foreign "Foreign citizen"
drop german

// Region
recode bik (-34 = .)
label define bik  1 "-1,999 inh." ///
                  2 "2,000-4,999 inh." ///
                  3 "5,000-19,999 inh." ///
                  4 "Zone 1-4, -50,000 inh." ///
                  5 "Zone 2-4, -100,000 inh." ///
                  6 "Zone 1, -100,000 inh." ///
                  7 "Zone 2-4, -500,000 inh." ///
                  8 "Zone 1, -500,000 inh." ///
                  9 "Zone 2-4, 499,999+ inh." ///
                 10 "Zone 1, 499,999+ inh.", modify
label var bik "Commuting zone"

// Keep only complete cases
keep if !missing(trust, internet, female, age, isced97, east, foreign, bik)

// See how imbalanced covariates are
  // Generate dummies for first table
foreach x of varlist isced97 bik {               // Plug in categorical variables here
  qui tab `x', gen(`x'x)                         // Create dummy variables
  foreach var of varlist `x'x* {
   local lab `: var label `var''
   *di "`lab'"                                   // Display label
   *di strpos("`lab'", "==") 
   local i = strpos("`lab'", "==") + 2
   local lab `: di substr("`lab'",  `i', .)'
   di "`lab'"
   label var `var' "  `lab'"                // Add space to label
  }
}
  // Calculate means
eststo clear
estpost tabstat trust female isced97x* age foreign east bikx*, by(internet) ///
                                                       statistics(mean sd) ///
                                                       columns(statistics) ///
                                                       casewise
   // Look at table
esttab, unstack cells(mean(fmt(2)) sd(fmt(2) par keep(trust age))) nonumber ///
        label collabels("Mean (SD)/Prop.") modelwidth(25) varwidth(25) ///
        refcat(isced97x1 "Education:" bikx1 "Communting zone:", nol) 
drop isced97x* bikx* // Drop variables created for table
eststo clear

// Alternative approach
  // Run desired model first
qui teffects ipw (trust) (internet east##(c.age##c.age) female foreign ib3.isced97 i.bik, logit)
  // Table of covariates
tebalance summarize, baseline // Same table as created above
matrix before = r(table)
matrix list before
  // Table of covariates after weighting
tebalance summarize
matrix after = r(table)
matrix list after
  // Merge tables
matrix both = before, after
matrix list both

  // Look at table
esttab matrix(both, fmt(2)), ///
       label nomtitle ///
       coeflabel(1.east "Eastern Germany" ///
                 age "Age" ///
                 c.age#c.age "Age X age" ///
                 1.east#c.age "Eastern Germany X age" ///
                 1.east#c.age#c.age "Eastern Germany X age X age") ///
       refcat(1.isced97 "Education:" 2.bik "Commuting zone:", nol) ///
       varwidth(30) modelwidth(18) ///
       collabel("Control (mean)" "Treated (mean)" "Control (var.)" "Treated (var.)" "Std. diff." "Std. diff. weighted" "Var. ratio" "Var. ratio weighted")

// Omnibus test: tests hypothesis that treatment model balances the covariates
tebalance overid

// Check overlap
teffects overlap, xtitle(Propensity score Internet non-user) ///
                  ytitle(Density) ///
                  legend(order(1 "Internet non-user" 2 "Internet user") pos(2) ring(0)) 

// IPTW
eststo: teffects ipw (trust) (internet east##(c.age##c.age) female foreign ib3.isced97 i.bik, logit), ate
eststo: teffects ipw (trust) (internet east##(c.age##c.age) female foreign ib3.isced97 i.bik, logit), atet

// Doing things by hand:
// Calculate weight for IPTW, ATE
logit internet east##(c.age##c.age) female foreign ib3.isced97 i.bik
predict probab
generate wate = .
replace  wate = 1/probab       if internet == 1
replace  wate = 1/(1 - probab) if internet == 0

// Calculate weight for IPTW, ATT
generate watt = .
replace  watt = 1                   if internet == 1
replace  watt = probab/(1 - probab) if internet == 0

// IPTW by hand
eststo: regress trust i.internet [pw = wate]
eststo: regress trust i.internet [pw = watt]
esttab, b(2) se(2) keep(main:) drop(_cons) rename(r1vs0.internet 1.internet) ///
        nobaselevels mtitles("IPTW ATE" "IPTW ATT" "IPTW ATE" "IPTW ATT") ///
        mgroup("teffects" "regress", pattern(1 0 1 0))

// Doubly robust
eststo: teffects ipwra (trust          east##(c.age##c.age) female foreign ib3.isced97 i.bik) ///
                     (internet east##(c.age##c.age) female foreign ib3.isced97 i.bik, logit)
eststo: teffects ipwra (trust          east##(c.age##c.age) female foreign ib3.isced97 i.bik) ///
                     (internet east##(c.age##c.age) female foreign ib3.isced97 i.bik, logit), atet
esttab, b(2) se(2) keep(main:) drop(_cons) rename(r1vs0.internet 1.internet) ///
        nobaselevels mtitles("IPTW ATE" "IPTW ATT" "IPTW ATE" "IPTW ATT" "IPTW RA ATE" "IPTW RA ATT") ///
        mgroup("teffects ipw" "regress" "teffects ipwra", pattern(1 0 1 0 1 0))

// Doubly robust by hand
tempvar internet0a internet1a internet0b internet1b ate att

// ATE
regress trust east##(c.age##c.age) female foreign ib3.isced97 i.bik if !internet [pw = wate]
predict `internet0a'
regress trust east##(c.age##c.age) female foreign ib3.isced97 i.bik if internet [pw = wate]
predict `internet1a'
generate `ate' = (`internet1a' - `internet0a')
summarize `ate'

// ATT
regress trust east##(c.age##c.age) female foreign ib3.isced97 i.bik if !internet [pw = watt]
predict `internet0b'
regress trust east##(c.age##c.age) female foreign ib3.isced97 i.bik if internet [pw = watt]
predict `internet1b'
generate `att' = (`internet1b' - `internet0b')
summarize `att'