如何找到残差并绘制它们

机器算法验证 r 回归
2022-02-05 07:56:14

我得到了数据

x = c(21,34,6,47,10,49,23,32,12,16,29,49,28,8,57,9,31,10,21,26,31,52,21,8,18,5,18,26,27,26,32,2,59,58,19,14,16,9,23,28,34,70,69,54,39,9,21,54,26)
y = c(47,76,33,78,62,78,33,64,83,67,61,85,46,53,55,71,59,41,82,56,39,89,31,43,29,55, 
     81,82,82,85,59,74,80,88,29,58,71,60,86,91,72,89,80,84,54,71,75,84,79)

如何获得残差并将它们与x? 我如何测试残差是否看起来大致正常?

我不确定我是否正确地进行了原始线性拟合,因为我得到了方程y=6.9x5.5但是讲义说线性回归线应该是形式yi=β0+β1x+ϵ.

1个回答

编辑:您有一个R标签,但在评论中说您对此了解不多。这是R代码。我对圣人一无所知。结束编辑

你可以这样做

x = c(21,34,6,47,10,49,23,32,12,16,29,49,28,8,57,9,31,10,21,
      26,31,52,21,8,18,5,18,26,27,26,32,2,59,58,19,14,16,9,23,
      28,34,70,69,54,39,9,21,54,26)
y = c(47,76,33,78,62,78,33,64,83,67,61,85,46,53,55,71,59,41,82,
      56,39,89,31,43,29,55, 81,82,82,85,59,74,80,88,29,58,71,60,
      86,91,72,89,80,84,54,71,75,84,79)

m1 <- lm(y~x)  #Create a linear model
resid(m1) #List of residuals
plot(density(resid(m1))) #A density plot
qqnorm(resid(m1)) # A quantile normal plot - good for checking normality
qqline(resid(m1))