Scientific work done easy in Stata - Back to basics!! Or a ...Settingthescene Describingdata...
Transcript of Scientific work done easy in Stata - Back to basics!! Or a ...Settingthescene Describingdata...
Setting the sceneDescribing data
Presenting estimated effectsEffect modification by the Apache II score
The effect of Ibuprofen on body temperatureThe end
Scientific work done easy in StataBack to basics!! Or a statistical fairy tale.
Niels Henrik Bruun
Dept. Of Public Health, Aarhus University
Niels Henrik Bruun Scientific work done easy in Stata
Setting the sceneDescribing data
Presenting estimated effectsEffect modification by the Apache II score
The effect of Ibuprofen on body temperatureThe end
1 Setting the scene
2 Describing data
3 Presenting estimated effects
4 Effect modification by the Apache II score
5 The effect of Ibuprofen on body temperature
6 The end
Niels Henrik Bruun Scientific work done easy in Stata
Setting the sceneDescribing data
Presenting estimated effectsEffect modification by the Apache II score
The effect of Ibuprofen on body temperatureThe end
The article
From Bernard et al. (1997):
Background In patients with sepsis the production of arachidonic acid metabolites bycyclooxygenase increases, but the pathophysiologic role of these prostaglandins is unclear. Inanimal models, inhibition of cyclooxygenase by treatment with ibuprofen before the onset ofsepsis reduces physiologic abnormalities and improves survival. In pilot studies of patientswith sepsis, treatment with ibuprofen led to improvements in gas exchange and airwaymechanics.
Conclusion In patients with sepsis, treatment with ibuprofen reduces levels of prostacyclin andthromboxane and decreases fever, tachycardia, oxygen consumption, and lactic acidosis, but itdoes not prevent the development of shock or the acute respiratory distress syndrome and doesnot improve survival.
Niels Henrik Bruun Scientific work done easy in Stata
Setting the sceneDescribing data
Presenting estimated effectsEffect modification by the Apache II score
The effect of Ibuprofen on body temperatureThe end
The data
The dataset of 455 sepsis patients are from W. Dupont (2004) and described in W. Dupont(2009).use "http://biostat.mc.vanderbilt.edu/dupontwd/wddtext/data/1.4.11.Sepsis.dta", clear
Temperature variables are converted to deg. Celsius and getting new variable labelsforeach var of varlist temp* {
2. if regexm("`:variable label `var''", "([0-9]+)") local time = regexs(1) // Get time from label3. else local time = 0 // Set time to 0 as default4. quietly replace `var' = (`var' - 32) * 5 / 9 // To deg. Celsius5. label variable `var' `"Temp. (deg. C) at `time' hours"'6. rename `var' tempc`time'7. }
Note foreach, if, and regex
Niels Henrik Bruun Scientific work done easy in Stata
Setting the sceneDescribing data
Presenting estimated effectsEffect modification by the Apache II score
The effect of Ibuprofen on body temperatureThe end
The research questions here1. Estimate the effect (risk difference) of ibuprofen on mortality in treatment of sepsis
2. Is there an (statistical) effect modification by the Apache II score?
Primary outcome: Overall survival at 30 days (fate)Exposure: Ibuprofen and placebo groups (treatment) - RCT, blindedBaseline risk factors: Apache II score (apache), race (race), temperature (tempc0)
The score APACHE II (“Acute Physiology and Chronic Health Evaluation II”) is aseverity-of-disease ICU scoring systems. The score is calculated from the baseline data. Higherscores correspond to more severe disease and a higher risk of death.
3. The effect on temperature (mean diff.) of ibuprofen in treatment of sepsis over time
Secondary outcome: Temperature development over time in the treatment groupsExposure: Ibuprofen and placebo groups (treatment)Time effect: Time (time) in hours from baseline
Niels Henrik Bruun Scientific work done easy in Stata
Setting the sceneDescribing data
Presenting estimated effectsEffect modification by the Apache II score
The effect of Ibuprofen on body temperatureThe end
This is an RCT study!!!
Both outcome and exposure are complete, ie no missings (next slide)
No need for adjustment on confounders in RCTRandomisation should remove confounding effects
Effect modification in RCT?Effect modifiers in RCT exists, affecting only outcome
We continue, keeping the above in mind
Niels Henrik Bruun Scientific work done easy in Stata
Setting the sceneDescribing data
Presenting estimated effectsEffect modification by the Apache II score
The effect of Ibuprofen on body temperatureThe end
-metadata-, also for all datasets in folders and subfolders (Good at DST)metadata
Name Index Label Value Label Name Format Value Label Values n unique missingid 1 Patient ID %9.0g 455 455 0treat 2 Treatment treatmnt %9.0g 0 "Placebo" 1 "Ibuprofen" 455 2 0race 3 Race race %9.0g 0 "White" 1 "Black" 2 "Other" 455 3 0apache 4 Baseline APACHE Score %9.0g 454 38 1o2del 5 Oxygen Delivery at Baseline (ml/min/m2̂) %9.0g 168 168 287fate 6 Mortal Status at 30 Days fate %9.0g 0 "Alive" 1 "Dead" 455 2 0followup 7 Follow-up (hours) %9.0g 455 148 0tempc0 8 Temp. (deg. C) at 0 hours %9.0g 455 122 0tempc2 9 Temp. (deg. C) at 2 hours %9.0g 420 106 35tempc4 10 Temp. (deg. C) at 4 hours %9.0g 402 108 53tempc8 11 Temp. (deg. C) at 8 hours %9.0g 418 113 37tempc12 12 Temp. (deg. C) at 12 hours %9.0g 421 111 34tempc16 13 Temp. (deg. C) at 16 hours %9.0g 422 113 33tempc20 14 Temp. (deg. C) at 20 hours %9.0g 432 108 23tempc24 15 Temp. (deg. C) at 24 hours %9.0g 413 105 42tempc28 16 Temp. (deg. C) at 28 hours %9.0g 407 105 48tempc32 17 Temp. (deg. C) at 32 hours %9.0g 401 102 54tempc36 18 Temp. (deg. C) at 36 hours %9.0g 399 101 56tempc40 19 Temp. (deg. C) at 40 hours %9.0g 402 98 53tempc44 20 Temp. (deg. C) at 44 hours %9.0g 406 97 49tempc72 21 Temp. (deg. C) at 72 hours %9.0g 403 104 52tempc96 22 Temp. (deg. C) at 96 hours %9.0g 316 87 139tempc120 23 Temp. (deg. C) at 120 hours %9.0g 382 93 73
Niels Henrik Bruun Scientific work done easy in Stata
Setting the sceneDescribing data
Presenting estimated effectsEffect modification by the Apache II score
The effect of Ibuprofen on body temperatureThe end
-sumat-
Similar to -summarize-, -means-, -table- and many more. Handles e.g. missings, unique values -also for string variablessumat apache tempc0, statistics(n mean ci) rowby(race) colby(treat) decimals((0,2,2,2,0,2))
matrix tbl = r(sumat) // Keep output for export
Treatment(Placebo) Treatment(Ibuprofen)n mean ci95% lb ci95% ub n mean ci95% lb ci95% ub
Race(White) Baseline APACHE Score 155 14.21 13.27 15.15 137 14.93 13.76 16.11Temp (deg C) at 0 hours 156 38.09 37.94 38.23 137 38.06 37.88 38.24
Race(Black) Baseline APACHE Score 58 18.00 15.85 20.15 72 16.97 15.17 18.78Temp (deg C) at 0 hours 58 37.79 37.45 38.13 72 37.87 37.54 38.20
Race(Other) Baseline APACHE Score 17 14.47 10.90 18.04 15 13.27 10.52 16.01Temp (deg C) at 0 hours 17 38.60 38.04 39.16 15 37.72 37.20 38.24
Niels Henrik Bruun Scientific work done easy in Stata
Setting the sceneDescribing data
Presenting estimated effectsEffect modification by the Apache II score
The effect of Ibuprofen on body temperatureThe end
Export matrices to Excel using -putexcel- (Stata 14 and up)
putexcel set "mytables", sheet(CI summary, replace) replaceputexcel (a1) = matrix( tbl ), names
Figure 1: Results exported into an Excel file. Format numbers etc in Excel
Niels Henrik Bruun Scientific work done easy in Stata
Setting the sceneDescribing data
Presenting estimated effectsEffect modification by the Apache II score
The effect of Ibuprofen on body temperatureThe end
Export matrices to pdf using -putpdf- (Stata 15 and up)putpdf clearputpdf beginputpdf table tablename = matrix( tbl ), nformat(%6.2f) rownames colnamesputpdf save pdfsumat, replace
Figure 2: Results exported into a PDF fileNiels Henrik Bruun Scientific work done easy in Stata
Setting the sceneDescribing data
Presenting estimated effectsEffect modification by the Apache II score
The effect of Ibuprofen on body temperatureThe end
Export matrices to word using -putdocx- (Stata 15 and up)putdocx clearputdocx beginputdocx table tablename = matrix( tbl ), nformat(%6.2f) rownames colnamesputdocx save wordsumat, replace
Figure 3: Results exported to a word file. Change formats in word.Niels Henrik Bruun Scientific work done easy in Stata
Setting the sceneDescribing data
Presenting estimated effectsEffect modification by the Apache II score
The effect of Ibuprofen on body temperatureThe end
Export matrices to html (or tex/latex) using -matprint- (Stata 12 and up)
matprint tbl using mp_sumat.html, style(html) decimals((0,2,2,2,0,2)) replace
Figure 4: Results exported to a html file via matprint
Copy/paste into other documentsEasy control of decimals
Niels Henrik Bruun Scientific work done easy in Stata
Setting the sceneDescribing data
Presenting estimated effectsEffect modification by the Apache II score
The effect of Ibuprofen on body temperatureThe end
percentiles at DST, combining two matrices before print
The (True/Crude) median and the interquartile intervalsumat apache, statistics(median iqi) rowby(race) colby(treat) decimals(3)matrix sumat1 = r(sumat)matrix roweq sumat1 = Crude
Blurred values for a continuous variable are derived from the variable by sorting thevariable and averaging over a number of nearest neighbors.sumat apache, statistics(median iqi) rowby(race) colby(treat) decimals(3) hide(5)matrix sumat2 = r(sumat)matrix roweq sumat2 = Blurred
Combine the two matrices, the first above the othermatrix sumat = sumat1 \ sumat2
Niels Henrik Bruun Scientific work done easy in Stata
Setting the sceneDescribing data
Presenting estimated effectsEffect modification by the Apache II score
The effect of Ibuprofen on body temperatureThe end
Compare the crude and blurred percentiles
matprint sumat, style(tex) decimals(3)
Treatment(Placebo) Treatment(Ibuprofen)median iq 25% iq 75% median iq 25% iq 75%
Crude Race(White) 14.000 10.000 18.000 14.000 9.000 20.000Race(Black) 17.000 11.750 23.000 16.000 10.000 23.000Race(Other) 12.000 9.000 17.500 13.000 10.000 17.000
Blurred Race(White) 14.000 9.600 18.000 14.000 9.100 20.000Race(Black) 17.300 11.500 23.250 15.900 10.250 22.850Race(Other) 12.600 8.600 19.200 12.200 9.000 17.000
Blurred percentiles are not yet accepted at DST!!
Niels Henrik Bruun Scientific work done easy in Stata
Setting the sceneDescribing data
Presenting estimated effectsEffect modification by the Apache II score
The effect of Ibuprofen on body temperatureThe end
-basetable-, a flexible tool for generating Table 1basetable treat race(c) apache(%6.2f) apache(%6.2f, iqi) tempc0(%6.2f) tempc0(%6.2f, iqi) ///
, missing pvformat(%6.2f, top)
Columns by: Treatment Placebo Ibuprofen Total P-value Missings / N (Pct)n (%) 231 (50.8) 224 (49.2) 455 (100.0) 0 / 455 (0.00)Race, n (%) 0.25 0 / 455 (0.00)White, n (%) 156 (67.5) 137 (61.2) 293 (64.4)Black, n (%) 58 (25.1) 72 (32.1) 130 (28.6)Other, n (%) 17 (7.4) 15 (6.7) 32 (7.0)Baseline APACHE Score, mean (sd) 15.19 (6.92) 15.48 (7.26) 15.33 (7.09) 0.66 1 / 455 (0.22)Baseline APACHE Score, median (iqi) 14.50 (10.00; 19.25) 14.00 (10.00; 21.00) 14.00 (10.00; 20.00) 0.80 1 / 455 (0.22)Temp. (deg. C) at 0 hours, mean (sd) 38.05 (1.06) 37.98 (1.19) 38.01 (1.13) 0.50 0 / 455 (0.00)Temp. (deg. C) at 0 hours, median (iqi) 38.20 (37.70; 38.70) 38.10 (37.35; 38.60) 38.17 (37.50; 38.67) 0.22 0 / 455 (0.00)
Option toxl for export to excelOption smoothdata for blurring median and quartilesOption nopvalue for no pvalue in tableOption nototal for no total in table
Niels Henrik Bruun Scientific work done easy in Stata
Setting the sceneDescribing data
Presenting estimated effectsEffect modification by the Apache II score
The effect of Ibuprofen on body temperatureThe end
-regmat-, a tool for Table 2
regmat, outcome(fate) exposure(treat) adjustment("" "apache tempc0") keep(b ci) label decimals(3): binreg, rd
Adjustment 1 Adjustment 2b Lower 95% CI Upper 95% CI b Lower 95% CI Upper 95% CI
Mortal Status at 30 Days Treatment -0.023 -0.113 0.066 -0.035 -0.110 0.040
Adjustment not necessary in RCTs! Just demonstrating.
Same conclusion as the article.
Niels Henrik Bruun Scientific work done easy in Stata
Setting the sceneDescribing data
Presenting estimated effectsEffect modification by the Apache II score
The effect of Ibuprofen on body temperatureThe end
-estout-, Jann (2004)
Preparation for -estout- and -coefplot-binreg fate treat, rdestimates store crudebinreg fate treat apache tempc0, rdestimates store adjustment
Table by -estout- showing similar results as -regmat-estout crude adjustment, cells("b(fmt(3)) ci(fmt(3))") keep(treat) label
------------------------------------------------------------------------crude adjustment
b ci95 b ci95------------------------------------------------------------------------Treatment -0.023 -0.113,0.066 -0.035 -0.110,0.040------------------------------------------------------------------------
Niels Henrik Bruun Scientific work done easy in Stata
Setting the sceneDescribing data
Presenting estimated effectsEffect modification by the Apache II score
The effect of Ibuprofen on body temperatureThe end
-coefplot-, Jann (2013)coefplot crude adjustment, keep(treat) label xline(0, lcolor(red%40)) ///
mlabel(string(@b, "%5.3f") + " (" + string(@ll, "%5.3f") + "; " + string(@ul, "%5.3f") + ")") ///caption("Favors Ibuprofen", size(small) position(7) orientation(horizontal) ring(0)) ///note("Favors placebo", size(small) position(5) ring(0)) ///
mlabposition(12) mlabsize(small) xtitle(Risk difference) name(coefplot, replace)
Figure 5: Graph from coefplot showing the results from regmat and/or estout.Niels Henrik Bruun Scientific work done easy in Stata
Setting the sceneDescribing data
Presenting estimated effectsEffect modification by the Apache II score
The effect of Ibuprofen on body temperatureThe end
Effect modification by the Apache II score
A definition: The effect of one variable (treat) on another (fate) varies across (strata) of athird (apache)
Niels Henrik Bruun Scientific work done easy in Stata
Setting the sceneDescribing data
Presenting estimated effectsEffect modification by the Apache II score
The effect of Ibuprofen on body temperatureThe end
About margins
“Margins are statistics calculated from predictions of a previously fit model at fixed values ofsome covariates and averaging or otherwise integrating over the remaining covariates.
The margins command estimates margins of responses for specified values of covariates andpresents the results as a table."
See StataCorp LLC (2017) and Mitchell (2012)
Niels Henrik Bruun Scientific work done easy in Stata
Setting the sceneDescribing data
Presenting estimated effectsEffect modification by the Apache II score
The effect of Ibuprofen on body temperatureThe end
-margins-, assuming linear effect modification by the Apache II scorebinreg fate i.treat##c.apache tempc0, rdestimates store lin_eff_modmargins, dydx(treat) at(apache=(5(10)35)) noatlegend
Average marginal effects Number of obs = 454Model VCE : EIM
Expression : Predicted mean fate, predict()dy/dx w.r.t. : 1.treat
------------------------------------------------------------------------------| Delta-method| dy/dx Std. Err. z P>|z| [95% Conf. Interval]
-------------+----------------------------------------------------------------0.treat | (base outcome)-------------+----------------------------------------------------------------1.treat |
_at |1 | -0.092 0.055 -1.66 0.10 -0.201 0.0162 | -0.028 0.036 -0.76 0.44 -0.099 0.0433 | 0.037 0.040 0.92 0.36 -0.041 0.1154 | 0.101 0.062 1.62 0.10 -0.021 0.224
------------------------------------------------------------------------------Note: dy/dx for factor levels is the discrete change from the base level.Niels Henrik Bruun Scientific work done easy in Stata
Setting the sceneDescribing data
Presenting estimated effectsEffect modification by the Apache II score
The effect of Ibuprofen on body temperatureThe end
-marginsplot-, visualising marginsmarginsplot, yline(0, lcolor(red)) name(mgplt, replace)
Figure 6: Margins and their 95% confidence intervals. The line between estimates reminds the viewer ofthe assumed linear effect modification. Is there linear effect modification by the Apache II score?
Niels Henrik Bruun Scientific work done easy in Stata
Setting the sceneDescribing data
Presenting estimated effectsEffect modification by the Apache II score
The effect of Ibuprofen on body temperatureThe end
Test for linear effect modification by the Apache II score
estimates store lin_eff_modtestparm i.treat#c.apache
( 1) 1.treat#c.apache = 0
chi2( 1) = 4.09Prob > chi2 = 0.0432
We can not accept the hypothesis of no statistical effect modification by the Apache II score. I.e.there is statistical effect modification
Niels Henrik Bruun Scientific work done easy in Stata
Setting the sceneDescribing data
Presenting estimated effectsEffect modification by the Apache II score
The effect of Ibuprofen on body temperatureThe end
-emc-, mortality for treatment groups dependent on apache
“Prefix command estimating effect measure values (contrasts) and their confidence interval for aset of effect modifier values.”emc, at(0(4)40) caption("Favors Ibuprofen", size(small) position(7) orientation(horizontal) ring(0)) ///note("Favors placebo", size(small) position(11) ring(0)) yline(0, lcolor(red)) name(emc_apache, replace) ///: binreg fate treat apache, rd
__apache __apache_contrast __apache_lb __apache_ub0 -0.495 -0.858 -0.1324 -0.278 -0.480 -0.0778 -0.071 -0.186 0.045
12 0.035 -0.094 0.16516 -0.026 -0.142 0.09120 -0.064 -0.219 0.09124 -0.020 -0.173 0.13328 0.066 -0.102 0.23532 0.160 -0.074 0.39336 0.253 -0.070 0.57540 0.346 -0.074 0.767
Niels Henrik Bruun Scientific work done easy in Stata
Setting the sceneDescribing data
Presenting estimated effectsEffect modification by the Apache II score
The effect of Ibuprofen on body temperatureThe end
Effect of Ibuprofen on mortality by apache score
Figure 7: The risk difference of Ibuprofen on mortality. Does Ibuprofen help when apache score is low? IsIbuprofen bad when apache score is high?
Niels Henrik Bruun Scientific work done easy in Stata
Setting the sceneDescribing data
Presenting estimated effectsEffect modification by the Apache II score
The effect of Ibuprofen on body temperatureThe end
Effect of Ibuprofen on body temperature at sepsis patients over time, 1
Using -reshape- to make the dataset long on temperature and adding a time variablekeep id treat tempc*reshape long tempc, i(id) j(time)label variable time "Time from baseline (hours)"
Making the graphemc, at(0(5)120) caption("Favors Ibuprofen", size(small) position(7) orientation(horizontal) ring(0)) ///note("Favors placebo", size(small) position(11) ring(0)) yline(0) ///xlabel(0(20)120) xline(44) name(emc_tmp, replace) ///: regress tempc i.treat c.time, vce(cluster id)
Niels Henrik Bruun Scientific work done easy in Stata
Setting the sceneDescribing data
Presenting estimated effectsEffect modification by the Apache II score
The effect of Ibuprofen on body temperatureThe end
Effect of Ibuprofen on body temperature at sepsis patients over time, 2
Figure 8: The mean difference on temperture over time between Ibuprofen and placebo at cases of sepsis.Vertical line is the end of 44 hours of ibuprofen therapy. The effect is statistcally clear, lowering thetemperature 0.5 to 0.75 degrees. Clinical effect?
Niels Henrik Bruun Scientific work done easy in Stata
Setting the sceneDescribing data
Presenting estimated effectsEffect modification by the Apache II score
The effect of Ibuprofen on body temperatureThe end
Questions?
See Bruun (2017)
All shown commands are at DST servers
Niels Henrik Bruun Scientific work done easy in Stata
Setting the sceneDescribing data
Presenting estimated effectsEffect modification by the Apache II score
The effect of Ibuprofen on body temperatureThe end
References I
Bernard, Gordon R., Arthur P. Wheeler, James A. Russell, Roland Schein, Warren R. Summer, Kenneth P. Steinberg, William J.Fulkerson, et al. 1997. “The Effects of Ibuprofen on the Physiology and Survival of Patients with Sepsis.” New England Journalof Medicine 336 (13):912–18. https://doi.org/10.1056/NEJM199703273361303.
Bruun, Niels Henrik. 2017. “Hacks for Stata Users.” http://www.bruunisejs.dk/StataHacks/.
Dupont, W.D. 2004. “Statistical Modeling for Biomedical Researchers, Datasets.”http://biostat.mc.vanderbilt.edu/dupontwd/wddtext/index.html.
———. 2009. Statistical Modeling for Biomedical Researchers: A Simple Introduction to the Analysis of Complex Data.Cambridge University Press.
Jann, Ben. 2004. “Estout: Making Regression Tables in Stata.” http://repec.sowi.unibe.ch/stata/estout/.
———. 2013. “COEFPLOT: Stata Module to Plot Regression Coefficients and Other Results.”http://repec.sowi.unibe.ch/stata/coefplot/index.html.
Mitchell, M.N. 2012. Interpreting and Visualizing Regression Models Using Stata. Taylor & Francis.
StataCorp LLC, TX, College Station. 2017. “Stata 15 Base Reference Manual.” https://www.stata.com.
Niels Henrik Bruun Scientific work done easy in Stata