1 / 36

Stata: Linear Regression Stata 3, linear regression

Stata: Linear Regression Stata 3, linear regression. Hein Stigum Presentation, data and programs at: http://folk.uio.no/heins/ courses. Birth weight by gestational age. Synthetic data example. Linear regression. Birth weight by gestational age. Regression idea.

Download Presentation

Stata: Linear Regression Stata 3, linear regression

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Stata: Linear RegressionStata 3, linear regression Hein Stigum Presentation, data and programs at: http://folk.uio.no/heins/ courses H.S.

  2. Birth weight by gestational age Synthetic data example H.S.

  3. Linear regression Birth weight by gestational age H.S.

  4. Regression idea H.S.

  5. Model, measure and assumptions • Model • Association measure 1 = change in y for one unit increase in x1 • Assumptions • Independent errors • Linear effects • Constant error variance • Robustness • influence H.S.

  6. Association measure Model: Start with: Hence: H.S.

  7. Purpose of regression • Estimation • Estimate association between outcome and exposure adjusted for other covariates • Prediction • Use an estimated model to predict the outcome given covariates in a new dataset H.S.

  8. Outcome distributions by exposure Linear regression Quantileregression or cutoff, logistic regression Linear regression or transform, linearregression H.S.

  9. C2 education C1 sex E gest age D birth weight Workflow • DAG • Scatter- and densityplots • Bivariate analysis • Regression • Model estimation • Test of assumptions • Independent errors • Linear effects • Constant error variance • Robustness • Influence H.S.

  10. Scatter and density plots Scatter of birth weight by gestational age Distribution of birth weight for low/high gestational age Look for deviations from linearity and outliers Look for shift in shape H.S.

  11. Syntax • Estimation • regressy x1 x2linear regression • regressyc.agei.sexcontinuous age, categorical sex • regressyc.age##i.sexmain+interaction • Compare models • estimates store m1save model • estimates table m1 m2comparecoefficients • estimates stats m1 m2 compare model fit • Post estimation • predictres, residualspredict residuals H.S.

  12. Model 1: outcome+exposure regress bw gestcrude model estimates store m1store model results H.S.

  13. Model 2 and 3: Add covariates regress bw gest i.educ sexadd covariates estimates table m1 m2 m3compare coefs Estimate association: m1 is biased, m2=m3 m3 more precise? estimates stats m1 m2 m3compare fit Prediction: m3 is best H.S.

  14. Factor (categorical) variables • Variable • educ = 1, 2, 3 for low, medium and higheducation • Built in • i.educuseeduc=1 as base (reference) • ib3.educuseeduc=3 as base (reference) • Manual “dummies” • educ=1 as base, make dummiesfor 2 and 3 • generateMedium =(educ==2) ifeduc<. • generateHigh =(educ==3) ifeduc<. H.S.

  15. Create meaningful constant Expected birth weight: Expected birth weight at: gest= 0, educ=1, sex=0, not meaningful gest=280, educ=1, sex=0 Margins: margins, at(gest= 0 educ=1 sex=0) = -1572not meaningful margins, at(gest= 280 educ=1 sex=0) = 3426 H.S.

  16. Results so far Would normally check for interaction now! H.S.

  17. Assumptions H.S.

  18. Test of assumptions • Assumptions • Independent residuals: • Linear effects: • Constant variance: discuss plot residuals versus predicted y predict res, residuals predict pred, xb scatter res pred estat hettest p=0.9 no heteroskedasticity H.S.

  19. Violations of assumptions • Dependent residuals Use mixed models or GEE • Non linear effects Add square term or spline • Non-constant variance Use robust variance estimation H.S.

  20. Measures of influence Robustness H.S.

  21. Influence idea H.S.

  22. Measures of influence • Measure change in: • Predicted outcome • Deviance • Coefficients (beta) • Delta beta Remove obs 1, see change remove obs 2, see change H.S.

  23. Leverage versus residuals2 lvr2plot, mlabel(id) high influence H.S.

  24. Delta-beta for gestational age dfbeta(gest) scatter _dfbeta_1 id OBS, variable specific If obs nr 370 is removed, beta will change from 17.9 to 18.6 H.S.

  25. Removing outlier regress bw gest i.educ sex if id!=370 est store m4 est table m3 m4, b(%8.1f) H.S.

  26. Removing outlier Full model N=5000 Outlier removed N=4999 One outlier affected several estimates Final model H.S.

  27. Help • Linear regression • help regress • syntax and options • help regress postestimation • dfbeta • estat hettest • lvr2plot • predict • margins H.S.

  28. bw2 Non-lineareffects H.S.

  29. bw2: Non-linear effects Handle: add polynomial or spline H.S.

  30. Non-linear effects: polynomial regress bw2 c.gest##c.gesti.educ sex 2. order polynomial in gest margins, at(gest=(250(10)310))predicted bw2 by gest marginsplotplot H.S.

  31. Non-linear effects: spline • Qubic spline • Plot • Linear spline mkspline g=gest, cubic nknots(4) make spline with 4 knots regress bw2 g1 g2 g3i.educsexregression with spline gen igest=5*round(gest/5) 5-year integer values of gest margins, over(igest) predicted bw by gest marginsplot mksplineg1 280 g2=gestmake linear spline with knot at 280 regress bw2 g1 g2i.educsexregression with spline H.S.

  32. bw3 Interaction H.S.

  33. Interaction definitions • Interaction: combined effect of two variables • Scale • Linear models additive • y=b0+b1x1+b2x2 both x1and x2 = b1+b2 • Logistic, Poisson, Cox multiplicative • Interaction • deviation from additivity (multiplicativity)  • effect of x1 depends on x2 H.S.

  34. bw3: Interaction (only linear effects) • Add interaction terms • Show results regress bw3 c.gest##i.sexi.educgest-sex interaction margins, dydx(gest) at(sex=0) effect of gest for boys margins, dydx(gest) at(sex=1) effect of gest for girls H.S.

  35. Summing up 1 • Build model • regressbw gest crude model • est store m1 store • regressbw gest i.educ sexfull model • est store m2 • est table m1 m2 compare coefficients • Interaction • regress bw3 c.gest##i.sex i.eductest interaction • margins, dydx(gest) at(sex=0) gest for boys • Assumptions • predict res, residuals residuals • predict pred, xb predicted • scatter res pred plot H.S.

  36. Summing up 2 • Non-linearity (linear spline) • mkspline g1 280 g2=gestspline with knot at 280 • regress bw2 g1 g2 i.educ sexregression with spline • Robustness • dfbeta(gest) delta-beta • scatter _dfbeta_1 idplot versus id H.S.

More Related