使用 ggplot 绘制最佳拟合线以在 r

问题描述

对于下面的数据集,我想运行一个加性回归模型,对压力和影响进行回归回归。然后,我需要在样本上绘制预测(拟合线),对 X 轴产生影响。应力的平均值可以用作最佳拟合线的常数值。

我当前的代码如下所示:

model.add <- lm(withdraw ~ estress + affect,data = estress.dat) 

library(ggplot2)
estress.dat$addFit <- predict(model.add,newdata = estress.dat)
ggplot(estress.dat,aes(x = affect,y = estress,color = withdraw)) + 
  geom_point() +
geom_line(aes(y = addFit),size = 1) + 
  theme_bw()

然而,我得到的情节看起来非常奇怪,有锯齿状的线条。所以我想我可能做错了。任何帮助纠正我的代码将不胜感激!

enter image description here

我的数据示例:

structure(list(tenure = c(1.67,0.58,2,5,9,2.5,0.5,0.58),estress = c(6,5.5,3,4.5,6,6),affect = c(2.6,1,2.4,1.16,1.5,1.33,3),withdraw = c(3,3.66,4.66,4.33,4),sex = structure(c(2L,1L,2L,2L),.Label = c("female","male"),class = "factor"),age = c(51L,45L,42L,50L,48L,51L,47L,40L,43L),ese = c(5.33,6.05,5.26,4.35,4.86,5.05,6.13,addFit = c(2.98571051159875,1.83195137430683,2.87030819668908,2.10870572064622,1.87037481064259,2.13966887974888,1.79352793797107,2.04734042752603,3.29336201408961)),row.names = c(NA,10L),class = "data.frame")

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)