【问题标题】:Visualizing the relationship between a continuous predictor and a categorical outcome可视化连续预测变量和分类结果之间的关系
【发布时间】:2018-11-07 10:55:12
【问题描述】:

我正在尝试可视化连续预测变量(范围 0-0.8)和离散结果(计数变量,可能值:0、1、2)之间的关系。

有许多选项可以在 x 轴上显示离散变量,在 y 轴上显示连续变量(例如,点图、小提琴、箱线图等)。这些选项显示连续预测变量的分布以及每组离散变量的中心性度量。但是,这并没有显示我试图描绘的信息。我想展示随着连续变量得分的增加,离散变量的值增加的可能性。

我曾尝试使用 geom_smooth 进行此操作,但由于结果是离散的,这似乎具有误导性:

p <- ggplot(pheno, aes(adhdanx, polye))
p + geom_smooth(method = "lm", colour = "#007ea7", size = 0.5, fill = "#007ea7")

我在 R 中工作。欢迎提出所有建议。

【问题讨论】:

  • 分享pheno,让您的问题可重现
  • 使用正确排序的因子变量的箱线图

标签: r ggplot2 plot relationship


【解决方案1】:

据我所知,对于仅具有分类预测变量的线性回归模型,不可能有直线拟合。您可以绘制每个。这里我会使用iris 数据集。

library(tidyverse)
as_tibble(iris)
#> # A tibble: 150 x 5
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>           <dbl>       <dbl>        <dbl>       <dbl> <fct>  
#>  1          5.1         3.5          1.4         0.2 setosa 
#>  2          4.9         3            1.4         0.2 setosa 
#>  3          4.7         3.2          1.3         0.2 setosa 
#>  4          4.6         3.1          1.5         0.2 setosa 
#>  5          5           3.6          1.4         0.2 setosa 
#>  6          5.4         3.9          1.7         0.4 setosa 
#>  7          4.6         3.4          1.4         0.3 setosa 
#>  8          5           3.4          1.5         0.2 setosa 
#>  9          4.4         2.9          1.4         0.2 setosa 
#> 10          4.9         3.1          1.5         0.1 setosa 
#> # ... with 140 more rows

考虑回归问题Petal.width ~ Species

iris %>%
  ggplot() +
  aes(x = Species, y = Petal.Width, colour = Species) +
  geom_boxplot(show.legend = FALSE)

从这个箱线图中,可以看到Petal.width在每个Species中的分布情况和正相关关系。对于定性预测器,变量将被编码为:

contrasts(iris$Species)
#>            versicolor virginica
#> setosa              0         0
#> versicolor          1         0
#> virginica           0         1

这样模型就变成了

在哪里

因此,每个拟合值将变为

根据这些估计

lm(Petal.Width ~ Species, data = iris)
#> 
#> Call:
#> lm(formula = Petal.Width ~ Species, data = iris)
#> 
#> Coefficients:
#>       (Intercept)  Speciesversicolor   Speciesvirginica  
#>             0.246              1.080              1.780

如上所述,有了这些事实,每个拟合值都可以绘制在图上。

来自lm()

iris %>%
  select(Species, Petal.Width) %>% # just for clarity
  mutate(pred = lm(Petal.Width ~ Species)$fitted.values) %>% # linear regression
  ggplot() +
  aes(x = Species, y = Petal.Width) +
  geom_point() +
  geom_point(aes(x = Species, y = pred), col = "red", size = 3) # fitted values

另外,注意每个拟合值都是样本均值

iris %>%
  select(Species, Petal.Width) %>%
  group_by(Species) %>% # for each category
  mutate(pred = mean(Petal.Width)) %>% # sample mean of response in each category
  ggplot() +
  aes(x = Species, y = Petal.Width) +
  geom_point() +
  geom_point(aes(x = Species, y = pred), col = "red", size = 3)

【讨论】:

    猜你喜欢
    • 2021-03-23
    • 1970-01-01
    • 2021-03-16
    • 2019-10-15
    • 2021-11-28
    • 2017-11-25
    • 1970-01-01
    • 2022-01-25
    • 2018-05-23
    相关资源
    最近更新 更多