0
$\begingroup$

So I have a categorical variable color which can take the values white, black, red.

I created dummy variables for each of those colors and the related coefficients for all of those dummy variables is negative.

I wonder how is this possible, what would be the baseline here?

EDIT: y variable is price, using sklearn.linear_model.LinearRegression coupled with pandas.get_dummies

$\endgroup$
  • $\begingroup$ what is your y variable? what code/data are you using? $\endgroup$ – probabilityislogic Feb 20 '19 at 12:36
  • $\begingroup$ See edit Im using sklearn and pandas the y variable is price. $\endgroup$ – Franco Piccolo Feb 20 '19 at 12:59
1
$\begingroup$

This is no problem. Intuitively, if you include all three categories, their coefficients "absorb" the intercept, which might be negative.

$\endgroup$
  • $\begingroup$ The intercept is positive, and all the coefficients are negative. $\endgroup$ – Franco Piccolo Feb 20 '19 at 11:52
1
$\begingroup$

Yes, the intercept is the base level, and the coefficient of each color is relative to that base level. Here is a short example in R. relevel allow to decide which level of the factor will be used as the base line.

library('dplyr')

> col <- factor(sample(c('red', 'blue', 'white'), n, replace = TRUE))
> 
> y <- case_when(col == 'red' ~ 30, 
+                col == 'blue' ~ 80, 
+                col == 'white' ~ 115) + rnorm(n, 0, 5)
> 
> 
> col <- relevel(col, 'red')
> lm(y ~ col) ### coefficients will be positive  

Call:
lm(formula = y ~ col)

Coefficients:
(Intercept)      colblue     colwhite  
      29.84        50.02        86.14  

> 
> col <- relevel(col, 'blue')
> lm(y ~ col) ### coefficients will be positive and negative 

Call:
lm(formula = y ~ col)

Coefficients:
(Intercept)       colred     colwhite  
      79.86       -50.02        36.12  

> 
> col <- relevel(col, 'white')
> lm(y ~ col) ### coefficients will be negative

Call:
lm(formula = y ~ col)

Coefficients:
(Intercept)      colblue       colred  
     115.98       -36.12       -86.14  

Example when all the coefficients are negative (notice there is no intercept)

> y <- case_when(col == 'red'   ~ -30, 
+                col == 'blue'  ~ -80, 
+                col == 'white' ~ -115) + rnorm(n, 0, 5)
> 
> lm(y ~ 0 + col) ### coefficients will be negative

Call:
lm(formula = y ~ 0 + col)

Coefficients:
colwhite   colblue    colred  
 -115.75    -80.30    -30.24  
$\endgroup$
  • $\begingroup$ But in this case you have one of the levels as the baseline, in my case all the levels have negative coefficients. $\endgroup$ – Franco Piccolo Feb 20 '19 at 11:53
  • $\begingroup$ If you have an intercept in the model, then one of the factors level is absorbed by it (notice in the example that the intercept value is similar to that of the level). Ill add an example in the answer. $\endgroup$ – Kozolovska Feb 20 '19 at 11:58
  • $\begingroup$ Thanks for the example, but in my particular case, in sklearn implementation in Python, there is an intercept which is positive, and at the same time there is a coefficient for each of the variables, which are all negative. $\endgroup$ – Franco Piccolo Feb 20 '19 at 12:09

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.