Top Banner
03/14/22 H.S. 1 Stata: Linear Regression Stata 3, linear regression Hein Stigum Presentation, data and programs at: http://folk.uio.no/heins/ courses
36
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Stata 3, Linear Regression v3

04/18/23 H.S. 1

Stata: Linear Regression

Stata 3, linear regression

Hein Stigum

Presentation, data and programs at:

http://folk.uio.no/heins/

courses

Page 2: Stata 3, Linear Regression v3

SYNTHETIC DATA EXAMPLEBirth weight by gestational age

04/18/23 H.S. 2

Page 3: Stata 3, Linear Regression v3

04/18/23 H.S. 3

Linear regression

Birth weight

by

gestational age

Page 4: Stata 3, Linear Regression v3

04/18/23 H.S. 4

Regression idea

residual error,e

xofeffect ,tcoefficienb

covariate =x

outcome=y

:model

1

10

exbby

covariate = x,x

:cofactorsmany with model

21

22110 exbxbby

2500

3000

3500

4000

4500

5000

birt

h w

eigh

t (gr

am

)

250 260 270 280 290 300 310gestational age (days)

Page 5: Stata 3, Linear Regression v3

04/18/23 H.S. 5

Model, measure and assumptions

• Model

• Association measure1 = change in y for one unit increase in x1

• Assumptions– Independent errors

– Linear effects

– Constant error variance

