更改 coefplot.glm() 中的系数名称

change coefficient names in coefplot.glm()

我想绘制一个带有自定义系数名称的 coefplot.glm()

考虑以下代码:

coefplot::coefplot.glm(lm(rbinom(1000,1,.5) ~ rnorm(1000,50,2) + rbinom(1000,1,prob=0.63) + rpois(1000, 2)))

这工作正常,但给了我原始变量名;我想在 coefplot.glm()c("x1", "x2", "x3", "Intercept") 的调用中更改它们。 [我实际上是在处理因式分解数据并重命名它并不容易——重命名将是另一个导入的向量]

我试过了

coefplot::coefplot.glm(lm(rbinom(1000,1,.5) ~ rnorm(1000,50,2) + rbinom(1000,1,prob=0.63) + rpois(1000, 2)),
               newNames = c("1", "2", "3", "Interc"))

但这会产生

Error in mapvalues(x, from = names(replace), to = replace, warn_missing = warn_missing) : from and to vectors are not the same length.

data <- data.frame(y=rbinom(1000,1,.5) ,x1=rnorm(1000,50,2),x2=rbinom(1000,1,prob=0.63),x3=rpois(1000, 2))
model<-lm(y~ x1 +x2  +x3 ,data=data)
coefplot::coefplot.glm(model)

你需要一个命名向量,如果你想把它放在里面就没那么简单了coefplot.glm,你可以试试下面的方法:

# create a function first for your lm
f = function(){
lm(rbinom(1000,1,.5) ~ rnorm(1000,50,2) + rbinom(1000,1,prob=0.63) + rpois(1000, 2))
}

有了这个函数,你可以调用它两次,第一次是为了剧情,第二次是为了获取名字

coefplot::coefplot.glm(f(),
newNames = setNames(c("Interc", "3", "2", "1"),names(coefficients(f())))
)

或者你只是这样做:

library(ggplot2)
coefplot::coefplot.glm(fit) + scale_y_discrete(labels=c("Interc", "3", "2", "1"))