本文介绍了使用glm和二项式族更改参考组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在R中使用由三个级别"Higher","Middle"和"Lower"组成的独立因子变量运行二项式回归时,我想使用relevel更改参考类别,出现此错误:

When I run a binomial regression in R with an independed factor variable consisting of three levels "Higher" , "Middle" and "Lower" of which I want to change the reference category using relevel I get this error:

"Error in relevel.ordered(cbsnivcat3, "Lower") : 
  'relevel' only for factors"

我检查了cbsnivcat3是否是一个因素

I have checked whether cbsnivcat3 is a factor

> is.factor(data$cbsnivcat3)
[1] TRUE

> levels(data$cbsnivcat3)
[1] "Higher" "Middle" "Lower" 

> t1m4=glm(tertiary ~ relevel(cbsnivcat3, "Lower") , family = binomial, data = data)
    Error in relevel.ordered(cbsnivcat3, "Lower") : 
      'relevel' only for factors

,但与factor glm(tertiary ~ factor(cbsnivcat3) , family = binomial, data = data)相同,都可以使用.

but the same with factor glm(tertiary ~ factor(cbsnivcat3) , family = binomial, data = data) works oke.

MQ1:是什么导致错误,我该怎么做?

MQ1: What causes the error, what do I do wrong?

预先感谢提示,命令和解决方案

Thanks in advance for the tips, commands and solution(s)

str(数据)

$ cbsnivcat3     : Ord.factor w/ 3 levels "Higher"<"Middle"<..: 1 1 1 1 1 1 1 1 1 1 ...
$ tertiary       : num  0 0 1 1 1 1 0 1 0 0 ...

推荐答案

cbsnivcat3是有序因素,因此relevel将不起作用.您可以将其更改为普通因素:

cbsnivcat3 is an ordered factor, so relevel won't work. What you can do is change it to an ordinary factor:

cbsnivcat3 <- factor(cbsnivcat3, levels=c("Lower","Middle","Higher"))

这篇关于使用glm和二项式族更改参考组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 07:33