Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing all glm coefficients in R

Tags:

r

glm

How do I print glm coefficients for all factor levels, including reference level? summary(glm_obj) prints only the values that deviate from reference values.

I know that those are 0's, but I need this for integration, i.e. telling other systems what factor levels can happen at all.

Sorry if it's too simple, could not find anywhere.

Thanks

To illustrate the problem I am facing:

> glm(Petal.Width~Species,data=iris)  

Call:  glm(formula = Petal.Width ~ Species, data = iris)  

Coefficients:
          (Intercept)  Speciesversicolor   Speciesvirginica  
                0.246              1.080              1.780  

Degrees of Freedom: 149 Total (i.e. Null);  147 Residual
Null Deviance:      86.57 
Residual Deviance: 6.157    AIC: -45.29`

The model description above yields only coefficients for versicolor and virginica, which is, as Dason has noted, absolutely fine from the point of view of the model itself.

However, I needed to share the model with another application, which must know what levels of Species to expect (and e.g. issue a warning in once a new, unstudied level appears).

Summary() gives the same results:

> summary(glm(Petal.Width~Species,data=iris))

Call:
glm(formula = Petal.Width ~ Species, data = iris)

Deviance Residuals: 
   Min      1Q  Median      3Q     Max  
-0.626  -0.126  -0.026   0.154   0.474  

Coefficients:
                  Estimate Std. Error t value Pr(>|t|)    
(Intercept)        0.24600    0.02894    8.50 1.96e-14 ***
Speciesversicolor  1.08000    0.04093   26.39  < 2e-16 ***
Speciesvirginica   1.78000    0.04093   43.49  < 2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for gaussian family taken to be 0.04188163)

Null deviance: 86.5699  on 149  degrees of freedom
Residual deviance:  6.1566  on 147  degrees of freedom
AIC: -45.285

Number of Fisher Scoring iterations: 2
like image 774
coulminer Avatar asked Aug 31 '25 05:08

coulminer


1 Answers

So I realise this question is pretty old, but a simple solution is to use the dummy.coef function

fit<-glm(Petal.Width~Species,data=iris)  
summary(fit)
dummy.coef(fit)

> dummy.coef(fit)
Full coefficients are 

(Intercept):     0.246                     
Species:        setosa versicolor virginica
                  0.00       1.08      1.78

I hope this helps!

like image 195
Choppy123 Avatar answered Sep 02 '25 19:09

Choppy123