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

我想绘制一个coefplot.glm()并标明系数名称。

考虑以下代码:

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

这很好用,但是给了我原始的变量名;我想在coefplot.glm()的{​​{1}}调用中更改它们。 [我实际上正在使用分解数据,并且重命名它确实不容易,重命名将是另一个导入的向量]

我尝试过

c("x1","x2","x3","Intercept")

但这会产生

  

mapvalues(x,从=名称(替换),到=替换,warn_missing = warn_missing)中的错误:coefplot::coefplot.glm(lm(rbinom(1000,2)),newNames = c("1","2","3","Interc")) from向量的长度不同。

chengwei8520 回答:更改coefplot.glm()中的系数名称

data <- data.frame(y=rbinom(1000,1,.5),x1=rnorm(1000,50,2),x2=rbinom(1000,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,.5) ~ rnorm(1000,2) + rbinom(1000,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","1"))
本文链接:https://www.f2er.com/3034688.html

大家都在问