2
votes

I am trying to plot linear regression between two parameters from my data set. But I am not able to do so, I am getting an error

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

Here is my code

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) and summary(fit) gave me this

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
2
have you tried only plot(fit)? It will plot for 4 graphics not only one.MLavoie
I tried that. It just opened a blank window and nothing happened. I also want to save the result as an image. I am failing in both.Ankur Sinha
I gave. I just did not post the result for str(data) because it was too long. But the result for str(data) seems fine to me. I will post it if you still want to see.Ankur Sinha
o.k.. But you are using plot.lm(). this function has no parameter xlab=. Use plot(age ~ duration) (or plot(duration, age)) and after fitting abline(fit).jogo
just add a reproductible exampleMLavoie

2 Answers

3
votes

This worked

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()
0
votes

Difference between plot(y~x) and plot(lm(y~x)):

plot(y~x) gives a scatter plot of variables y Vs x. and abline(lm(y~x)) adds a regression line to the plot.

plot(lm(y~x)) provides regression diagnostic plots for regression model. It produces six diagnostic plots for given regression model.

  • Residuals against fitted values
  • Sqrt (residuals)  against fitted values
  • Normal Q-Q plot
  • Cook's distances versus row labels
  • Residuals against leverages
  • Cook's distances against leverage/(1-leverage).

By default, the first three and 5 are provided.

Hit enter to see these plots one by one. Or to see all four plots in one window, use the following command. It divides plot area into 2*2 grid.

  par(mfrow=c(2,2))

To save any plot in png format:

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

Similary, you can use

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

functions to save any plot in other formats.