2

我想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"))

但这会产生

mapvalues(x, from = names(replace), to = replace, warn_missing = warn_missing) 错误:from向量to长度不同。

4

2 回答 2

0

关于什么

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)
于 2019-11-25T17:07:03.070 回答
0

你需要一个命名向量,如果你想在 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"))
于 2019-11-28T11:33:17.387 回答