简体   繁体   English

在同一图形上添加多个回归线方程R2和SSE

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

In R, I am using stat_poly_eq() to annotate the equation from a linear model on a plot, and I encounter 2 issues: 在R中,我使用stat_poly_eq()注释绘图中线性模型的方程,遇到两个问题:

  1. How can I annotate three separate equations, one for each group and another with the the whole data? 如何注释三个单独的方程式,一个方程式用于每个组,另一个方程式用于整个数据?

  2. How can I add the corresponding error sum of squares (SSE) on each equation? 如何在每个方程式上加上相应的平方误差和(SSE)?

As shown here , the following code produces a general equation including all data: 如所示在这里 ,下面的代码产生包括所有数据的一般方程:

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

I had to manually add in the error sum of squares, and position the equation based on the full data set. 我必须手动添加误差平方和,然后根据完整的数据集定位方程式。 Using the approach below. 使用以下方法。

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)

在此处输入图片说明

Or you could duplicate your data set, so the full data set is contained within a factor level itself... Still need to manually add the ESS. 或者,您可以复制您的数据集,因此完整的数据集本身就包含在一个因子级别中...仍然需要手动添加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

在此处输入图片说明

Edit: If you want to remove the plotting characters for the third group that can be done too. 编辑:如果要删除第三组的打印字符,也可以这样做。

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