【发布时间】:2015-11-01 10:13:18
【问题描述】:
我想绘制具有不同截距但斜率相同的回归线。
使用以下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))
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
【问题讨论】:
-
为什么不将
geom_line与lm的结果相加,即在ggplot 之外进行计算?从技术上讲,正如您在模型中看到的那样,没有两个不同的截距,而是对虚拟GenderFemale的额外偏移 -
可能是统计问题。您的 ggplot 代码使用 color=Gender 所做的是为每个性别创建一个线性回归模型。因此,模型将确定它们是否具有相同的斜率和/或相同的截距。如果它碰巧实际上具有相同的斜率和不同的截距,请确保将其绘制出来。为什么要为两种性别强制使用相同的斜率?
标签: r ggplot2 statistics regression linear-regression