没有得到线性回归图

Not getting Linear Regression Plot

我正在尝试绘制数据集中两个参数之间的线性回归。但是我做不到,我收到一个错误

Error in plot.default(yh, r, xlab = l.fit, ylab = "Residuals", main = main, : formal argument "xlab" matched by multiple actual arguments

这是我的代码

file <- "bank.csv"
data <- read.csv(file, header=TRUE, sep=";")
data <- data[(data$Previous_Outcome == "success") | (data$Previous_Outcome == "nonexistent"),]
data <- data[(data$Duration != "0"),]
age = data$Age
duration <- data$Duration
fit <- lm(age ~ duration)
png(filename = "AgevsDurationRegression.png", width=480, height=480, units="px")
> plot(fit, main="Age vs Call Duration Regression", xlab = "Duration in Seconds", ylab = "Age in Years")
Error in plot.default(yh, r, xlab = l.fit, ylab = "Residuals", main = main,  : 
  formal argument "xlab" matched by multiple actual arguments

str(data) 和 summary(fit) 给了我这个

summary(fit)

Call:
lm(formula = age ~ duration)

Residuals:
    Min      1Q  Median      3Q     Max 
-23.063  -8.045  -2.027   6.956  58.007 

Coefficients:
              Estimate Std. Error t value Pr(>|t|)    
(Intercept) 40.0792439  0.0752589 532.551   <2e-16 ***
duration    -0.0001804  0.0002040  -0.884    0.377    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 10.28 on 36930 degrees of freedom
Multiple R-squared:  2.117e-05, Adjusted R-squared:  -5.903e-06 
F-statistic: 0.782 on 1 and 36930 DF,  p-value: 0.3765

这有效

file <- "bank.csv"
data <- read.csv(file, header=TRUE, sep=";")
data <- data[(data$Previous_Outcome == "success") | (data$Previous_Outcome == "nonexistent"),]
data <- data[(data$Duration != "0"),]
age = data$Age
duration <- data$Duration
png("file1.png")
plot(duration~age)
abline(lm(duration~age))
dev.off()

plot(y~x) 和 plot(lm(y~x)) 的区别:

plot(y~x) 给出变量 y Vs x 的散点图。和 abline(lm(y~x)) 向绘图添加回归线。

plot(lm(y~x)) 提供回归模型的回归诊断图。 它为给定的回归模型生成六个诊断图。

  • 拟合值的残差
  • Sqrt(残差)相对于拟合值
  • 正常Q-Q图
  • 库克的距离与行标签
  • 杠杆的残差
  • 库克对杠杆的距离/(1-杠杆)。

默认提供前三个和前五个。

按回车键逐一查看这些地块。 或者要在一个 window 中查看所有四个图,请使用以下命令。它将绘图区域划分为2*2的网格。

  par(mfrow=c(2,2))

要以 png 格式保存任何绘图:

  png(file="myplot", width=400, height=350)
  dev.off()

同理,可以使用

  bmp(file="myplot", width=400, height=350); 
  jpeg(file="myplot", width=400, height=350) or
  tiff(file="myplot", width=400, height=350) 

以其他格式保存任何图表的功能。