Abline 命令没有显示回归线?

Abline command is not showing a regression line?

我是 R 编程的新手,我正在尝试为该数据集绘制回归线,但它似乎不起作用。
我完全按照我的教授使用的方法进行操作,但它似乎不起作用。我还将 abline 命令与 abline(lm(batters$EMH~batters$TB)) 互换,结果相似。

这是我的代码:

batters<-read.table(header=TRUE, text="
X   AVG EBH  TB   OPS K.to.BB.Ratio
1     LeMahieu 0.327  61 312 0.893          1.95
2      Urshela 0.314  55 236 0.889          3.48
3       Torres 0.278  64 292 0.871          2.64
4        Judge 0.272  46 204 0.921          2.21
5      Sanchez 0.232  47 208 0.841          3.13
6         Wong 0.285  40 202 0.784          1.76
7       Molina 0.270  34 167 0.711          2.52
8  Goldschmidt 0.260  60 284 0.821          2.13
9        Ozuna 0.243  53 230 0.804          1.84
10      DeJong 0.233  62 259 0.762          2.39
11      Altuve 0.298  61 275 0.903          1.98
12     Bregman 0.296  80 328 1.015          0.69
13    Springer 0.292  62 283 0.974          1.69
14     Reddick 0.275  36 205 0.728          1.83
15    Chirinos 0.238  40 162 0.791          2.45
16   Bellinger 0.305  84 351 1.035          1.14
17      Turner 0.290  51 244 0.881          1.72
18      Seager 0.272  64 236 0.817          2.23
19      Taylor 0.262  45 169 0.794          3.11
20       Muncy 0.251  58 251 0.889          1.65
21     Meadows 0.291  69 296 0.922          2.43
22      Garcia 0.282  47 227 0.796          4.03
23        Pham 0.273  56 255 0.818          1.52
24        Choi 0.261  41 188 0.822          1.69
25      Adames 0.254  46 222 0.735          3.32
26      Yelich 0.329  76 328 1.101          1.48
27       Braun 0.285  55 232 0.849          3.09
28   Moustakas 0.254  66 270 0.845          1.85
29     Grandal 0.246  56 240 0.848          1.28
30       Arcia 0.223  32 173 0.633          2.53")

plot(batters$EBH,batters$TB,main="Attribute Pairing 5",xlab="EBH",ylab="TB")
lm(formula = batters$EBH~batters$TB)
#Call:
#lm(formula = batters$EBH ~ batters$TB)

#Coefficients:
#(Intercept)   batters$TB  
#    -4.1275       0.2416  

lin_model_1<-lm(formula = batters$EBH~batters$TB)
summary(lin_model_1)

abline(-4.12752, 0.24162)

我为混乱的编码道歉,这是为了 class。

您的公式在 lm() 函数调用中是反向的。因变量在“~”的左边。
在您的图中,y 轴(因变量)是 TB,但在线性回归模型中,它被定义为自变量。因此,为了使线性回归模型起作用,需要交换 EBH 和 TB。

plot(batters$EBH,batters$TB,main="Attribute Pairing 5",xlab="EBH",ylab="TB")
model <-lm(formula = batters$TB ~batters$EBH)

model

Call: lm(formula = batters$TB ~ batters$EBH)

Coefficients: (Intercept) batters$EBH
46.510 3.603

 abline(model)
 #or
 abline (46.51, 3.60)

此外,如果将“模型”传递给 abline,则可以避免使用 abline

指定斜率和截距的需要