![](/img/trans.png)
[英]How to add a regression line with linear and quadratic components in ggplot?
[英]How to add a multiple linear regression line in ggplot?
剛剛有一個關於在 ggplot 中添加多元線性回歸線的快速問題。 到目前為止,這是我的代碼:
data(teengamb, package='faraway')
attach(teengamb)
lmod=lm(gamble~income+sex)
formula=4.041+5.172*income+-21.634*sex
formula_1=append(formula, 4.041, 0)
formula_1_df=data.frame(MLR=formula_1, Participant=c(0:47), sex=append(sex, 0, 0), income=append(income, 0, 0))
formula_1_df %>%
ggplot(aes(Participant, MLR))+geom_point(aes(color=sex))
我的多元線性回歸線是 4.041 + 5.172*(收入)+ -21.634*(性別)。 我將如何將此行添加到我的 ggplot 中?
另外,由於sex
是一個二元變量,我是否可以將其添加為預測變量?
我附上了一張圖表的圖片。
我想通了我做錯了什么。 formula_1
和formula
是正確的,分別生成以下圖表。
然后添加行:
出於某種原因,我認為公式線會是線性的,但它實際上是一種沖擊 Y 型線。
因此,通過添加geom_line(data=data.frame(MLR, Participant), col="purple")
,我得到了正確的答案,即震驚圖。
data(teengamb, package='faraway')
attach(teengamb)
lmod=lm(gamble~income+sex)
formula=4.041+5.172*income+-21.634*sex
formula_1=append(formula, 4.041, 0)
formula_1_df=data.frame(MLR=formula_1, Participant=c(0:47), sex=append(sex, 0, 0), income=append(income, 0, 0))
formula_1_df %>%
ggplot(aes(Participant, MLR))+geom_point(aes(color=sex))+geom_line(data=data.frame(MLR, Participant), col="purple")
給我:
我認為這是正確的答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.