对于我在这个平台上的第一个问题,我提前道歉.我浏览过很多线程,但我发现的大部分内容都不涉及任何伪变量. 然而,经过大约4个小时的调查,我似乎找不出出问题了.
出于教学和演示的目的,我目前正在try 使用众所周知的虹膜数据集来设置虚拟变量线性模型的小图.
然而,无论我try 什么,我都无法让它同时绘制多条回归线,而我预计有两条回归线:一条从花斑到setosa,一条从virginica到setosa,setosa是第一组因素.
这是我的代码以及迄今为止我try 过的内容.
#正在加载tidyverse.
library(tidyverse)
#加载iris数据集并将其保存为对象.
dataset <- as_tibble(iris)
#作为背景:我想可视化以下线性回归.
ols_lm_iris_all <- lm(Petal.Length ~ Species,
data = dataset)
summary(ols_lm_iris_all)
#这会产生下面的lm模型,该模型给出了以下两的比较:versicolor与setosa以及virginica与setosa.到目前为止一切顺利.
#我用于可视化数据的代码如下.
iris_lm_plot_all <- ggplot(dataset, aes(x = Species, y = Petal.Length, colour = Species)) +
geom_smooth(method = lm, aes(group = Species)) +
geom_jitter(width = 0.2) +
labs(title = "Linear OLS regression with two regression lines for three types of Iris.")
iris_lm_plot_all
#然而,最后我得到的是一个有抖动但根本没有任何回归线的散点图.
#这是我也try 过的,部分成功了.如果我们指定"aes(group = 1)"而不是group = Species,我们会得到从最后一个因子virginica到setosa的一条lm线.这只是工作的一半,但现在我们没有将花彩变成setosa.
iris_lm_plot_all <- ggplot(dataset, aes(x = Species, y = Petal.Length, colour = Species)) +
geom_smooth(method = lm, aes(group = 1)) +
geom_jitter(width = 0.2) +
labs(title = "Linear OLS regression with two regression lines for three types of Iris.")
我还想到了:geom_smooth是否没有正确处理Species的虚拟化?