Search code examples
roffset

How to fix coefficients in R for categorical variables


I would like to know how to put offsets (or fixed coefficients) in a model on categorical variables for each different level and see how that effects the other variables. I'm not sure how to exactly code that.

library(tidyverse)

mtcars <- as_tibble(mtcars)

mtcars$cyl <- as.factor(mtcars$cyl)

model1 <- glm(mpg ~ cyl + hp, data = mtcars)
summary(model1)

This gives the following:

Call: glm(formula = mpg ~ cyl + hp, data = mtcars)

Deviance Residuals: Min 1Q Median 3Q Max

-4.818 -1.959 0.080 1.627 6.812

Coefficients: Estimate Std. Error t value Pr(>|t|)
(Intercept) 28.65012 1.58779 18.044 < 2e-16 *** cyl6 -5.96766 1.63928 -3.640 0.00109 ** cyl8 -8.52085 2.32607 -3.663 0.00103 ** hp -0.02404 0.01541 -1.560 0.12995
--- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for gaussian family taken to be 9.898847)

`Null deviance: 1126.05  on 31  degrees of freedom`

Residual deviance: 277.17 on 28 degrees of freedom AIC: 169.9

Number of Fisher Scoring iterations: 2

I would like to set the cylinders to different offsets, say 6 cylinders to -4 and 8 cylinders to -9 so I can see what that does to horse power. I tried this in the below code but get an errror so I'm not sure the correct way to do one unique value in a categorical variable much less more than one.

model2 <- glm(mpg ~ offset(I(-4 * cyl[6]))+ hp, data = mtcars)

Would anyone help me figure out how to correctly do this?


Solution

  • In a fresh R session:

    glm(mpg ~ offset(I(-4 * (cyl == 6) + -9 * (cyl == 8))) + hp, data = mtcars)
    # Call:  glm(formula = mpg ~ offset(I(-4 * (cyl == 6) + -9 * (cyl == 8))) + 
    #     hp, data = mtcars)
    # 
    # Coefficients:
    # (Intercept)           hp  
    #    27.66881     -0.01885  
    # 
    # Degrees of Freedom: 31 Total (i.e. Null);  30 Residual
    # Null Deviance:        353.8 
    # Residual Deviance: 302    AIC: 168.6