SAS FAQ
How can I visualize interactions of continuous variables in multiple regression?

It is difficult to picture what it means for there to be an interaction of continuous variables in multiple regression. These examples show how you can use SAS to produce three dimensional spin plots to help visualize such interactions.  We use 2 strategies to help these animated spin plots help you see the interactions, 1) by allowing you to view the regression plane from a variety of views to picture the regression surface, and 2) by varying certain terms in the regression equation and showing an animated graph illustrating what happens when that term in the regression model varies.  For more background information on understanding such regression analyses, including more technical detail on calculations and interpretations, we recommend Multiple Regression: Testing and Interpreting Interactions and Interaction Effects in Multiple Regression, both available from our Statistics Books for Loan.

First, you need to download the macros stored in spplot.zip. We will assume that you unzip them and store them in c:\spinplots .

Second, we include all of the macros so we can use them.

%include "c:\spinplots\sp_plot.sas";
%include "c:\spinplots\sp_plota.sas";
%include "c:\spinplots\fixlen.sas";
%include "c:\spinplots\sp_plotbx1.sas";
%include "c:\spinplots\sp_plotbx1x2.sas";
%include "c:\spinplots\sp_plotbx1x1x2.sas";

We then call the %sp_plota macro to make a animated spin plot where the coefficient for x1, called bx1, is 0, and bx2 is 0, and the interaction of these is 0.  We store the resulting file in a file called c:\spinplots\reg_int_cont1.gif but you can call it anything you like.  You can then bring this up in your web browser to view it (it is not viewable within SAS).  As you see in the plot, this is quite a boring plot since it is a completely flat plane.

%sp_plota(outfile="c:\spinplots\reg_int_cont1.gif", bx1=0,bx2=0,bx1x2=0);

Now let's show what happens when you vary bx1, the coefficient for x1.  We use the %sp_plotbx1 macro (which varies bx1) and indicate that we wish to vary bx1 from 0 to 3 incrementing by 0.05.  We set the angle for viewing this at 190 and we make the graph go very quickly by setting the delay to only 10.  As you can see, when bx1 increases, the plane tilts such that increasing values of x1 lead to increased values of y.  

%sp_plotbx1(outfile="c:\spinplots\reg_int_cont2.gif",
            bx1=0,bx2=0,bx1_lo=0,bx1_hi=3,bx1_by=.05, slen=5, 
            angle=190,gopt= delay=10);
          

Now, let's see what happens when we introduce an interaction between x1 and x2 (i.e., bx12).  We use the macro named %sp_plotbx1x2 (which varies the bx1x2 term) to show what happens when you increase the value of bx12 from 0 to .5 by .01.  As you see, as bx1x2 increases, the regression plane goes from being flat to being curved, and it curves such that y increases in the corners where x1 and x2 are both high or both low, and y decreases in the other 2 corners.

* when b1 b2=0, add bx1x2 from 0 to .5, fast ;
%sp_plotbx1x2(outfile="c:\spinplots\reg_int_cont4.gif",
              bx1=0,bx2=0, bx1x2_lo=0, bx1x2_hi=.5, bx1x2_by=.01, 
              slen=5, angle=190,gopt= delay=10);

Let's show the same graph, but slow it down.  Sometimes the fast graph (like above) helps to understand the plot, but sometimes a slower plot (like the one below) is more helpful.  For example, in the plot below you can see the values for the coefficient bx1x2 changing and see how that corresponds with the shape of the graph.


%sp_plotbx1x2(outfile="c:\spinplots\reg_int_cont5.gif",
              bx1=0, bx2=0, bx1x2_lo=0, bx1x2_hi=.5, bx1x2_by=.05, 
              slen=5, angle=190,gopt= delay=50);

The bx1x2 term could be negative instead of positive.  The graph below shows a fast graph varying bx12 from -.5 to 0. As you see, it looks much like the graph above, except that the y values flare down when x1 and x2 are both high, or both low.  

%%sp_plotbx1x2(outfile="c:\spinplots\reg_int_cont6.gif",
               bx1=0, bx2=0, bx1x2_lo=-.5, bx1x2_hi=0, bx1x2_by=.01, 
               slen=5,angle=190,gopt= delay=10);

We now show the same graph more slowly.

%sp_plotbx1x2(outfile="c:\spinplots\reg_int_cont7.gif",
              bx1=0,bx2=0, bx1x2_lo=-.5, bx1x2_hi=0, bx1x2_by=.05, 
              slen=5, angle=190,gopt= delay=50);
              

The graph below varies bx1x2 from -.5 to .5 so you can see the full spectrum of the changes as you go from having a negative interaction, to no interaction, to having a positive interaction.  

%sp_plotbx1x2(outfile="c:\spinplots\reg_int_cont8.gif",
              bx1=0, bx2=0, bx1x2_lo=-.5, bx1x2_hi=.5, bx1x2_by=.01, 
              slen=5, angle=190,gopt= delay=10); 

And we show the above graph more slowly below.

