Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用relevel()并在R中拟合GLM后,恢复到因子的先前顺序_R_Glm_Revoscaler - Fatal编程技术网

使用relevel()并在R中拟合GLM后,恢复到因子的先前顺序

使用relevel()并在R中拟合GLM后,恢复到因子的先前顺序,r,glm,revoscaler,R,Glm,Revoscaler,这是一个复杂的问题,我不确定我是否尽可能简洁地表达了它,但是 我想拟合多元广义线性模型——由于模型的大小和复杂性,我不得不使用RevoScaleR软件包中的rxGlm(),而不是内置的glm()函数 模型中的每个因素都有一个我选择的参考级别,这一点很重要,我当然可以使用relevel()来设置。然而,这里令人讨厌的是,因子级别被重新排序,因此它使GLM模型输出难以处理。为了演示的目的,我希望在安装模型后能够检索原始的因子级排序 一个简单的例子: library(RevoScaleR) # fro

这是一个复杂的问题,我不确定我是否尽可能简洁地表达了它,但是

我想拟合多元广义线性模型——由于模型的大小和复杂性,我不得不使用
RevoScaleR
软件包中的
rxGlm()
,而不是内置的
glm()
函数

模型中的每个因素都有一个我选择的参考级别,这一点很重要,我当然可以使用
relevel()
来设置。然而,这里令人讨厌的是,因子级别被重新排序,因此它使GLM模型输出难以处理。为了演示的目的,我希望在安装模型后能够检索原始的因子级排序

一个简单的例子:

library(RevoScaleR) # from Microsoft R Client

x <- data.frame(country = c("Australia", "Belgium", "Chile", "Belgium", "Belgium"),
                degree = c("Y", "Y", "N", "Y", "N"),
                salary = c(10000, 15000, 5000, 20000, 4000))

model <- rxGlm(salary ~ country + degree, data = x, dropFirst = TRUE)

model$coefficients
这两个因素都按字母顺序排列,因此参考级别为
国家=澳大利亚
程度=N
。假设我希望我的参考水平为
国家=比利时
学位=Y
。我可以这样做,然后重新运行模型:

x$country <- relevel(x$country, ref = "Belgium")
x$degree <- relevel(x$degree, ref = "Y")

model <- rxGlm(salary ~ country + degree, data = x, dropFirst = TRUE)

model$coefficients
这些是我想要的系数,但现在排序是错误的。是否有一种简单的方法可以使用我在
relevel()
命令之前使用的因子排序来重新排列此项


谢谢。

创建名称向量,然后使用这些名称为系数编制索引。例如:

Names <- c(
  '(Intercept)',
  paste('country', sort(levels(x$country)), sep = '='),
  paste('degree', sort(levels(x$degree)), sep = '=')
)
coefs2 <- coefs[Names]
使用:

coefs <- c(
  `(Intercept)` = 17500L, `country=Belgium` = NA, `country=Australia` = -7500L, 
  `country=Chile` = 1000L, `degree=Y` = NA, `degree=N` = -13500L
)

coefs创建名称向量,然后使用这些名称为系数编制索引。例如:

Names <- c(
  '(Intercept)',
  paste('country', sort(levels(x$country)), sep = '='),
  paste('degree', sort(levels(x$degree)), sep = '=')
)
coefs2 <- coefs[Names]
使用:

coefs <- c(
  `(Intercept)` = 17500L, `country=Belgium` = NA, `country=Australia` = -7500L, 
  `country=Chile` = 1000L, `degree=Y` = NA, `degree=N` = -13500L
)

coefs你见过forcats::fct_relevel吗?你能展示一下你的预期输出吗?你看过forcats::fct_relevel吗?你能展示一下你的预期产出吗?谢谢。有趣。我很快就会试试这个。谢谢。有趣。我很快就会试试这个。
coefs <- c(
  `(Intercept)` = 17500L, `country=Belgium` = NA, `country=Australia` = -7500L, 
  `country=Chile` = 1000L, `degree=Y` = NA, `degree=N` = -13500L
)