簡體   English   中英

從散點圖中找到 2 個回歸方程/斜率並比較方程/斜率

[英]Finding 2 regression equations/slopes and comparing equations/slopes from a scatter plot

我在 R 中使用 iris 數據集。我過濾了數據集,因此 iris$Species == setosa 或 versicolor。 然后我創建了一個散點圖,其中 x 軸是 Sepal.Length,y 軸是 Sepal.Width。 根據物種突出顯示這些點,並根據物種將 2 條不同的線性回歸線添加到散點圖中。

以下是我的問題:

  1. 是否可以從散點圖中獲得 2 條線(setosa 或 versicolor)的斜率方程/斜率值? 如果是這樣,如何?
  2. 是否可以使用統計測試來查看 2 條線(setosa 或 versicolor)的斜率方程/斜率值是否彼此顯着不同?

如果/何時可以,請告訴我。

提前致謝。

-PS

這是圖:

Sepal.Length X Sepal.Width 散點圖,點為 Species,其中 Species 為 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. 你是什么意思? 您是否還想將測試結果添加為圖形上的注釋? 或者只是獨立於圖形比較斜率? 請編輯您的問題。 一旦清楚,我會回答。 (作為一般性評論,盡量避免在示例代碼片段中包含與您的問題無關的詳細信息,例如背景顏色和點大小的變化。)

我有興趣比較獨立於圖形的斜率。 我想看看回歸線之間是否存在差異以及如何解釋這些差異。

回復回復:

使用 lm 運行回歸。

然后對這些回歸使用 ANCOVA 以查看斜率差異。

謝謝。 我想我已經試着按照你說的去做了。 將模型與沒有交互的 v. 進行比較的方差分析表很重要。 我認為這意味着基於分組變量物種的回歸斜率之間存在差異。 這個解釋正確嗎?

代碼如下。 代碼是否正確完成?

對此的后續問題:如何根據數字找到 2 條回歸線(iris$Species = setosa v. versicolor)的斜率?

這是 ANCOVA 比較 2 個回歸的代碼:


## 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)

需要記住的是,繪圖就是繪圖- 它是一種幫助您可視化和理解數據的方法。 它與數據本身不是一回事。 您無法像處理數據那樣操作、轉換、處理、轉換或統計分析繪圖

同樣,在繪圖上繪制的回歸線與線性回歸不同。 是的,繪圖軟件必須進行線性回歸才能得到這條線,但您不應該嘗試從圖中提取有關回歸的信息。 這是在做事倒退。 如果要進行回歸,請進行回歸。

為了使事情變得簡單並與您的數據集等效,我們將從鳶尾花數據集中刪除 virginica 物種:

iris_filtered <- subset(iris, Species != "virginica", drop = TRUE)

現在我們根據SpeciesSepal.LengthSepal.Width進行線性回歸。 我們使用函數lm來做到這一點。 我們想知道Sepal.Length的斜率在Species之間是否不同,因此我們對它們之間的相互作用進行建模。 以下行完成了所有這些:

model <- lm(Sepal.Width ~ Species * Sepal.Length, data = iris_filtered)

現在我們回顧一下我們的模型:

summary(model)

#> Call:
#> lm(formula = Sepal.Width ~ Species * Sepal.Length, data = iris_filtered)
#> 
#> Residuals:
#>      Min       1Q   Median       3Q      Max 
#> -0.72394 -0.16281 -0.00306  0.15936  0.60954 
#> 
#> Coefficients:
#>                                Estimate Std. Error t value Pr(>|t|)    
#> (Intercept)                     -0.5694     0.5352  -1.064 0.290049    
#> Speciesversicolor                1.4416     0.6891   2.092 0.039069 *  
#> Sepal.Length                     0.7985     0.1067   7.487 3.41e-11 ***
#> Speciesversicolor:Sepal.Length  -0.4788     0.1292  -3.707 0.000351 ***
#> ---
#> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#> 
#> Residual standard error: 0.2632 on 96 degrees of freedom
#> Multiple R-squared:  0.707,  Adjusted R-squared:  0.6978 
#> F-statistic:  77.2 on 3 and 96 DF,  p-value: < 2.2e-16

這是這個模型告訴我們的:

  • setosa線在 -0.5694cm 處與 y 軸交叉,但 p 值與 0 沒有顯着差異(p = 0.29)
  • versicolor線穿過 y 軸,比setosa線高 1.4416 厘米,在 0.8722 厘米(-0.5694 + 1.4416 = 0.8722)處。 這種差異在 p = 0.039 處僅具有統計顯着性。
  • 對於setosa物種, Sepal.Length每增加 1cm 的Sepal.Width增加 0.7985cm。 該斜率與 0 非常顯着不同。
  • 所述Sepal.Width在增加由0.3197厘米每厘米增長Sepal.Length物種- (0.4788 = 0.3917 0.7985) versicolor 這與setosa的梯度顯着不同(p = 0.000351)。

因此,我們有我們的實際模型,並從它的梯度,我們知道在山坡上有顯著差距,我們用3行代碼做這件事,我們也沒必要情節什么。

只是為了證明這是有效的,讓我們在我們的數據上“手工”繪制這些線以顯示回歸的外觀:

with(iris[iris$Species == "setosa",], 
     plot(Sepal.Length, Sepal.Width, col = "red", xlim = c(4, 7), ylim = c(2, 4.5)))
with(iris[iris$Species == "versicolor",], 
     points(Sepal.Length, Sepal.Width, col = "blue"))
abline(a = -0.5694, b = 0.7985, col = "red")
abline(a = 0.8722, b = 0.3197, col = "blue")

在此處輸入圖片說明

使用 lm 運行回歸。

然后對這些回歸使用 ANCOVA 以查看斜率差異。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM