-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stata Tables.do
119 lines (92 loc) · 3.23 KB
/
Stata Tables.do
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
***************************************************************
*** Program Name: Stata Tables.do
***
*** Notes: This program contains information on the commands used
*** with Stata to generate tables that are compatible with StatTag
***
*** Date Created: 5 January, 2017
***************************************************************
* pull up the "bpwide" dataset that comes with Stata
sysuse bpwide
*generating additional variable,"intervention", through binomial distribution random sampling with probability of ~0.5 to be assigned to intervention group
set seed 20151103
gen intervention=rbinomial(1,0.5)
label variable intervention "1=intervention 0=control"
*generate the difference in bp
gen bp_diff=bp_after-bp_before
label variable bp_diff "Difference in BP"
*installation of estout package
ssc install estout, replace
*Total N
count if bp_before != . & bp_after != .
local ntotal = r(N)
global totn = r(N)
*Intervention and Control N
count if intervention == 1
local nintervention = r(N)
local ncontrol = `ntotal' - r(N)
*Variables to Store results
gen str12 rowname = ""
gen control1 = .
gen control2 = .
gen int1 = .
gen int2 = .
gen pval = .
*Lists of variables(one for categorical, one for continuous)
global catlist sex agegrp
global conlist bp_before bp_after bp_diff
*Row Counter
gen nn = _n
global rowct 1
* cycle through and fill out table 1
* note this is hard coded for intervention with 2 levels
* coded as 0 for control and 1 for intervention
foreach var of global catlist {
qui tabulate `var' intervention, chi2
replace pval = r(p) if nn == $rowct
levelsof `var', local(varlevs)
foreach lev of local varlevs {
replace rowname = "`var' = `lev'" if nn == $rowct
qui count if `var' == `lev' & intervention == 0
replace control1 = r(N) if nn == $rowct
replace control2 = r(N)/$totn if nn == $rowct
qui count if `var'== `lev' & intervention == 1
replace int1 = r(N) if nn == $rowct
replace int2 = r(N)/$totn if nn == $rowct
global rowct = $rowct + 1
}
}
foreach var of global conlist {
replace rowname = "`var'" if nn == $rowct
qui summarize `var' if intervention == 0
replace control1 = r(mean) if nn == $rowct
replace control2 = r(sd) if nn == $rowct
qui summarize `var' if intervention == 1
replace int1 = r(mean) if nn == $rowct
replace int2 = r(sd) if nn == $rowct
qui ttest `var', by(intervention)
replace pval = r(p) if nn == $rowct
global rowct = $rowct + 1
}
mkmat control1 - pval if nn < $rowct, matrix(Table1) rownames(rowname)
*Run the regression models
regress bp_diff sex
estimates store model1
xi: regress bp_diff i.agegrp
estimates store model2
regress bp_diff intervention
estimates store model3
xi: regress bp_diff sex i.agegrp intervention
estimates store model4
estimates table model1 model2 model3 model4
matrix define A = r(coef)
estout model1 model2 model3 model4, cells("b p")
matrix define B = r(coefs)
estimates table model1 model2
matrix list r(coef)
*Simple matrix of tabulated results
tabulate sex intervention, matcell(A)
matrix list A
*using the mkmat command
mkmat control1 - pval if nn < $rowct, matrix(B) rownames(rowname)
matrix list B