简体   繁体   中英

Changing reference group for categorical predictor variable in logistic regression

I'm running a logistic regression with a categorical predictor variable with levels 0-6. By default, R considers level 0 as the reference group.

How can I tell R to use, for example, level 3 instead of level 0 as reference without renaming the levels?

Use the C function to define your contrasts in the dataframe.

If your dataframe is DF and the factor variable is fct , then

DF$fct <- C(DF$fct, contr.treatment, base=3)

(untested).

This is very easy with Frank Harrell's packages (which allows for many additional functions).
For example, for a dataframe named 'df'

library(Hmisc) 
library(rms)
dd=datadist(df)
options(datadist='dd')
(m=lrm(y ~ catvar, data=df)) #uses the mode as the reference group
summary(m, catvar=3) #using level=3 as the reference group
summary(m, catvar=0) #using level=0 as the reference group

(And, some sample data would have been nice - you can always use dput to output a small dataset)

C功能已经提出,还看contrastsrelevel ,并reorder ,等等。

你可以使用relevel函数:dataframe $ x1 < - relevel(datafrmae $ x1,“在这里输入参考类别”)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM