Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot2:绘制具有不同截距但具有相同坡度的回归线_R_Ggplot2_Statistics_Regression_Linear Regression - Fatal编程技术网

R ggplot2:绘制具有不同截距但具有相同坡度的回归线

R ggplot2:绘制具有不同截距但具有相同坡度的回归线,r,ggplot2,statistics,regression,linear-regression,R,Ggplot2,Statistics,Regression,Linear Regression,我想用不同的截距绘制回归线,但斜率相同 使用以下ggplot2code,我可以绘制具有不同截距和不同斜率的回归线。但却不知道如何绘制具有不同截距但斜率相同的回归线 library(ggplot2) ggplot(data=df3, mapping=aes(x=Income, y=Consumption, color=Gender)) + geom_point() + geom_smooth(data=df3, method = "lm", se=FALSE, mapping=a

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

使用以下
ggplot2
code,我可以绘制具有不同截距和不同斜率的回归线。但却不知道如何绘制具有不同截距但斜率相同的回归线

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
库(ggplot2)
ggplot(数据=df3,映射=aes(x=收入,y=消费,颜色=性别))+geom_point()
geom_smooth(数据=df3,方法=“lm”,se=假,映射=aes(x=收入,y=消费))

消耗量为什么不使用
lm
的结果计算ggplot之外的回归:

# 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))
#具有相同斜率但不同性别截距的回归

fm1为什么不添加一条带有
lm
结果的
geom_线
,即在ggplot之外进行计算?从技术上讲,正如您在模型中看到的,没有两个不同的截取,但是虚拟
GenderFemale
的额外偏移量可能是一个统计问题。通过使用color=Gender,ggplot代码所做的是为每个性别创建一个线性回归模型。因此,模型将确定它们是否具有相同的斜率和/或相同的截距。如果它恰好具有相同的坡度和不同的截距,请确保将绘制它。为什么要对两性强制相同的斜率?您可以使用
predict
lm1
对象来简化这一点,或者使用新的数据集,或者直接将预测添加到原始数据集中,以绘制运行
fm1s@bpace的实用程序感谢您指出,在我实现了aosmith建议的更改之后,这个没有任何用处。