chi11 rising

25
Introduction New Graphical Tools Conclusion Graphics tricks for models Bill Rising StataCorp LP 2011 Stata Conference Chicago, IL July 15, 2011 Bill Rising Graphics tricks for models

Upload: arthur42

Post on 10-Apr-2016

224 views

Category:

Documents


2 download

DESCRIPTION

Chi11 Rising

TRANSCRIPT

Page 1: Chi11 Rising

IntroductionNew Graphical Tools

Conclusion

Graphics tricks for models

Bill Rising

StataCorp LP

2011 Stata ConferenceChicago, ILJuly 15, 2011

Bill Rising Graphics tricks for models

Page 2: Chi11 Rising

IntroductionNew Graphical Tools

ConclusionGoals

Goals

Show two new Stata graphics toolsPlotting marginal effects and predictive margins usingmarginsplotMaking contour plots via twoway contour and twowaycontourline

Bill Rising Graphics tricks for models

Page 3: Chi11 Rising

IntroductionNew Graphical Tools

ConclusionGoals

Getting Started

This will be an interactive demonstrationThis will help show some of the new interface as well as someother new tools

Start by opening up the nhanes2 dataset. webuse nhanes2

Good for continuation from yesterday

These are survey data. svyset

We will need to use the svy: prefix for estimation

Bill Rising Graphics tricks for models

Page 4: Chi11 Rising

IntroductionNew Graphical Tools

ConclusionVisualizing MarginsContour Plots

Pros and Cons of margins

The margins command is great!Can easily compute averaged predicted valuesCan easily compute average marginal effectsBoth of these make tables of values

Curse the margins command!The tabular output can be difficult to read

Bill Rising Graphics tricks for models

Page 5: Chi11 Rising

IntroductionNew Graphical Tools

ConclusionVisualizing MarginsContour Plots

A Simple Regression with a Quadratic

Here is a simple model for BMI as a function of age and sex. svy: regress bmi c.age##c.age i.sex

Can see that the parabola is concave down, and that womenhave smaller BMIs then men

Bill Rising Graphics tricks for models

Page 6: Chi11 Rising

IntroductionNew Graphical Tools

ConclusionVisualizing MarginsContour Plots

Taking a Look at Predictive Margins

We can take a look at the predictive margins at several ageswithin each sex. margins, at(age==(25(10)75)) over(sex)

Note: Because of using svy, we should really be specifyingvce(unconditional) for all these examples; this is being left offto keep the commands short

It looks like the differences between sexes are constant (asexpected)As one would expect from the concave-down parabola the BMI’sincrease and then decrease

Bill Rising Graphics tricks for models

Page 7: Chi11 Rising

IntroductionNew Graphical Tools

ConclusionVisualizing MarginsContour Plots

Visualizing the Predictive Margins

We can get a much better look at the margins by using themarginsplot commandHere it is in its simplest form. marginsplot

23

24

25

26

27

Lin

ear

Pre

dic

tion

25 35 45 55 65 75age in years

Male Female

Predictive Margins with 95% CIs

Bill Rising Graphics tricks for models

Page 8: Chi11 Rising

IntroductionNew Graphical Tools

ConclusionVisualizing MarginsContour Plots

Default Behavior

The default behavior is toDraw a connected-line plotDraw pointwise confidence intervals at each point

Stata is bright enough to use the at() variable for the x-axis andto overlay the two curves

Bill Rising Graphics tricks for models

Page 9: Chi11 Rising

IntroductionNew Graphical Tools

ConclusionVisualizing MarginsContour Plots

Switching Axes

We can change this default behavior, of courseHere are the same data in a less-useful form (though it shows theconstant offset for the females). marginsplot, x(sex)

23

24

25

26

27

Lin

ear

Pre

dic

tion

Male Female1=male, 2=female

age=25 age=35

age=45 age=55

age=65 age=75

Predictive Margins with 95% CIs

Bill Rising Graphics tricks for models

Page 10: Chi11 Rising

IntroductionNew Graphical Tools

ConclusionVisualizing MarginsContour Plots

Making a Finer Grid

We can also make a finer grid for the graph by giving more agepoints

We’ll also turn off the CIs here. quietly margins, at(age==(25(5)75)) over(sex). marginsplot, noci

23

24

25

26

27

Lin

ear

Pre

dic

tion

25 30 35 40 45 50 55 60 65 70 75age in years

Male Female

Predictive Margins

Bill Rising Graphics tricks for models

Page 11: Chi11 Rising

IntroductionNew Graphical Tools

ConclusionVisualizing MarginsContour Plots

Average Marginal Effects

We can also look at average marginal effects. quietly margins, at(age==(25(5)75)) dydx(*). marginsplot, noci

−.4

−.2

0.2

Eff

ects

on

Lin

ea

r P

red

ictio

n

25 30 35 40 45 50 55 60 65 70 75age in years

age 2.sex

Conditional Marginal Effects

Bill Rising Graphics tricks for models

Page 12: Chi11 Rising

IntroductionNew Graphical Tools

ConclusionVisualizing MarginsContour Plots

Looking at a Logit

Working with something which is non-linear in the natural (notthe model) measure is a bit more interestingHere is a simple logistic regression. svy: logistic diabetes age i.(race sex)

Bill Rising Graphics tricks for models

