【发布时间】:2021-01-11 01:47:59
【问题描述】:
我在 R 中使用 iris 数据集。我过滤了数据集,因此 iris$Species == setosa 或 versicolor。然后我创建了一个散点图,其中 x 轴是 Sepal.Length,y 轴是 Sepal.Width。点按物种高亮显示,并在散点图中按物种添加2条不同的线性回归线。
这是我的问题:
- 是否可以从散点图中获取 2 条线(setosa 或 versicolor)的斜率方程/斜率值?如果有,怎么做?
- 是否可以使用统计检验来查看 2 条线(setosa 或 versicolor)的斜率方程/斜率值是否存在显着差异?
如果/何时可以,请告诉我。
提前谢谢。
-附言
下图:
这是生成绘图的 R 代码:
# creates data for scatter plot
## dataset of interest
iris
## for iris
colnames (iris)
### creates dataset with just cases where iris$Species == setosa or versicolor
#### unique values for iris$Species
unique(iris$Species)
#### loads tidyverse package
library(tidyverse)
##### filters dataset with just cases where iris$Species == setosa or versicolor
iris__setosa_or_versicolor <- iris %>% filter(iris$Species != "virginica")
##### turns iris__setosa_or_versicolor to dataframe
iris__setosa_or_versicolor <- data.frame(iris__setosa_or_versicolor)
##### unique values for iris__setosa_or_versicolor$Species
unique(iris__setosa_or_versicolor$Species)
## creates scatter plot
### loads ggplot2
library(ggplot2)
### Basic scatter plot
scatter_plot__sepal_length_x_sepal_width__points_is_species <- ggplot(iris__setosa_or_versicolor, aes(x=Sepal.Length, y=Sepal.Width)) + geom_point()
scatter_plot__sepal_length_x_sepal_width__points_is_species
### Basic scatter plot with regression line added
scatter_plot__sepal_length_x_sepal_width__points_is_species <- ggplot(iris__setosa_or_versicolor, aes(x=Sepal.Length, y=Sepal.Width)) + geom_point() + geom_smooth(method=lm, se=FALSE, color="green")
scatter_plot__sepal_length_x_sepal_width__points_is_species
### Basic scatter plot separated by Species
scatter_plot__sepal_length_x_sepal_width__points_is_species <- ggplot(iris__setosa_or_versicolor, aes(x=Sepal.Length, y=Sepal.Width, color=Species, shape=Species)) + geom_point() + geom_smooth(method=lm, se=FALSE, fullrange=TRUE) + labs(title="Scatter plot of Sepal.Length X Sepal.Width with dots as Species where Species is setosa or versicolor", x="Sepal.Length", y = "Sepal.Width") + scale_colour_manual(values = c("#ff0000","#0000ff"))
scatter_plot__sepal_length_x_sepal_width__points_is_species
scatter_plot__sepal_length_x_sepal_width__points_is_species <-
scatter_plot__sepal_length_x_sepal_width__points_is_species + theme(panel.background = element_rect(fill = "white", colour = "white", size = 0.5, linetype = "solid"), panel.grid.major = element_line(size = 0.5, linetype = 'solid', colour = "lightblue"), panel.grid.minor = element_line(size = 0.25, linetype = 'solid', colour = "lightblue"))
scatter_plot__sepal_length_x_sepal_width__points_is_species
scatter_plot__sepal_length_x_sepal_width__points_is_species <-
scatter_plot__sepal_length_x_sepal_width__points_is_species + geom_point(size=3)
scatter_plot__sepal_length_x_sepal_width__points_is_species
### displays scatter plot
scatter_plot__sepal_length_x_sepal_width__points_is_species
编辑 1:
对评论的回应:
你在 2. 中是什么意思?您是否还想在图上添加测试结果作为注释?或者只是独立于图形比较斜率?请编辑您的问题。一旦清楚,我会回答。 (作为一般评论,请尽量避免在示例代码 sn-p 中包含与您的问题无关的详细信息,例如背景颜色和点大小的变化。)
我有兴趣比较独立于图形的斜率。我想看看回归线之间是否存在差异以及如何解释这些差异。
对答案的回应:
使用 lm 运行回归。
然后对这些回归使用 ANCOVA 来查看斜率差异。
谢谢。我想我已经按照你说的做了。将模型与没有交互的 v. 进行比较的方差分析表是显着的。我认为这意味着基于分组变量物种的回归斜率之间存在差异。这种解释正确吗?
代码如下。代码是否正确完成?
对此的后续问题:如何根据数据找到 2 条回归线 (iris$Species = setosa v. versicolor) 的斜率?
这是使用 ANCOVA 比较两个回归的代码:
## comparing the slopes of the regression lines using ANCOVA
# ---- NOTE: DV - Sepal.Width
# ---- NOTE: IV - Sepal.Length
# ---- NOTE: grouping variable: Species
# ---- NOTE: dataset: iris__setosa_or_versicolor
# ---- NOTE: based on this site: https://stats.stackexchange.com/questions/51780/how-to-perform-an-ancova-in-r
### create interaction_regression_model
interaction_regression_model <- aov(Sepal.Width~Sepal.Length*Species,data=iris__setosa_or_versicolor)
#### gives summary of interaction_regression_model
summary(interaction_regression_model)
### create no_interaction_regression_model
no_interaction_regression_model <- aov(Sepal.Width~Sepal.Length+Species,data=iris__setosa_or_versicolor)
#### gives summary of no_interaction_regression_model
summary(no_interaction_regression_model)
### compare 2 regression models, using ancova through anova command
anova(no_interaction_regression_model,interaction_regression_model)
【问题讨论】:
-
您可以使用“ggpmisc”包中的
stat_poly_eq()添加方程式。请参阅this old and popular question 的答案。也有使用其他方法的其他答案,但我更喜欢我在对前面问题的回答中描述的方法。 -
你在 2. 中是什么意思?您是否还想在图上添加测试结果作为注释?或者只是独立于图形比较斜率?请编辑您的问题。一旦清楚,我会回答。 (作为一般性评论,请尽量避免在示例代码中包含与您的问题无关的 sn-p 细节,例如背景颜色和点大小的变化。)
标签: r ggplot2 statistics regression scatter-plot