簡體   English   中英

在同一圖形上添加多個回歸線方程R2和SSE

[英]Adding Multiple Regression Line Equations, R2 and SSE on the same graph

在R中,我使用stat_poly_eq()注釋繪圖中線性模型的方程,遇到兩個問題:

  1. 如何注釋三個單獨的方程式,一個方程式用於每個組,另一個方程式用於整個數據?

  2. 如何在每個方程式上加上相應的平方誤差和(SSE)?

如所示在這里 ,下面的代碼產生包括所有數據的一般方程:

x <- runif(200, 0, 100)
y <- 5*x + rnorm(200, 0, 10)
df <- data.frame(x, y)
df$GENDER[1:100] <- 1
df$GENDER[101:nrow(df)] <- 2



formula <- y  ~ poly(x, 1, raw = TRUE)


my_features <- list(scale_shape_manual(values=c(16, 1)),
                  geom_smooth(method = "lm", aes(group = 1), 
                              formula = formula, colour = "Black", 
                              fill = "grey70"),
                  geom_smooth(method = "lm", aes(group = factor(GENDER), se = F),
                              formula = formula, colour = "Black"),
                  stat_poly_eq(aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~~")),
                               formula = formula, parse = TRUE)
)


ggplot(df, aes(x = x, y = y, aes(shape = factor(GENDER)))) +
  geom_point(aes(shape = factor(GENDER))) + 
  my_features

我必須手動添加誤差平方和,然后根據完整的數據集定位方程式。 使用以下方法。

library(ggplot2)
library(ggpmisc)

# Get Error Sum of Squares
sum((lm(y ~ poly(x, 1, raw = TRUE)))$res^2)
sum(lm(y[df$GENDER == 1] ~ poly(x[df$GENDER == 1], 1, raw = TRUE))$res^2)
sum(lm(y[df$GENDER == 2] ~ poly(x[df$GENDER == 2], 1, raw = TRUE))$res^2)


my_features <- list(
  scale_shape_manual(values=c(16, 1)),
  geom_smooth(method = "lm", aes(group = 1), 
    formula = formula, colour = "Black", fill = "grey70"),                                  
                                                         #Added colour
  geom_smooth(method = "lm", aes(group = factor(GENDER), colour = factor(GENDER)),
    formula = formula, se = F),
  stat_poly_eq(
    aes(label = paste(paste(..eq.label.., ..rr.label.., sep = "~~~~"),
                            #Manually add in ESS
                      paste("ESS", c(9333,9622), sep = "=="),
                sep = "~~~~")),
    formula = formula, parse = TRUE)
)

ggplot(df, aes(x = x, y = y, shape = factor(GENDER), colour = factor(GENDER))) +
  geom_point(aes(shape = factor(GENDER))) +
  my_features +

  #Add in overall line and label
  geom_smooth(method = "lm", aes(group = 1), colour = "black") +
  stat_poly_eq(aes(group = 1, label = paste(..eq.label.., ..rr.label.., 'ESS==19405', sep = "~~~~")),
                           formula = formula, parse = TRUE, label.y = 440)

在此處輸入圖片說明

或者,您可以復制您的數據集,因此完整的數據集本身就包含在一個因子級別中...仍然需要手動添加ESS。

x <- runif(200, 0, 100)
y <- 5*x + rnorm(200, 0, 10)
df1 <- data.frame(x, y)
df1$GENDER[1:100] <- 1
df1$GENDER[101:nrow(df1)] <- 2

df2 <- df1
df2$GENDER <- 3

#Now data with GENDER == 3 is the full data
df <- rbind(df1, df2)

my_features <- list(
                          #Add another plotting character
scale_shape_manual(values=c(16, 1, 2)),                            
                                                         #Added colour
  geom_smooth(method = "lm", aes(group = factor(GENDER), colour = factor(GENDER)),
    formula = formula, se = F),
  stat_poly_eq(
    aes(label = paste(paste(..eq.label.., ..rr.label.., sep = "~~~~"),
                            #Manually add in ESS
                      paste("ESS", c(9333,9622,19405), sep = "=="),
                sep = "~~~~")),
    formula = formula, parse = TRUE)
)

ggplot(df, aes(x = x, y = y, shape = factor(GENDER), group = factor(GENDER), colour = factor(GENDER))) +
  geom_point(aes(shape = factor(GENDER))) +
  my_features

在此處輸入圖片說明

編輯:如果要刪除第三組的打印字符,也可以這樣做。

my_features <- list(
  geom_smooth(method = "lm", aes(group = factor(GENDER), colour = factor(GENDER)),
    formula = formula, se = F),
     stat_poly_eq(
       aes(label = paste(paste(..eq.label.., ..rr.label.., sep = "~~~~"),
                               #Manually add in ESS
                         paste("ESS", c(9333,9622,19405), sep = "=="),
                   sep = "~~~~")),
       formula = formula, parse = TRUE)
)

p <- ggplot(df, aes(x = x, y = y, shape = factor(GENDER), group = factor(GENDER), colour = factor(GENDER))) +
      my_features 
p + 
  scale_color_manual(labels = c("Male", "Female", "Both"), values = hue_pal()(3)) +
  geom_point(data = df[df$GENDER == 1,], aes(colour = factor(GENDER)), shape = 16)+
  geom_point(data = df[df$GENDER == 2,], aes(colour = factor(GENDER)), shape = 1) +
  guides(colour = guide_legend(title = "Gender", override.aes = list(shape = NA)))

在此處輸入圖片說明

暫無
暫無

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

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