gpt4 book ai didi

r - ggplot2:绘制具有不同截距但具有相同斜率的回归线

转载 作者:行者123 更新时间:2023-12-03 14:04:56 27 4
gpt4 key购买 nike

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

与以下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

最佳答案

为什么不用 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))

产生相同的斜率和不同的截距:
enter image description here

从技术上讲,正如您在模型中看到的,没有两个不同的截距,而是对虚拟变量 GenderFemale 的额外偏移量。 .

编辑:包括 predict为了简化,感谢@aosmith 的建议。

关于r - ggplot2:绘制具有不同截距但具有相同斜率的回归线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31903606/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com