sjlabelled icon indicating copy to clipboard operation
sjlabelled copied to clipboard

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

Open frankcsliu opened this issue 4 years ago • 4 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)

frankcsliu avatar May 14 '20 04:05 frankcsliu

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

strengejacke avatar May 28 '20 08:05 strengejacke

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.

frankcsliu avatar May 29 '20 00:05 frankcsliu

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

strengejacke avatar Nov 27 '21 12:11 strengejacke

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.

frankcsliu avatar Oct 19 '22 03:10 frankcsliu