strengejacke / sjlabelled

Working with Labelled Data in R

Home Page:https://strengejacke.github.io/sjlabelled

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

the argument ref.lvl in sjmisc::to_factor() not working

frankcsliu opened this issue · comments

Hi, Daniel, I recently found that the argument is not effective any more and seems not recognizing labels.
Example data to download here

load(kao06)
library(sjmisc)
kao06$partyID <- rec(kao06$L2B,
rec="98:hi=NA; else=copy",
val.labels = c("partyA","partyB","partyC",
"partyD", "partyE", "others"),
as.num=F)

not working

kao06$partyID <- to_factor(kao06$partyID, ref.lvl="partyB")
kao06.mod.2 <- glm(turnout ~ partyID, family=binomial, data=kao06)
summary(kao06.mod.2)

not working

kao06$partyID <- to_factor(kao06$partyID, ref.lvl="partyA")
summary(kao06.mod.2)

not working

kao06$partyID <- to_factor(kao06$partyID, ref.lvl=2)
summary(kao06.mod.2)

this traditional method works:

kao06$partyID <- ref_lvl(kao06$partyID, lvl=2)
summary(kao06.mod.2)

yes, to_factor() doesn't work, because your input is already a factor. That's why it's immediately returned, unchanged.

Could you make it work for factor, too? I see it as a more sofiscated alternative to ref_lvl() because I like the argument of specifiying label. Thanks.

It's actually an issue of sjlabelled. sjmisc just re-exports that function.

I recently learned that forcats::fct_relevel() may help reset the reference group, but it is far from handier than this wanted feature sjlabelled::to_factor() for factors.