首页 > 解决方案 > 如何使用 jtools 包中的 coef_plot 在系数图中更好地标记分类变量

问题描述

我正在尝试创建包含分类和线性变量的线性模型的系数图。我希望系数图显示分类变量的系数是与基线类别的比较 - 这里“国家”将其他国家与“英格兰”进行比较。

有没有办法做到这一点,而不是将每个变量单独标记为“国家变量 - 与英格兰 - 威尔士的比较”或类似的东西?下面的示例代码


## Create example dataframe

DV <- c(0.1, 0.6, 0.8, 1, 1, 0, 0.4, 0.8, 0.9 , 0.3)
country <- c('England', 'Wales', 'Scotland', 'England', 'Wales', 'Scotland', 'England', 'Wales', 'Scotland', 'Scotland')
politics <- c(0.8, 1, 0.1, 0.6,0, 1,  0.9 , 0.3, 0.4, 0.8)

df <- data.frame(DV, country, politics)

## Run linear model

mod1 <- lm(DV ~ country + politics, data = df)


## Coefficient plot of the model

plot_coefs(mod1)

## Rename the variables in the coefficient plot

plot_coefs(mod1,
           coefs = c('Scotland' = 'countryScotland',
                     'Wales' = 'countryWales',
                     'Political alignment' = 'politics'))

以上并没有达到我想要的 - 分类变量不同级别的系数显然不是分类变量的级别。谢谢

我想更改标签,使其显示类似

国家(以英格兰为基准)

系数显示在级别标签的右侧,但标题显示它是具有多个级别的分类变量

标签: rggplot2regression

解决方案


推荐阅读