%sp_plotbx1x2(outfile="c:\spinplots\reg_int_cont9.gif",
              bx1=0, bx2=0, bx1x2_lo=-.5, bx1x2_hi=.5, bx1x2_by=.05, 
              slen=5,angle=190,gopt= delay=50);

Below we show a spin plot for the graph where bx1 is 0, bx2 is 0, and bx1x2 is .5This allows you to see the graph from all angles.

%sp_plota(outfile="c:\spinplots\reg_int_cont10.gif", bx1=0,bx2=0,bx1x2=.5, title=" ");

We showed what happens when you vary bx1 in the regression but that was when there was no interaction.  Let's vary bx1 from 0 to 2 and you can see that, although the plane is twisted, the effect of varying bx1 is much the same as our previous example, that the regression plane takes on greater tilt (with respect to x1) when bx1 increases.  Because of the increased tilt, we needed to increase the minimum and maximum for the y values via the plot=zmin=-70 zmax=70 option (we are calling the vertical axis y but SAS thinks of it as Z, so that is why the options are zmin and zmax).

%sp_plotbx1(outfile="c:\spinplots\reg_int_cont11.gif",
            bx1=0, bx2=0, bx1x2=.5, bx1_lo=0, bx1_hi=2, bx1_by=.1,
            angle=190, slen=5, gopt=delay=50, plot=zmin=-70 zmax=70);
            

As an aside, you might be tempted to look at these curved planes and think that the bx1x2 term creates regression lines that are curved.  Surprisingly, these graphs are formed all with straight lines.  The graph below tries to help illustrate this by graphing the plane as a series of lines.  As you can see, each of the lines is perfectly straight, but they twist in such a way to form the curved plane. (The cmd=scatter option is used to show a scatterplot rather than a 3d plane to help see the separate regression lines.)

%sp_plotbx1x2(outfile="c:\spinplots\reg_int_cont12.gif",
              bx1=3, bx2=0, bx1x2_lo=0, bx1x2_hi=.5, bx1x2_by=.05, 
              slen=5, angle=190, gopt=delay=50,
              plot=zmin=-70 zmax=70 noneedle shape='balloon' size=.5, cmd=scatter);

Let's see what happens when we add a quadratic term for x1 (which we are calling bx1x1).  In the graph below, we show a graph where bx2 is 3, giving tilt with respect to x2, where bx1 is 0, but bx1x1 is -.5.  This gives the plane the upside down U shape with respect to x1.

%sp_plota(outfile="c:\spinplots\reg_int_cont13.gif", 
                  cons=10, bx1=0, bx2=3, bx1x1=-.5,
                  title="Spinplot", 
                  title2="y=&cons+&bx1*x1+&bx2*x2+&bx1x1*x1*x1",
                  plot= zmin=-80 zmax=50);
          

Now, let's see what happens when we add an interaction of bx1x1 by x2 (a term we call bx1x1x2).  The %sp_plotx1x1x2 macro allows us to vary this interaction to see what effect it has on the regression plane.  The graph below varies this interaction from 0 to 0.07 by 0.01.  We increased the size (via the vsize and hsize options) to make the graph easier to view.  As you can see, as this interaction term increases, the upside down U shape gets more extreme when x2 is small and gets more flat and even slight reverses itself when x2 is large.  As you see, the strength of the x1-squared effect depends on the level of x2

%sp_plotbx1x1x2(outfile="c:\spinplots\reg_int_cont14.gif",
                cons=30, bx1=0, bx1x1=-.4, bx2=3, bx1x1x2_lo=0, bx1x1x2_hi=.07, bx1x1x2_by=.01,
		slen=5, angle=10,gopt=delay=10 vsize=4 hsize=6, plot= zmin=-100 zmax=100);

Given that bx1x1x2 is .07, we can vary bx1x2 and see how this influences the shape of the graph.  As you see below, when bx1x2 is 0, the curve of x1 is symmetrical, but as the bx1x2 term increases the values increase when x1 and x2 are both small or both large, lifting the graph in these two corners. 

%sp_plotbx1x2(outfile="c:\spinplots\reg_int_cont15.gif",
              cons=30, bx1=0, bx1x1=-.4, bx2=3, bx1x1x2=.07, bx1x2_lo=0, bx1x2_hi=.4, bx1x2_by=.01,
              title="Spinplot", 
              title2=h=4 "y=&cons+&bx1*x1+&bx2*x2+&bx1x1*x1*x1+&bx1x1x2*x1*x1*x2&bf*x1*x2" ,
              angle=10, slen=5, gopt=delay=10 vsize=4 hsize=6, plot=zmin=-120 zmax=120);

By being able to visualize these higher order terms, you can see how they might make sense in your research and if you find such relationships, you can use these macros to make graphs to help you visualize and interpret the results of your analyses.  For further information on analyses with interactions with continuous variables, please see Multiple Regression: Testing and Interpreting Interactions and Interaction Effects in Multiple Regression, both available from our Statistics Books for Loan.

The entire SAS program is stored in spinplots.sas.

How to cite this page

Report an error on this page or leave a comment

The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.