Get startedGet started for free

Visualizing categorical-continuous interactions

1. Visualizing categorical-continuous interactions

Simple smooth x,y interactions are not the only kind of complex relationship between variables in GAMs. Let's take a look at how to visualize another type of interaction.

2. Categorical-continuous interactions

Back in Chapter 1, we introduced the idea of factor-smooth interactions. That is, models where we used the "by" argument in a smooth to fit different smooths for each value of a categorical variable. Here's the fuel economy model we fit, where we used different smooths for the effect of weight on fuel economy depending on the fuel type used by cars.

3. Factor-smooths

There's another kind of categorical-continuous interaction, called a "factor-smooth". In a factor-smooth, rather than using the by argument, we use a factor-smooth basis type. We specify the two variables as part of the smooth by setting the bs argument to "fs". Note that in this case, we do not include an additional linear term to make separate intercepts for each level. The factor-smooth formulation accounts for this automatically.

4. Factor-smooths

With factor-smooths, we do not get a different term for each level of the categorical variable. Rather, we get one overall interaction term. This means that they are not as good for distinguishing between categories. However, factor smooths are good for controlling for the effects of categories that are not our main variables of interest, especially when there are very many categories, or only a few data points in some categories.

5. Plotting factor-smooths

When you call the plot() function on a GAM with a factor-smooth fit using the bs="fs" argument, it will, by default, make one plot with multiple smooths on it. However, you can also use the vis.gam() function to visualize factor-smooths. These staircase-like perspective plots are often helpful for comparing the shapes of different smooths.

6. Let's practice!

Now lets explore different categorical-continuous interactions with the meuse data set.

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.