ggplot2:绘制不同截距但具有相同斜率的回归线 [英] ggplot2: Plotting regression lines with different intercepts but with same slope

查看:1153
本文介绍了ggplot2:绘制不同截距但具有相同斜率的回归线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制不同截距的回归线,但斜率相同。

使用下面的 ggplot2 代码,我可以绘制具有不同截距和不同斜率的回归线。但无法弄清楚如何绘制具有不同截距但不同斜率的回归线。任何帮助将不胜感激。谢谢

  library(ggplot2)
ggplot(data = df3,mapping = aes(x = Income,y = Consumption ,color = Gender))+ geom_point()+
geom_smooth(data = df3,method =lm,se = FALSE,mapping = aes(x = Income,y = Consumption))

消费<-c(51,52,53,54,56,57,55,56,58,59,62,63)
性别< -g1(n = 2,k = 6,长度= 2 * 6,标签= c(男性,女性),有序=假)
收入< - 代表(x = c(80,90,100),每个= 2)
df3< - data.frame(消费,性别,收入)
df3

#对于每个性别
fm1 < - lm(公式=消费〜性别+收入,数据= df3)
总结(fm1)

调用:
lm(公式=消费〜性别+收入,数据= df3)

残差:
分钟1Q中位数3Q最大
-0.8333 -0.8333 0.1667 0.1667 1.1667

系数:
估计标准。错误t值Pr(> | t |)
(拦截)26.83333 2.54557 10.54 2.30e-06 ***
GenderFemale 5.00000 0.45812 10.91 1.72e-06 ***
收入0.30000 0.02805 10.69 2.04e-06 ***
---
Signif。代码:0'***'0.001'**'0.01'*'0.05'。'0.1''1

9个自由度上的剩余标准误差:0.7935
多个R-平方:0.9629,调整的R平方:0.9546
F统计量:第2和第9位DF为116.7,p值为3.657e-07


解决方案

为什么不计算ggplot之外的回归,其结果来自 lm

 #对于每个性别
fm1 < - lm具有相同斜率但截距不同的回归(公式=消费性别+收入,数据= df3)
df3 = cbind(df3,pred = predict(fm1))

ggplot(data = df3,mapping = aes(x =收入,y =消费,颜色=性别))+ geom_point()+
geom_line(mapping = aes(y = pred))

产生相同的斜率和不同的截距:



技术上,如您在模型中看到的那样,没有两个不同的截距,但是对于虚拟变量 GenderFemale

编辑:包括预测来简化,感谢@aosmith的建议。

I want to plot regression lines with different intercepts but with the same slope.

With the following ggplot2 code, I can plot regression lines with different intercepts and different slopes. But could not figured out how to draw regression lines with different different intercepts but the same slopes. Any help will be highly appreciated. Thanks

library(ggplot2)
    ggplot(data=df3, mapping=aes(x=Income, y=Consumption, color=Gender)) + geom_point() + 
    geom_smooth(data=df3, method = "lm", se=FALSE, mapping=aes(x=Income, y=Consumption))

Consumption <- c(51, 52, 53, 54, 56, 57, 55, 56, 58, 59, 62, 63)
Gender <- gl(n = 2, k = 6, length = 2*6, labels = c("Male", "Female"), ordered = FALSE)
Income <- rep(x=c(80, 90, 100), each=2)
df3 <- data.frame(Consumption, Gender, Income)
df3

# Regression with same slope but different intercepts for each Gender
fm1 <- lm(formula=Consumption~Gender+Income, data=df3)
summary(fm1)

Call:
lm(formula = Consumption ~ Gender + Income, data = df3)

Residuals:
    Min      1Q  Median      3Q     Max 
-0.8333 -0.8333  0.1667  0.1667  1.1667 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept)  26.83333    2.54557   10.54 2.30e-06 ***
GenderFemale  5.00000    0.45812   10.91 1.72e-06 ***
Income        0.30000    0.02805   10.69 2.04e-06 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.7935 on 9 degrees of freedom
Multiple R-squared:  0.9629,    Adjusted R-squared:  0.9546 
F-statistic: 116.7 on 2 and 9 DF,  p-value: 3.657e-07

解决方案

Why don't you calculate the regression outside of ggplot with the results from lm:

# Regression with same slope but different intercepts for each Gender
fm1 <- lm(formula=Consumption~Gender+Income, data=df3)
df3 = cbind(df3, pred = predict(fm1))

ggplot(data=df3, mapping=aes(x=Income, y=Consumption, color=Gender)) + geom_point() + 
  geom_line(mapping=aes(y=pred))

Produces same slope and different intercepts:

Technically as you see in your model there is not two different intercepts but an extra offset to the dummy variable GenderFemale.

Edit: included predict to simplify, thanks to @aosmith for suggesting.

这篇关于ggplot2:绘制不同截距但具有相同斜率的回归线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