Page 13: Chi11 Rising

IntroductionNew Graphical Tools

ConclusionVisualizing MarginsContour Plots

The Predictive Margins are More Interesting

Now the predictive margins are more interesting. quietly margins, at(age==(25(5)75)) over(race sex). marginsplot, noci

0.1

.2.3

Pr(

Dia

bete

s)

25 30 35 40 45 50 55 60 65 70 75age in years

White, Male White, Female

Black, Male Black, Female

Other, Male Other, Female

Predictive Margins

Bill Rising Graphics tricks for models

Page 14: Chi11 Rising

IntroductionNew Graphical Tools

ConclusionVisualizing MarginsContour Plots

Now for Something Complicated

We’ll now fit a model with (too) many interactions. svy: logit diabetes c.age##c.age##race##sex

Looking at the interaction terms is nearly worthlessThis is probably getting close to overfitting

Bill Rising Graphics tricks for models

Page 15: Chi11 Rising

IntroductionNew Graphical Tools

ConclusionVisualizing MarginsContour Plots

Predictive Margins, One Last Time

Here are the same set of margins applied to this different model. quietly margins, at(age=(20(5)75)) over(race sex). marginsplot, noci

Bill Rising Graphics tricks for models

Page 16: Chi11 Rising

IntroductionNew Graphical Tools

ConclusionVisualizing MarginsContour Plots

Contour Plots

Stata 12 now has both filled contour plots and contour line plotsThese are both twoway plots

twoway contour for filled contour plotstwoway contourline for contour line plots

They are in the twoway dialog

Bill Rising Graphics tricks for models

Page 17: Chi11 Rising

IntroductionNew Graphical Tools

ConclusionVisualizing MarginsContour Plots

A Simple Example Dataset

Here is a dataset meant to be artificial mountains. use mtns

Take a look at a few observations. list in 1/10

Bill Rising Graphics tricks for models

Page 18: Chi11 Rising

IntroductionNew Graphical Tools

ConclusionVisualizing MarginsContour Plots

Making a Filled Contour

Here is the default filled contour. twoway contour z y x

01

23

4n

ort

hin

g

0 1 2 3 4easting

0

.5

1

1.5

2

2.5

he

igh

t in

th

ou

sa

nd

s o

f m

ete

rs

Note: If this displays with line artifacts, turn off anti-aliasing inyour pdf viewer—this is a known limitation of pdfs

Bill Rising Graphics tricks for models

Page 19: Chi11 Rising

IntroductionNew Graphical Tools

ConclusionVisualizing MarginsContour Plots

Making the Contour Prettier

Adding many levels smooths the color gradations. twoway contour z y x, levels(40)

This makes the legend a bit absurd, thoughWe should shut off the legend and change the aspect ratio

Bill Rising Graphics tricks for models

Page 20: Chi11 Rising

IntroductionNew Graphical Tools

ConclusionVisualizing MarginsContour Plots

A Pretty Contour Plot

This looks quite good. twoway contour z y x, levels(40) clegend(off) xsize(5) ysize(5)

01

23

4nort

hin

g

0 1 2 3 4easting

Bill Rising Graphics tricks for models

Page 21: Chi11 Rising

IntroductionNew Graphical Tools

ConclusionVisualizing MarginsContour Plots

A Contour Line Plot

Contour line plots outline elevations instead of filling them inThey work best with color lines on. twoway contourline z y x, levels(40) colorlines plegend(off) xsize(5) ysize(5)

01

23

4n

ort

hin

g

0 1 2 3 4easting

Bill Rising Graphics tricks for models

Page 22: Chi11 Rising

IntroductionNew Graphical Tools

ConclusionVisualizing MarginsContour Plots

What If There is No Grid?

The artificial mountain dataset was defined on a gridBoth twoway contour and twoway contourline will useinterpolation to fill the rest of the plot region if the data are noton a gridLet’s look at this Excel file about a sandstone stratum under OhioFirst we’ll bring it into Stata. import excel using sandstone, firstrow

Looking at it, there are gaps in the grid. scatter northing easting

Bill Rising Graphics tricks for models

Page 23: Chi11 Rising

IntroductionNew Graphical Tools

ConclusionVisualizing MarginsContour Plots

We Can Still Make a Contour Plot

We can still make a contour plot from this. twoway contour depth northing easting

60000

70000

80000

90000

nort

hin

g

30000 35000 40000 45000 50000easting

7,600

7,700

7,800

7,900

8,000

8,100

depth

Bill Rising Graphics tricks for models

Page 24: Chi11 Rising

IntroductionNew Graphical Tools

ConclusionVisualizing MarginsContour Plots

Taking A Look at Help

We should take a look at the help for contour plotsThe help files have been spruced up, so that we can skip to theRemarks right away!

Bill Rising Graphics tricks for models

Page 25: Chi11 Rising

IntroductionNew Graphical Tools

ConclusionConclusion

Just For Fun

Believe it or not, it is possible to make a contour plot ofpredictive margins. do margcon2

Here is the picture20

40

60

80

age in y

ears

10 20 30 40 50 60Body Mass Index (BMI)

0

.05

.1

.15

.2

.25

.3

.35

.4

.45

.5

Pr(

dia

bete

s),

pre

dic

t()

Enjoy!Bill Rising Graphics tricks for models