2
$\begingroup$

I'm creating a multivariate model in R right now.

When I plot categorical variables into the lm() function and check the summary() output, my categorical variable gets split up into a beta coefficient for each option inside of the variable.

When I checked the data type it came back as a factor variable, here is the output of summary to visualize the issue easier.

$\endgroup$
5
  • 3
    $\begingroup$ Each category is modeled as the difference between it and a reference category (the intercept). Iif you 5 categories, you need 4 coefficients to model those 5 means. $\endgroup$
    – Axeman
    Nov 29, 2019 at 19:54
  • $\begingroup$ I'm finding it hard understanding this from a more traditional numeric variable regression. I figured the variable would have a constant number next to the coefficient to fit for whatever the beta value would be. $\endgroup$
    – Freddy Haug
    Nov 29, 2019 at 20:07
  • 1
    $\begingroup$ See e.g. here or here. R basically automatically expands your factor into binary dummy variables. $\endgroup$
    – Axeman
    Nov 29, 2019 at 20:18
  • $\begingroup$ If you want a global "test" for your factor (as in an ANOVA) you can use for example drop1 that basically compare the model with and without the factor as explanatory variable. For example : m <- lm(Sepal.Length ~ Species, data = iris) ; drop1(m, test = "F") $\endgroup$
    – Gilles
    Nov 29, 2019 at 20:27
  • $\begingroup$ In R, it's also common to car::Anova to get a traditional anova table (with type ii sum of squares by default). To use @Gilles ' example: if(!require(car)){install.packages("car")}; m = lm(Sepal.Length ~ Species, data = iris); library(car); Anova(m) $\endgroup$ Nov 30, 2019 at 15:50

1 Answer 1

3
$\begingroup$

In a nutshell, since Regression analysis requires numerical variables; when a regression is performed with a categorical variable in a regression model, each category of the variable is transformed into a separate variable aka 'dummy' variable. When a dummy variable is 1, it means a categorical variable belongs to that category represented by this dummy variable.

This link here explains nicely how regression with categorical variables is performed in R :

http://www.sthda.com/english/articles/40-regression-analysis/163-regression-with-categorical-variables-dummy-coding-essentials-in-r/

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy