32
bipolate: A Stata command for bivariate interpolation with particular application to 3D graphing Joseph Canner, MHS Xuan Hui, MD ScM Eric Schneider, PhD Johns Hopkins University Stata Conference Boston, MA August 1, 2014

bipolate : A Stata command for bivariate interpolation with particular application to 3D graphing

Embed Size (px)

DESCRIPTION

bipolate : A Stata command for bivariate interpolation with particular application to 3D graphing. Joseph Canner, MHS Xuan Hui, MD ScM Eric Schneider, PhD Johns Hopkins University Stata Conference Boston, MA August 1, 2014. Background. Educational outcome study Continuous outcome - PowerPoint PPT Presentation

Citation preview

Page 1: bipolate : A Stata command for bivariate interpolation with particular application to 3D graphing

bipolate: A Stata command for bivariate interpolation with particular

application to 3D graphing Joseph Canner, MHS

Xuan Hui, MD ScM

Eric Schneider, PhD

Johns Hopkins University

Stata Conference

Boston, MA

August 1, 2014

Page 2: bipolate : A Stata command for bivariate interpolation with particular application to 3D graphing

Background

• Educational outcome study– Continuous outcome– Two categorical (1-5) predictors

• Panel data – 8 time periods– 285 observations per time period

• Researcher desired 3D plot of outcome versus two predictors

Page 3: bipolate : A Stata command for bivariate interpolation with particular application to 3D graphing

Solution #1: contour2

34

5E

duN

EW

1 2 3 4CommunicationNEW

40

50

60

70

80

90

100

110

120

130

140

150

160

CO

MP

SS

Page 4: bipolate : A Stata command for bivariate interpolation with particular application to 3D graphing

Solution #2: surface*

. surface CommunicationNEW EduNEW COMPSS, xlabel(1/5) ylabel(1/5)

* by Adrian Mander, available from SSC

Page 5: bipolate : A Stata command for bivariate interpolation with particular application to 3D graphing

Result

CommunicationNEW1

EduNEW

2

CO

MP

SS

3 4 51

2

3

4

5

40.00

94.00

148.00

Page 6: bipolate : A Stata command for bivariate interpolation with particular application to 3D graphing

collapse first

. collapse (mean) mCOMPSS=COMPSS, by(EduNEW CommunicationNEW). surface CommunicationNEW EduNEW mCOMPSS, xlabel(1/5) ylabel(1/5)

Page 7: bipolate : A Stata command for bivariate interpolation with particular application to 3D graphing

Result

CommunicationNEW1

EduNEW

2

(me

an)

CO

MP

SS

3 4 51

2

3

4

5

44.00

78.41

112.82

Page 8: bipolate : A Stata command for bivariate interpolation with particular application to 3D graphing

SAS Solution

proc g3grid data=a out=b ;grid EduNEW*CommNEW=mCOMPSS / axis1=1 to 5 by 0.1 axis2=1 to 5 by 0.1;run;

Page 9: bipolate : A Stata command for bivariate interpolation with particular application to 3D graphing

SAS Solution (cont’d)

proc g3d data=b;plot EduNEW*CommNEW=mCOMPSS / xticknum=5 yticknum=5 grid;run;

Page 10: bipolate : A Stata command for bivariate interpolation with particular application to 3D graphing

SAS Result

Page 11: bipolate : A Stata command for bivariate interpolation with particular application to 3D graphing

Stata Conference 2013

Wishes and grumbles session: no plans to implement 3D graphing

Page 12: bipolate : A Stata command for bivariate interpolation with particular application to 3D graphing

SAS PROC G3GRID

• Interpolation options:– <default>: biquintic polynomial

• PARTIAL: use splines for derivatives• NEAR=n: number of nearest neighbors

(default=3)

– SPLINE: bivariate spline• SMOOTH=numlist: smoothed spline

– JOIN: linear interpolation

Page 13: bipolate : A Stata command for bivariate interpolation with particular application to 3D graphing

Bivariate interpolation

• SAS G3GRID default• Akima, Hiroshi (1978), "A Method of Bivariate

Interpolation and Smooth Surface Fitting for Irregularly Distributed Data Points," ACM Transaction on Mathematical Software, 4, 148-159.

• Fortran 77 originally in the NCAR library; Fortran 77 and Fortran 90 versions freely available on the web

Page 14: bipolate : A Stata command for bivariate interpolation with particular application to 3D graphing

History

“The original version of BIVAR was written by Hiroshi Akima in August 1975 and rewritten by him in late 1976. It was incorporated into NCAR's public software libraries in January 1977. In August 1984 a new version of BIVAR, incorporating changes described in the Rocky Mountain Journal of Mathematics article cited below, was obtained from Dr. Akima by Michael Pernice of NCAR's Scientific Computing Division, who evaluated it and made it available in February, 1985.”

