有什么方法可以适合`glm()`以便包含所有级别(即没有参考级别)吗? [英] Is there any way to fit a `glm()` so that all levels are included (i.e. no reference level)?

查看:53
本文介绍了有什么方法可以适合`glm()`以便包含所有级别(即没有参考级别)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑代码:

x <- read.table("http://data.princeton.edu/wws509/datasets/cuse.dat",
                header=TRUE)[,1:2]

fit <- glm(education ~ age, family="binomial", data=x)

summary(fit)

年龄分为4个级别:"<25","25-29","30-39","40-49"

Where age has 4 levels: "<25" "25-29" "30-39" "40-49"

结果是:

因此,默认情况下,其中一个级别用作参考级别.有没有办法让所有4级+截距都具有glm输出系数(即没有参考级)?像SAS这样的软件包默认情况下会执行此操作,因此我想知道是否有任何选择.

So by default, one of the levels is used as a reference level. Is there a way to have glm output coefficients for all 4 levels + the intercept (i.e. have no reference level)? Software packages like SAS do this by default, so I was wondering if there was any option for this.

谢谢!

推荐答案

请参见?formula,特别是在模型规格中包括+ 0的含义...

See ?formula, specifically, the meaning of including + 0 in your model specification...

# Sample data - explanatory variable (continuous)
x <- runif( 100 )
# explanatory data, factor with 3 levels
f <- as.factor( sample( 3 , 100 , TRUE ) )
# outcome data
y <- runif( 100 ) + rnorm(100) + rnorm( 100 , mean = c(1,3,6) )

# model without intercept
summary( glm( y ~ x + f + 0 ) )
#Call:
#glm(formula = y ~ x + f + 0)

#Deviance Residuals: 
#    Min       1Q   Median       3Q      Max  
#-5.7316  -1.8923   0.0195   1.8918   5.9520  

#Coefficients:
#   Estimate Std. Error t value Pr(>|t|)    
#x    0.3216     0.9772   0.329    0.743    
#f1   3.4493     0.6823   5.055 2.06e-06 ***
#f2   3.6349     0.6959   5.223 1.02e-06 ***
#f3   3.1962     0.6598   4.844 4.87e-06 ***

这篇关于有什么方法可以适合`glm()`以便包含所有级别(即没有参考级别)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