{smcl} {* 20aug2004}{...} {hline} {center:{hi:Hand-draw response surface}} {hline} {p 4 4 4}{it} Here is a more elaborate example. Suppose we want to map out the nonlinear relationship between two covariates and an outcome, say the probability from a logistic estimation, by showing the probability ranges in different colors and the covariates on the X- and Y-axes. Let's use the familiar auto dataset to do just that.{sf} {p 0 2 0}o Create quadratic terms {p 4 4 4}{it} We need some nonlinearity to make this interesting, so let's create quadratic terms for weight and mileage (we will also scale the weight variable). (Click the following 6 commands.) {sf} {p 0 2 0}. {stata drop _all}{p_end} {p 0 2 0}. {stata set mem 60m}{p_end} {p 0 2 0}. {stata sysuse auto}{p_end} {p 0 2 0}. {stata gen wt = weight / 1000} {p 0 2 0}. {stata gen wt2 = wt^2}{p_end} {p 0 2 0}. {stata gen mpg2 = mpg^2}{p_end} {p 0 2 0}o Estimate model {p 4 4 4}{it} Let's estimate a logistic model with foreign as the dependent variable using our linear and quadratic terms as covariates. {sf} {p 0 2 0}. {stata logistic foreign mpg mpg2 wt wt2}{p_end} {p 4 4 4}{it} Now, it is time to produce our plot. Start by producing a lattice of values for our two covariates. We will first find the range of each variable then use the {cmd:range} and {cmd:fillin} commands to produce an evenly spaced square lattice of points covering the range of the two variables {sf} {p 0 2 0}o Find range of data {p 0 2 0}. {stata sum wt mpg} {p 0 2 0}o Create lattice over range of 2 variables {p 0 2 0}. {stata drop _all}{p_end} {p 0 2 0}. {stata range wt 1.76 4.84 80}{p_end} {p 0 2 0}. {stata range mpg 12 41 80}{p_end} {p 0 2 0}. {stata fillin wt mpg}{p_end} {p 4 4 4}{it} We need to recompute our quadratic terms for all of the points, then we can predict the probability of being foreign at each of the points. {sf} {p 0 2 0}o Recreate quadratic terms {p 0 2 0}. {stata gen wt2 = wt^2}{p_end} {p 0 2 0}. {stata gen mpg2 = mpg^2}{p_end} {p 0 2 0}o Predict probability foreign for all lattice values {p 0 2 0}. {stata predict prob_foreign} {p 4 4 4}{it} Let's cut these predicted probabilities into 4 ranges, each of which we will plot in a different color {sf} {p 0 2 0}o Cut predicted probabilities into quartiles {p 0 2 0}. {stata xtile qt = prob_foreign , nq(4)} {p 4 4 4}{it} We will keep only the variables necessary to draw the graph {sf} {p 0 2 0}. {stata keep wt mpg qt} {p 0 2 0}o Create corners of boxes around lattice points to fill the space {p 4 4 4}{it} Despite requiring a few commands, we are just creating the points for little squares that are centered at our lattice points. {stata do surf_pts:Click here} to run the next 14 commands in order. Or, if you prefer a challenge, try clicking each individually in the correct order.{sf} {p 0 2 0}. {stata local dx = (1/2) * (4.84 - 1.76) / 79}{p_end} {p 0 2 0}. {stata local dy = (1/2) * (41 - 12) / 79} {p 0 2 0}. {stata gen id = _n} {p 0 2 0}. {stata expand 6}{p_end} {p 0 2 0}. {stata sort id} . {stata "by id: replace wt = wt - `dx' if _n==1"} . {stata "by id: replace mpg = mpg - `dy' if _n<=2"} . {stata "by id: replace wt = wt + `dx' if _n==2 | _n==3"} . {stata "by id: replace mpg = mpg + `dy' if _n==3 | _n==4"} . {stata "by id: replace wt = wt - `dx' if _n==4"} . {stata "by id: replace wt = wt - `dx' if _n==5"} . {stata "by id: replace mpg = mpg - `dy' if _n==5"} . {stata "by id: replace wt = . if _n==6"} . {stata "by id: replace mpg = . if _n==6"} {p 0 2 0}o Graph the boxes using a different color for each quartile {stata do surface01:. twoway area mpg wt if qt==1 , nodropbase bc(navy) ||} {stata do surface01:area mpg wt if qt==2 , nodropbase bc(green) ||} {stata do surface01:area mpg wt if qt==3 , nodropbase bc(red) ||} {stata do surface01:area mpg wt if qt==4 , nodropbase bc(yellow)} {stata do surface01:scheme(intensity)} {stata do surface01:ylabel(20 30 40) xlabel(2 3 4)} {stata do surface01:plotregion(margin(zero))} {stata do surface01:legend(subtitle(Predicted probability foreign)} {stata do surface01:order(1 "(0,.25]" 2 "(.25,.5]"} {stata do surface01:3 "(.5,.75]" 4 "(.75,1)" )} {stata do surface01:)} {p 4 4 4}{it} We can give the two dimensions equal importance by adding the {cmd:aspect(1)} option.{sf} {stata do surface0a:. twoway area mpg wt if qt==1 , nodropbase bc(navy) ||} {stata do surface0a:area mpg wt if qt==2 , nodropbase bc(green) ||} {stata do surface0a:area mpg wt if qt==3 , nodropbase bc(red) ||} {stata do surface0a:area mpg wt if qt==4 , nodropbase bc(yellow)} {stata do surface0a:scheme(intensity)} {stata do surface0a:ylabel(20 30 40) xlabel(2 3 4)} {stata do surface0a:plotregion(margin(zero))} {stata do surface0a:legend(subtitle(Predicted probability foreign)} {stata do surface0a:order(1 "( 0,.25]" 2 "(.25,.5]"} {stata do surface0a:3 "(.5,.75]" 4 "(.75,1)" )} {stata do surface0a:)} {stata do surface0a:, aspect(1)} {hline} >> {view scheme-intensity.scheme:Our intensity scheme} {hline} {center:{view statsby.smcl:<<} {view tindex.smcl:index} {view cover3.smcl:>>}}