• Robustness– influence

),0(, 222110 Nxxy

Page 6: Stata 3, Linear Regression v3

04/18/23 H.S. 6

Association measure

11

1

2210

2210

121

22110

1

211

βy

β

xβββ

xβββ

yyy

xβxββy

xx

Model:

Start with:

Hence:

Page 7: Stata 3, Linear Regression v3

04/18/23 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

Page 8: Stata 3, Linear Regression v3

Outcome distributions by exposure

Exposed Unexposed

-3 0 1 4Outcome

04/18/23 H.S. 8

Exposed Unexposed

-3 -2 -1 0 1 2 3Outcome

Linear regression

Quantile regressionor

cutoff, logistic regression

0 2 4 6 8Outcome

Linear regressionor

transform,linear regression

Page 9: Stata 3, Linear Regression v3

04/18/23 H.S. 9

Workflow

• DAG

• Scatter- and densityplots

• Bivariate analysis

• Regression– Model estimation– Test of assumptions

• Independent errors• Linear effects• Constant error variance

– Robustness • Influence

Egest age

Dbirth weight

C2education

C1sex

Page 10: Stata 3, Linear Regression v3

Scatter and density plots

Scatter of birth weight by gestational age

Distribution of birth weight for low/high gestational age

04/18/23 H.S. 10

gest<280 days gest>=280 days

0 2000 4000 6000Birth weight (gr)

Look for deviations from linearityand outliers Look for shift in shape

3704962

020

0040

0060

00B

irth

wei

ght (

gr)

240 260 280 300 320 340Gestational age

Page 11: Stata 3, Linear Regression v3

04/18/23 H.S. 11

Syntax

• Estimation– regress y x1 x2 linear regression

– regress y c.age i.sex continuous age, categorical sex

– regress y c.age##i.sex main+interaction

• Compare models– estimates store m1 save model

– estimates table m1 m2 compare coefficients

– estimates stats m1 m2 compare model fit

• Post estimation– predict res, residuals predict residuals

Page 12: Stata 3, Linear Regression v3

04/18/23 H.S. 12

Model 1: outcome+exposure

regress bw gest crude model

estimates store m1 store model results

Page 13: Stata 3, Linear Regression v3

04/18/23 H.S. 13

Model 2 and 3: Add covariates

Estimate association:m1 is biased, m2=m3

Prediction: m3 is best

regress bw gest i.educ sex add covariatesestimates table m1 m2 m3 compare coefs

estimates stats m1 m2 m3 compare fit

m3 more precise?

Page 14: Stata 3, Linear Regression v3

Factor (categorical) variables

• Variable– educ = 1, 2, 3 for low, medium and high education

• Built in– i.educ use educ=1 as base (reference)

– ib3.educ use educ=3 as base (reference)

• Manual “dummies”– educ=1 as base, make dummies for 2 and 3

– generate Medium =(educ==2) if educ<.

– generate High =(educ==3) if educ<.

04/18/23 H.S. 14

Page 15: Stata 3, Linear Regression v3

Create meaningful constant

Expected birth weight at:

sexeduceducgestbwE 43210 32)(

gr1572 0

gest= 0, educ=1, sex=0, not meaningful

gest=280, educ=1, sex=0 gr342628010

Expected birth weight:

Margins:margins, at(gest= 0 educ=1 sex=0) = -1572 not meaningful

margins, at(gest= 280 educ=1 sex=0) = 3426

04/18/23 H.S. 15

Page 16: Stata 3, Linear Regression v3

coeff 95% conf. Int.Birth weight at ref 3426 (3385 , 3467)Gestational age

per day 17.9 (16 , 20)Education

Low 0Medium 71.5 (25 , 118)High 99.1 (51 , 148)

SexBoy 0Girl -154.3 (-187 , -121)

Results so far

04/18/23 H.S. 16

Would normally check for interaction now!

Page 17: Stata 3, Linear Regression v3

ASSUMPTIONS

04/18/23 H.S. 17

Page 18: Stata 3, Linear Regression v3

04/18/23 H.S. 18

Test of assumptions• Assumptions

– Independent residuals:

– Linear effects:

– Constant variance:

-300

0-2

000

-100

00

1000

2000

2500 3000 3500 4000 4500Linear prediction

estat hettestp=0.9 no heteroskedasticity

discuss

plot residuals versus predicted y

predict res, residualspredict pred, xbscatter res pred

Page 19: Stata 3, Linear Regression v3

04/18/23 H.S. 19

Violations of assumptions• Dependent residuals

Use mixed models or GEE

• Non linear effectsAdd square term or spline

• Non-constant varianceUse robust variance estimation

-1-.

50

.51

200 220 240 260 280 300gest

-2-1

01

2re

s

3400 3500 3600 3700 3800p

Page 20: Stata 3, Linear Regression v3

ROBUSTNESSMeasures of influence

04/18/23 H.S. 20

Page 21: Stata 3, Linear Regression v3

04/18/23 H.S. 21

Influence idea

outlier

regression without outlier

regression with outlier

020

0040

0060

00B

irth

wei

ght (

gr)

250 300 350 400Gestational age (days)

Page 22: Stata 3, Linear Regression v3

04/18/23 H.S. 22

Measures of influence

• Measure change in:– Predicted outcome

– Deviance

– Coefficients (beta)• Delta beta

Remove obs 1, see changeremove obs 2, see change

-.6

-.4

-.2

0.2

Influ

ence

1 2 10Id

Page 23: Stata 3, Linear Regression v3

1 2 34 56

7

8910111213

1415 16171819 2021

22

2324252627

2829303132

33

34 35363738

3940

41424344

454647484950

515253

545556575859 6061

6263

64

6566 67

68697071727374 7576

77787980 818283 84

85

86878889 90

91929394

9596

97

9899 100101102103104

105106107108

109110111112113114115116 117118119

120121 122123

124125126127128129130131132133134135

136137138

139140141142143144145146

147148149150

151152153154155156157158

159

160161162

163

164 165166167168 169170171

172173 174175176

177

178179

180181 182

183

184185186

187188189190 191192193194195196

197 198199200201202203204

205

206207208 209210

211212

213214

215

216217218219220221 222223224 225226227228 229230231

232

233234235236

237238239240241242243244 245

246247248249

250251252 253254255 256257 258259260261262263

264

265266267

268269270271272273274

275276277278279

280281282283

284285286287288

289290 291292293

294295296297

298299300301302

303304305

306307308309310311312313314315316317318319320321322323324325326327 328329330

331332333

334

335336337338339340341

342

343344345 346347348

349350

351352353354355

356 357358359

360361

362363

364365366

367368369

370

371372 373374375376

377378 379380381382 383384385386

387 388389390

391392

393

394395

396397

398

399400401

402403404405 406407408409410

411412

413414 415416417418

419420

421422423424

425426 427428

429430

431432433

434

435

436437438439

440441442443444445446

447448449450451452453454

455

456457 458459460461462

463464465466467468469

470471

472473474475476477

478

479480

481482483484485486 487488489

490

491492493494 495

496497498499 500501502503504

505506507

508509510511

512513514515516517518 519

520

521

522523524525 526527528

529

530531532

533534535536537538539540541542543544545546547548549

550 551552553554

555556 557558559560561 562

563 564565

566

567568

569

570

571572573

574575576577

578

579580581582583584585586587588

589590

591592

593594595596597598599600601

602

603604605606607

608609

610611

612613 614615616617

618619620621622

623

624625626627628

629630631

632633634635

636

637638

639

640641642

643644645

646647648

649

650651652653654655656657

658

659660 661662663664665 666667668669

670671

672673674675

676677678679680681 682683684

685686687688

689 690691692693 694695

696697698699700701702703704705706707

708709710711

712713714715716717718719720721722723724725

726

727

728729730731732733734735736 737738739740741742743744745

746747748749

750751752753754

755756757758 759760

761762763764765766

767768769770

771772

773 774775776777 778779 780781782 783784

785

786787788

789790791792793794795796797798799800 801802803804805806807808

809

810811812813814815816817

818

819

820

821

822823

824825826827 828829830831832

833834

835836837

838839840841842

843

844845846

847

848849850

851852853 854855856857858859860861 862

863864865

866867868869870871

872873874

875

876877878879

880881882 883 884885886887

888

889890891892

893894895896897898

899900901902903904905906907908909910911912913914

915

916917

918919920921922923924925926

927928929930931932933934935936

937938

939

940941942943944

945946947948949950951952

953 954955956957958959960961962

963964965 966967968969970971972973974975976977978

979 980981982 983984985986987988

989990991992993994995 996997

998

9991000100110021003

100410051006

10071008100910101011

1012101310141015

10161017101810191020

1021

102210231024102510261027

1028

102910301031103210331034

103510361037

1038

1039104010411042

104310441045

10461047

1048104910501051105210531054105510561057

1058

105910601061

10621063 1064

1065

10661067106810691070107110721073

107410751076

107710781079108010811082

1083108410851086

1087108810891090109110921093

1094

1095109610971098

10991100

11011102110311041105

1106

11071108

1109111011111112

11131114

1115111611171118 1119112011211122

11231124112511261127112811291130

113111321133

11341135

11361137

113811391140

114111421143114411451146

114711481149

1150115111521153115411551156

11571158

1159 11601161

11621163

1164

1165

1166

116711681169

11701171

1172

1173117411751176117711781179

118011811182118311841185 1186

11871188

1189119011911192119311941195119611971198

119912001201120212031204 12051206

120712081209

1210

1211

1212 12131214

12151216

12171218 121912201221

1222122312241225 1226122712281229

12301231123212331234

1235123612371238

12391240

1241

124212431244

1245

12461247

1248

1249125012511252

1253125412551256

12571258125912601261

12621263

126412651266

12671268

1269

12701271 12721273127412751276 1277

127812791280

1281

1282128312841285

1286

128712881289

129012911292

129312941295129612971298

12991300130113021303

130413051306

130713081309131013111312

13131314

1315131613171318 131913201321132213231324132513261327132813291330

13311332

1333133413351336133713381339

13401341134213431344134513461347

13481349

13501351

135213531354135513561357

1358135913601361 136213631364

13651366

13671368136913701371

13721373

1374

1375

137613771378

137913801381138213831384

13851386138713881389139013911392

139313941395

13961397

1398

139914001401140214031404

1405140614071408140914101411141214131414

14151416

14171418141914201421 1422

1423

1424

14251426142714281429

143014311432 1433

14341435143614371438

14391440144114421443

144414451446144714481449

14501451145214531454

1455

1456

14571458

1459

146014611462146314641465146614671468

1469

14701471147214731474 147514761477

1478147914801481

1482148314841485 1486148714881489

1490149114921493 14941495149614971498 14991500150115021503

1504150515061507150815091510151115121513 15141515

1516

1517151815191520

1521

15221523152415251526

15271528

152915301531153215331534

153515361537

1538153915401541154215431544154515461547 154815491550155115521553

15541555

155615571558

1559156015611562

156315641565

1566156715681569 1570157115721573

15741575

15761577

15781579158015811582158315841585158615871588

158915901591 159215931594

15951596 15971598

1599160016011602

1603160416051606

160716081609

1610161116121613

1614

1615

1616161716181619

1620 16211622 16231624

162516261627

16281629

1630

163116321633

16341635

16361637163816391640 16411642164316441645

16461647164816491650

1651165216531654

16551656

16571658

165916601661166216631664166516661667

1668

16691670

167116721673

1674

16751676 16771678167916801681

1682168316841685

168616871688

16891690169116921693

1694169516961697169816991700

170117021703

17041705

17061707170817091710171117121713 1714171517161717

1718171917201721

1722

1723

17241725172617271728

172917301731

173217331734173517361737

17381739174017411742

174317441745 174617471748

1749

1750 17511752175317541755

17561757175817591760

1761176217631764176517661767

1768

1769177017711772

17731774177517761777

17781779178017811782178317841785

17861787

1788

17891790

1791179217931794

17951796179717981799

18001801

18021803

1804180518061807180818091810181118121813

18141815181618171818

18191820

18211822182318241825

1826182718281829

18301831

18321833183418351836183718381839

1840

1841184218431844

1845

184618471848

1849 1850

1851

18521853

18541855

18561857

185818591860

186118621863

18641865186618671868

18691870

1871

1872

18731874 187518761877

1878 1879

1880

1881

1882

18831884

18851886188718881889

189018911892

189318941895

1896

1897 189818991900

190119021903190419051906190719081909191019111912191319141915

1916191719181919

192019211922

19231924 19251926192719281929

19301931

1932193319341935

1936

19371938 1939

194019411942

1943194419451946194719481949195019511952195319541955

19561957195819591960196119621963

19641965196619671968196919701971197219731974

1975197619771978197919801981198219831984

1985198619871988 19891990

1991 1992199319941995199619971998

199920002001200220032004 20052006200720082009201020112012

20132014

201520162017

201820192020202120222023

20242025

2026 202720282029

203020312032

20332034

203520362037203820392040204120422043

20442045

2046

2047

2048

2049205020512052

205320542055205620572058 20592060

206120622063

2064

20652066

2067206820692070

20712072

207320742075207620772078

207920802081

2082208320842085

20862087208820892090209120922093 20942095209620972098209921002101210221032104

2105

21062107

2108

2109

2110

211121122113

21142115211621172118

2119

2120 2121212221232124212521262127212821292130

21312132213321342135213621372138213921402141

2142

2143214421452146214721482149

2150

215121522153215421552156

2157

2158

21592160216121622163216421652166

21672168216921702171217221732174217521762177217821792180

21812182

2183218421852186

218721882189

219021912192219321942195 21962197

2198219922002201

220222032204

22052206 220722082209221022112212

22132214

2215221622172218221922202221 222222232224

222522262227222822292230

2231

2232223322342235

2236

223722382239 224022412242224322442245

2246224722482249

2250225122522253 22542255225622572258 225922602261

22622263226422652266

22672268 226922702271 227222732274

227522762277

227822792280

2281228222832284

2285

228622872288 22892290 2291

229222932294

2295

2296229722982299

2300

230123022303

23042305230623072308230923102311231223132314

2315231623172318

2319232023212322

2323 232423252326

23272328 2329233023312332

233323342335

233623372338233923402341

234223432344

23452346234723482349

235023512352

23532354

23552356

2357

2358

23592360236123622363

236423652366236723682369237023712372

2373237423752376

2377237823792380238123822383

2384238523862387

2388 238923902391

23922393239423952396239723982399

2400240124022403

2404

2405240624072408240924102411241224132414241524162417 2418241924202421

24222423

2424

2425242624272428 2429243024312432243324342435 243624372438

243924402441

2442

2443244424452446244724482449

24502451

2452

24532454

24552456

2457245824592460246124622463246424652466

24672468

2469

24702471

2472

247324742475

2476247724782479248024812482

2483248424852486248724882489

249024912492 2493

2494

2495 24962497249824992500250125022503

25042505250625072508250925102511

251225132514251525162517

2518

251925202521252225232524

25252526252725282529

25302531253225332534

2535

2536

253725382539

254025412542254325442545

25462547

25482549255025512552255325542555

2556 255725582559

25602561

25622563256425652566256725682569257025712572

25732574

257525762577

257825792580258125822583

25842585258625872588258925902591

25922593

2594259525962597

25982599260026012602

26032604260526062607260826092610 26112612261326142615

261626172618261926202621

2622262326242625262626272628

2629

2630

263126322633

2634

26352636263726382639 264026412642 26432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668

266926702671

2672

267326742675

26762677 267826792680 2681

268226832684

26852686

26872688

2689269026912692269326942695269626972698269927002701270227032704 27052706

27072708270927102711

27122713271427152716 2717271827192720

2721272227232724

2725

27262727

27282729273027312732273327342735

27362737

2738 2739

274027412742

274327442745

2746

2747274827492750275127522753

2754275527562757

27582759

2760276127622763276427652766276727682769

277027712772277327742775

27762777

27782779

27802781 27822783

278427852786

2787

27882789

2790

27912792279327942795

27962797279827992800280128022803

28042805280628072808 280928102811

28122813281428152816

281728182819282028212822

2823282428252826

2827

28282829 28302831283228332834283528362837

28382839284028412842284328442845

28462847284828492850 28512852285328542855

285628572858

2859

2860 2861

28622863286428652866286728682869287028712872

287328742875

2876 2877

2878

28792880

28812882288328842885

2886288728882889

2890289128922893289428952896

289728982899

2900

290129022903

2904290529062907290829092910291129122913

29142915

29162917291829192920292129222923

29242925

29262927

29282929

29302931293229332934

2935

2936293729382939

29402941294229432944294529462947

29482949

2950

2951295229532954

2955295629572958295929602961296229632964296529662967

2968296929702971297229732974 297529762977297829792980

2981

298229832984298529862987298829892990299129922993299429952996 29972998

299930003001

300230033004

300530063007

3008300930103011

3012301330143015 30163017301830193020

3021 3022

302330243025

302630273028

30293030303130323033

303430353036

3037

3038303930403041304230433044304530463047304830493050

3051305230533054

30553056

305730583059306030613062 30633064306530663067306830693070

3071307230733074307530763077307830793080

308130823083

3084308530863087 3088308930903091309230933094 3095309630973098309931003101

3102310331043105

31063107

3108310931103111311231133114

311531163117

3118 311931203121312231233124312531263127

31283129

313031313132313331343135

31363137

31383139

314031413142314331443145314631473148

3149

31503151

3152

31533154315531563157315831593160

316131623163

3164

3165

3166

3167

3168

316931703171317231733174317531763177317831793180

31813182

31833184

3185

31863187318831893190

3191

319231933194 3195319631973198319932003201

32023203

3204

3205

3206320732083209

3210321132123213

321432153216321732183219322032213222322332243225322632273228

32293230323132323233323432353236323732383239

324032413242

324332443245

3246324732483249325032513252325332543255325632573258

32593260

3261326232633264

32653266 3267326832693270

32713272

3273

3274

3275

3276

32773278

3279328032813282 32833284

3285

3286328732883289329032913292

32933294 3295329632973298329933003301

330233033304330533063307330833093310331133123313

3314331533163317 3318331933203321

3322332333243325

332633273328332933303331

3332

333333343335

3336

3337333833393340

334133423343334433453346334733483349

3350

3351

3352

33533354 3355335633573358

3359

336033613362

33633364 336533663367

33683369337033713372

337333743375337633773378337933803381338233833384338533863387

33883389

3390339133923393

33943395

3396339733983399

340034013402 34033404

34053406

340734083409 341034113412

34133414

3415341634173418

34193420

3421 34223423

342434253426

3427

342834293430343134323433 343434353436

3437

34383439

3440

34413442344334443445

3446344734483449345034513452

345334543455345634573458 345934603461

34623463

34643465

34663467

34683469347034713472347334743475

34763477

347834793480

3481 348234833484348534863487

34883489

34903491349234933494349534963497 3498349935003501350235033504

35053506

350735083509

35103511351235133514

3515 35163517

3518 3519352035213522352335243525

3526

35273528

352935303531

353235333534353535363537

353835393540354135423543

35443545 35463547

354835493550

3551

3552

35533554355535563557

3558355935603561

3562

35633564356535663567356835693570357135723573

357435753576

357735783579

3580358135823583

3584 3585358635873588

3589359035913592

35933594359535963597

359835993600

36013602 36033604360536063607

36083609361036113612361336143615

361636173618

361936203621362236233624

36253626

3627362836293630363136323633

363436353636363736383639 36403641

364236433644364536463647

36483649365036513652

36533654

3655365636573658365936603661

36623663

3664

36653666

3667366836693670

36713672

36733674

3675

3676

3677

36783679

3680

368136823683

36843685368636873688

36893690

36913692

36933694369536963697 36983699370037013702

3703

3704

3705

3706370737083709

3710

3711371237133714

3715

371637173718 371937203721

372237233724372537263727

372837293730373137323733373437353736 3737

3738373937403741374237433744374537463747

37483749

3750 375137523753375437553756375737583759

37603761

37623763 3764

3765376637673768 37693770

377137723773

3774

377537763777377837793780

378137823783

37843785

3786

378737883789

37903791379237933794379537963797

379837993800

38013802

38033804

38053806

3807

3808380938103811

3812381338143815381638173818 381938203821 382238233824382538263827

38283829

383038313832

38333834383538363837383838393840384138423843384438453846

384738483849

3850

3851

3852

3853385438553856 385738583859386038613862

3863386438653866386738683869 3870387138723873

38743875

38763877

38783879

38803881

38823883388438853886

38873888

38893890

38913892389338943895389638973898389939003901 39023903390439053906

390739083909

3910391139123913391439153916

39173918

39193920

392139223923392439253926392739283929 3930

39313932

39333934

393539363937

3938393939403941 39423943

394439453946

3947

3948394939503951

395239533954

39553956 39573958395939603961 3962396339643965396639673968

3969

397039713972397339743975397639773978397939803981

39823983 398439853986

3987398839893990 399139923993

39943995

39963997

39983999 40004001

4002400340044005400640074008

4009401040114012401340144015 4016401740184019

4020

4021

40224023

40244025

4026

40274028402940304031403240334034

4035

403640374038 4039

404040414042

40434044

40454046

4047

40484049

4050

4051405240534054405540564057

4058405940604061 4062406340644065

4066

406740684069407040714072 407340744075

4076407740784079 40804081408240834084

40854086

4087

408840894090409140924093 409440954096409740984099410041014102

4103410441054106410741084109

411041114112411341144115411641174118

41194120412141224123412441254126

41274128

4129413041314132413341344135

41364137

4138413941404141

4142 414341444145

414641474148

41494150

415141524153

41544155

41564157415841594160416141624163416441654166416741684169

41704171417241734174417541764177

4178

417941804181

41824183418441854186 41874188

41894190 419141924193

41944195419641974198

4199

4200420142024203420442054206420742084209421042114212

42134214421542164217421842194220

4221

422242234224

42254226

4227

4228

422942304231423242334234

42354236

42374238 42394240424142424243

42444245424642474248

4249

42504251

4252

4253

4254

42554256

4257425842594260426142624263426442654266

4267426842694270

4271427242734274427542764277

427842794280

428142824283 428442854286428742884289429042914292

429342944295429642974298

42994300

430143024303

4304430543064307

43084309

4310

431143124313

431443154316

43174318

43194320

4321432243234324 4325432643274328432943304331 43324333

43344335433643374338

433943404341

43424343

43444345

434643474348

4349435043514352

4353

4354435543564357

43584359436043614362

43634364

43654366436743684369

43704371437243734374

437543764377

437843794380 4381

43824383

4384438543864387438843894390

439143924393

43944395

4396439743984399

440044014402440344044405

4406440744084409

4410441144124413441444154416

441744184419

442044214422 4423

4424442544264427

4428

44294430443144324433

4434443544364437443844394440

44414442444344444445444644474448

4449

4450 44514452445344544455

445644574458 44594460 446144624463

4464446544664467

44684469447044714472

447344744475

447644774478

44794480

44814482 44834484 4485448644874488

448944904491

44924493449444954496 44974498449945004501450245034504

450545064507

4508450945104511

45124513 451445154516451745184519

4520452145224523452445254526

4527

452845294530

45314532

4533453445354536 4537453845394540454145424543

45444545

4546

45474548454945504551455245534554

455545564557

4558455945604561

45624563456445654566

45674568456945704571457245734574

457545764577

45784579458045814582

458345844585

45864587458845894590

459145924593

45944595459645974598 45994600

4601 4602460346044605

46064607460846094610

461146124613

4614

4615 46164617

4618

461946204621 46224623462446254626462746284629463046314632463346344635

4636

463746384639464046414642464346444645

4646464746484649

46504651

4652465346544655

465646574658

4659

46604661

46624663

466446654666 466746684669

467046714672467346744675

4676467746784679

468046814682

4683

46844685468646874688

46894690

4691469246934694

4695469646974698 46994700

47014702

4703470447054706

4707 470847094710471147124713

47144715471647174718471947204721472247234724472547264727472847294730

4731

4732

473347344735

47364737

4738

4739 47404741

4742

474347444745474647474748

4749

475047514752

47534754 47554756

47574758

475947604761

4762

4763

47644765 476647674768

47694770

47714772 47734774

477547764777

47784779

4780 478147824783

4784

478547864787

478847894790

4791479247934794

479547964797479847994800

480148024803

4804 48054806

4807

4808

48094810481148124813 4814

4815

4816481748184819

482048214822

48234824482548264827 4828482948304831

48324833

4834483548364837483848394840484148424843

4844484548464847484848494850485148524853485448554856

48574858485948604861486248634864

486548664867

48684869487048714872

487348744875

48764877 487848794880

4881

4882

4883

4884488548864887

488848894890

489148924893489448954896489748984899

49004901490249034904

49054906490749084909

49104911 4912

4913

491449154916491749184919492049214922

49234924 49254926

4927492849294930493149324933493449354936

4937

493849394940 4941

494249434944

494549464947

4948

4949495049514952 49534954

4955 49564957

4958

495949604961

4962496349644965496649674968

4969497049714972

4973497449754976

4977497849794980

49814982498349844985

4986498749884989499049914992 499349944995499649974998

49995000

0.0

05

.01

.01

5L

eve

rag

e

0 .002 .004 .006 .008Normalized residual squared

04/18/23 H.S. 23

Leverage versus residuals2

lvr2plot, mlabel(id)

high influ

ence

Page 24: Stata 3, Linear Regression v3

370

4962

-.8

-.6

-.4

-.2

0.2

Dfb

eta

gest

0 1000 2000 3000 4000 5000id

beta(gest)= 17.9

Delta-beta for gestational age

04/18/23 H.S. 24

dfbeta(gest)scatter _dfbeta_1 id

OBS, variable specific

If obs nr 370 is removed, beta will change from 17.9 to 18.6

Page 25: Stata 3, Linear Regression v3

04/18/23 H.S. 25

Removing outlier

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

Page 26: Stata 3, Linear Regression v3

Removing outlier

04/18/23 H.S. 26

Full model N=5000 Outlier removed N=4999

One outlier affected several estimates

Final model

coeff 95% conf. Int.Birth weight at ref 3426 (3385 , 3467)Gestational age

per day 17.9 (16 , 20)Education

Low 0Medium 71.5 (25 , 118)High 99.1 (51 , 148)

SexBoy 0Girl -154.3 (-187 , -121)

coeff 95% conf. Int.Birth weight at ref 3433 (3391 , 3474)Gestational age

per day 18.5 (17 , 20)Education

Low 0Medium 64.2 (18 , 110)High 88.6 (40 , 137)

SexBoy 0Girl -152.7 (-185 , -120)

Page 27: Stata 3, Linear Regression v3

Help

• Linear regression– help regress

• syntax and options

– help regress postestimation• dfbeta• estat hettest• lvr2plot• predict• margins

04/18/23 H.S. 27

Page 28: Stata 3, Linear Regression v3

NON-LINEAR EFFECTSbw2

04/18/23 H.S. 28

Page 29: Stata 3, Linear Regression v3

bw2: Non-linear effects

04/18/23 H.S. 29

1000

2000

3000

4000

5000

6000

Birt

h w

eigh

t (gr

)

240 260 280 300 320Gestational age

Handle:add

polynomialor

spline

Page 30: Stata 3, Linear Regression v3

Non-linear effects: polynomial

04/18/23 H.S. 30

regress bw2 c.gest##c.gest i.educ sex 2. order polynomial in gest

margins, at(gest=(250(10)310)) predicted bw2 by gestmarginsplot plot

25

00

30

00

35

00

40

00

Lin

ea

r P

red

ictio

n

250 260 270 280 290 300 310Gestational age

Predictive Margins with 95% CIs

Page 31: Stata 3, Linear Regression v3

Non-linear effects: spline• Qubic spline

• Plot

• Linear spline

04/18/23 H.S. 31

mkspline g=gest, cubic nknots(4) make spline with 4 knotsregress bw2 g1 g2 g3 i.educ sex regression with spline

gen igest=5*round(gest/5) 5-year integer values of gest margins, over(igest) predicted bw by gestmarginsplot

mkspline g1 280 g2=gest make linear spline with knot at 280regress bw2 g1 g2 i.educ sex regression with spline

250

03

000

350

04

000

Lin

ear

Pre

dict

ion

250 260 270 280 290 300 310igest

Predictive Margins with 95% CIs

Page 32: Stata 3, Linear Regression v3

INTERACTIONbw3

04/18/23 H.S. 32

Page 33: Stata 3, Linear Regression v3

Interaction definitions

• Interaction: combined effect of two variables

• Scale– Linear models additive

• y=b0+b1x1+b2x2 both x1 and x2 = b1+b2

– Logistic, Poisson, Cox multiplicative

• Interaction– deviation from additivity (multiplicativity)

– effect of x1 depends on x2

04/18/23 H.S. 33

Page 34: Stata 3, Linear Regression v3

bw3: Interaction (only linear effects)

• Add interaction terms

• Show results

04/18/23 H.S. 34

regress bw3 c.gest##i.sex i.educ gest-sex interaction

margins, dydx(gest) at(sex=0) effect of gest for boysmargins, dydx(gest) at(sex=1) effect of gest for girls

Page 35: Stata 3, Linear Regression v3

Summing up 1

• Build model– regress bw gest crude model– est store m1 store– regress bw gest i.educ sex full model– est store m2– est table m1 m2 compare coefficients

• Interaction– regress bw3 c.gest##i.sex i.educ test interaction– margins, dydx(gest) at(sex=0) gest for boys

• Assumptions– predict res, residuals residuals– predict pred, xb predicted– scatter res pred plot

04/18/23 H.S. 35

Page 36: Stata 3, Linear Regression v3

Summing up 2

• Non-linearity (linear spline)– mkspline g1 280 g2=gest spline with knot at

280– regress bw2 g1 g2 i.educ sex regression with spline

• Robustness– dfbeta(gest) delta-beta– scatter _dfbeta_1 id plot versus id

04/18/23 H.S. 36