Ref: Hiroshi Akima, On Estimating Partial Derivatives for Bivariate Interpolation of Scattered Data, Rocky Mountain Journal of Mathematics, Volume 14, Number 1, Winter 1984.

Page 15: bipolate : A Stata command for bivariate interpolation with particular application to 3D graphing

Algorithm summary

• XY plane divided into triangular cells• Bivariate quintic polynomial in X and Y

fitted to each triangular cell• Coefficients are determined by

continuity requirements and by estimates of partial derivatives at the vertices and along triangle edges

Page 16: bipolate : A Stata command for bivariate interpolation with particular application to 3D graphing

Algorithm features

• Invariant to certain transformations:– Rotation of XY coordinate system– Linear scale transformation of the Z axis– Tilting of the XY plane

Page 17: bipolate : A Stata command for bivariate interpolation with particular application to 3D graphing

Algorithm features (cont’d)

• Interpolating function and first-order partial derivatives are continuous

• Local method: change in data in one area does not effect the interpolating function in another area

• Gives exact results when all points lie in a plane

Page 18: bipolate : A Stata command for bivariate interpolation with particular application to 3D graphing

bipolate command

Syntax: bipolate xvar yvar zvar [if] [in] [using] [, options]

Page 19: bipolate : A Stata command for bivariate interpolation with particular application to 3D graphing

bipolate options

• method: interpolation or filling• xgrid, ygrid: specify x-axis and y-axis

values to use for interpolation• fillusing: specify data set to use for filling• collapse: how to handle multiple values of

z at a given x and y • saving: save the resulting data to set to

disk

Page 20: bipolate : A Stata command for bivariate interpolation with particular application to 3D graphing

Use of bipolate

. bipolate CommunicationNEW EduNEW COMPSS, xgrid(1(0.1)5) ygrid(1(0.1)5) method(interp) saving(test_bip). use test_bip, clear. surface EduNEW CommunicationNEW COMPSS_mod

Page 21: bipolate : A Stata command for bivariate interpolation with particular application to 3D graphing

Result

EduNEW1.00

CommunicationNEW

3.00

me

an o

f CO

MP

SS

5.001.00

3.00

5.00

32.27

68.45

104.64

Page 22: bipolate : A Stata command for bivariate interpolation with particular application to 3D graphing

SAS Result

Page 23: bipolate : A Stata command for bivariate interpolation with particular application to 3D graphing

Remaining puzzles

• Why are there small differences between interpolated values?– SAS: “This default method is a modification

of that described by Akima (1978)”• Re-orienting axis

Page 24: bipolate : A Stata command for bivariate interpolation with particular application to 3D graphing

surface …, … xscale(reverse)

EduNEW1

CommunicationNEW

2

me

an o

f CO

MP

SS

3451

2

3

4

5

32.27

68.45

104.64

Page 25: bipolate : A Stata command for bivariate interpolation with particular application to 3D graphing

bipolate+contour1

23

45

Ed

uNE

W

1 2 3 4 5CommunicationNEW

40

50

60

70

80

90

100

110

120

130

140

150

160

me

an o

f CO

MP

SS

Page 26: bipolate : A Stata command for bivariate interpolation with particular application to 3D graphing

Future plans

• Make available on SSC within a few weeks

• Test other data sets• Testing and debugging by Stata

community

Page 27: bipolate : A Stata command for bivariate interpolation with particular application to 3D graphing

Cobar Mine Data

t1-16.00

t2

34.00

z

84.00-72.00

-32.50

7.00

11.00

18.50

26.00

Page 28: bipolate : A Stata command for bivariate interpolation with particular application to 3D graphing

Cobar Mine Datamethod(interp)

t1-16.00

t2

34.00

z_no

ne

84.00-72.00

-32.50

7.00

-49.61

-11.22

27.17

Page 29: bipolate : A Stata command for bivariate interpolation with particular application to 3D graphing

Cobar Mine Datamethod(fill)

t1-16.00

t2

34.00

(me

an)

z

84.00-72.00

-32.50

7.00

0.68

13.92

27.17

Page 30: bipolate : A Stata command for bivariate interpolation with particular application to 3D graphing

Cobar Mine Datatwoway contour

-80

-60

-40

-20

0t2

-20 0 20 40 60 80t1

1112

13

14

1516

17

18

19

2021

22

23

2425

26

z

Page 31: bipolate : A Stata command for bivariate interpolation with particular application to 3D graphing

Cobar Mine Databipolate+twoway contour

-80

-60

-40

-20

0t2

-20 0 20 40 60 80t1

012345678910111213141516171819202122232425262728

(me

an

) z

Page 32: bipolate : A Stata command for bivariate interpolation with particular application to 3D graphing

Possible Future Enhancements

• Implement partial and near options• Implement scale/noscale option• Implement spline and smooth spline

interpolation• See if Mata has functions that can

reproduce the algorithm more compactly