. sysuse bplong, clear
(fictional blood-pressure data)
. keep if when=="After":when
(120 observations deleted)
. isid patient
. /* summary stats */
. table sex, c(mean bp semean bp sd bp N bp)
Sex | mean(bp) sem(bp) sd(bp) N(bp)
----------+-----------------------------------------------
Male | 155.5167 1.967891 15.24322 60
Female | 147.2 1.515979 11.74272 60
. label list sex
sex:
0 Male
1 Female
. set seed 10122020
.
. /* (A) Absolute effect */
. ttest bp, by(sex) reverse
Two-sample t test with equal variances
Group | Obs Mean Std. Err. Std. Dev. [95% Conf. Interval]
---------+--------------------------------------------------------------------
Female | 60 147.2 1.515979 11.74272 144.1665 150.2335
Male | 60 155.5167 1.967891 15.24322 151.5789 159.4544
---------+--------------------------------------------------------------------
combined | 120 151.3583 1.294234 14.17762 148.7956 153.921
---------+--------------------------------------------------------------------
diff | -8.316667 2.484107 -13.23587 -3.397459
diff = mean(Female) - mean(Male) t = -3.3480
Ho: diff = 0 degrees of freedom = 118
Ha: diff < 0 Ha: diff != 0 Ha: diff > 0
Pr(T < t) = 0.0005 Pr(|T| > |t|) = 0.0011 Pr(T > t) = 0.9995
. regress bp i.sex
Source | SS df MS Number of obs = 120
-------------+---------------------------------- F(1, 118) = 11.21
Model | 2075.00833 1 2075.00833 Prob > F = 0.0011
Residual | 21844.5833 118 185.123588 R-squared = 0.0867
-------------+---------------------------------- Adj R-squared = 0.0790
Total | 23919.5917 119 201.004972 Root MSE = 13.606
bp | Coef. Std. Err. t P>|t| [95% Conf. Interval]
-------------+----------------------------------------------------------------
sex |
Female | -8.316667 2.484107 -3.35 0.001 -13.23587 -3.397459
_cons | 155.5167 1.756529 88.54 0.000 152.0383 158.9951
.
. /* (B) Relative Effect /
.
. / (-1) logged outcome t-test (works for strictly positive data and small relative differences) */
. generate ln_bp = ln(bp)
. ttest ln_bp, by(sex) reverse
Two-sample t test with equal variances
Group | Obs Mean Std. Err. Std. Dev. [95% Conf. Interval]
---------+--------------------------------------------------------------------
Female | 60 4.988759 .0100682 .0779881 4.968612 5.008905
Male | 60 5.041963 .0127957 .0991153 5.016358 5.067567
---------+--------------------------------------------------------------------
combined | 120 5.015361 .0084655 .092735 4.998598 5.032123
---------+--------------------------------------------------------------------
diff | -.053204 .0162819 -.0854466 -.0209615
diff = mean(Female) - mean(Male) t = -3.2677
Ho: diff = 0 degrees of freedom = 118
Ha: diff < 0 Ha: diff != 0 Ha: diff > 0
Pr(T < t) = 0.0007 Pr(|T| > |t|) = 0.0014 Pr(T > t) = 0.9993
.
. /* (0) bootstrap means */
. capture program drop mybs
. program define mybs, rclass
quietly summarize bp if sex=="Female":sex
scalar female_avg_bp = r(mean)
quietly summarize bp if sex=="Male":sex
scalar male_avg_bp = r(mean)
return scalar ratio = (female_avg_bp - male_avg_bp)/male_avg_bp
end
.
. bootstrap ratio = r(ratio), reps(500) nodots nowarn: mybs
Bootstrap results Number of obs = 120
Replications = 500
command: mybs
ratio: r(ratio)
| Observed Bootstrap Normal-based
| Coef. Std. Err. z P>|z| [95% Conf. Interval]
-------------+----------------------------------------------------------------
ratio | -.0534777 .0153194 -3.49 0.000 -.0835031 -.0234522
.
.
. /* (1b) delta method using regression and ratio of predictions by hand */
. regress bp i.sex
Source | SS df MS Number of obs = 120
-------------+---------------------------------- F(1, 118) = 11.21
Model | 2075.00833 1 2075.00833 Prob > F = 0.0011
Residual | 21844.5833 118 185.123588 R-squared = 0.0867
-------------+---------------------------------- Adj R-squared = 0.0790
Total | 23919.5917 119 201.004972 Root MSE = 13.606
bp | Coef. Std. Err. t P>|t| [95% Conf. Interval]
-------------+----------------------------------------------------------------
sex |
Female | -8.316667 2.484107 -3.35 0.001 -13.23587 -3.397459
_cons | 155.5167 1.756529 88.54 0.000 152.0383 158.9951
. nlcom ratio:(_b[1.sex])/_b[_cons]
ratio: (_b[1.sex])/_b[_cons]
bp | Coef. Std. Err. z P>|z| [95% Conf. Interval]
-------------+----------------------------------------------------------------
ratio | -.0534777 .015552 -3.44 0.001 -.083959 -.0229963
. margins, eydx(sex) // another way: calculate the elasticity
Conditional marginal effects Number of obs = 120
Model VCE : OLS
Expression : Linear prediction, predict()
ey/dx w.r.t. : 1.sex
| Delta-method
| ey/dx Std. Err. t P>|t| [95% Conf. Interval]
-------------+----------------------------------------------------------------
sex |
Female | -.0549607 .0164307 -3.35 0.001 -.0874979 -.0224235
Note: ey/dx for factor levels is the discrete change from the base level.
.
. /* (2a) logged outcome regression /
. / works for strictly positive data and small relative differences */
. regress ln_bp i.sex
Source | SS df MS Number of obs = 120
-------------+---------------------------------- F(1, 118) = 10.68
Model | .084920032 1 .084920032 Prob > F = 0.0014
Residual | .938452791 118 .00795299 R-squared = 0.0830
-------------+---------------------------------- Adj R-squared = 0.0752
Total | 1.02337282 119 .008599772 Root MSE = .08918
ln_bp | Coef. Std. Err. t P>|t| [95% Conf. Interval]
-------------+----------------------------------------------------------------
sex |
Female | -.053204 .0162819 -3.27 0.001 -.0854466 -.0209615
_cons | 5.041963 .011513 437.94 0.000 5.019164 5.064762
.
. /* (2b) GLM with exponentiated coefficients */
. glm bp i.sex, family(gaussian) link(log) nolog
Generalized linear models Number of obs = 120
Optimization : ML Residual df = 118
Scale parameter = 185.1236
Deviance = 21844.58333 (1/df) Deviance = 185.1236
Pearson = 21844.58333 (1/df) Pearson = 185.1236
Variance function: V(u) = 1 [Gaussian]
Link function : g(u) = ln(u) [Log]
AIC = 8.075427
Log likelihood = -482.5256155 BIC = 21279.66
| OIM
bp | Coef. Std. Err. z P>|z| [95% Conf. Interval]
-------------+----------------------------------------------------------------
sex |
Female | -.0549607 .0164307 -3.35 0.001 -.0871643 -.0227571
_cons | 5.046753 .0112948 446.82 0.000 5.024616 5.06889
.
. /* (3) bootstrap ratio of predictions from regression by hand */
. bootstrap ratio = (_b[1.sex]/_b[_cons]), reps(500) nodots: regress bp i.sex
Linear regression Number of obs = 120
Replications = 500
command: regress bp i.sex
ratio: _b[1.sex]/_b[_cons]
| Observed Bootstrap Normal-based
| Coef. Std. Err. z P>|z| [95% Conf. Interval]
-------------+----------------------------------------------------------------
ratio | -.0534777 .0147228 -3.63 0.000 -.0823338 -.0246215
.
. /* (4) Fieller's method (uncorrelated means) /
. / there is also a correlated means version */
. fieller bp, by(sex) reverse
Confidence Interval for a Quotient by Fieller's Method (Unpaired Data)
Numerator Mean: 147.2
Denominator Mean: 155.51667
Quotient: .94652234
95% CI: .91652092�.97771318
.
. /* (5) delta method by hand (uncorrelated means) /
. / there is also a correlated means version */
. table sex, c(mean bp sd bp N bp)
Sex | mean(bp) sd(bp) N(bp)
----------+-----------------------------------
Male | 155.5167 15.24322 60
Female | 147.2 11.74272 60
. display "SE(ratio) = " sqrt(((15.24322^2/60)(155.5167)^2+(11.74272^2/60)(147.2)^2)/(155.5167^4))
SE(ratio) = .01566056