Read in data:
atmruns<-read.table(header=TRUE, text="atms minutes
200 5.8
200 35.9
200 20.9
200 35.4
200 10.2
200 24.5
200 16.6
400 86.1
400 59.6
400 64.9
400 61.2
400 54.3
400 96.8
400 55.8
600 114
600 82.9
600 74
600 88.4
600 75.3
600 113.3
600 93.9
800 119.1
800 131.3
800 122
800 120
800 127
800 139.4
800 149.9
1000 175.1
1000 165.8
1000 214.4
1000 166.6
1000 145.4
1000 185
1000 160.2
")
head(atmruns)
Fit model and look at results
fit<-lm(minutes~atms,data=atmruns)
summary(fit)
Call:
lm(formula = minutes ~ atms, data = atmruns)
Residuals:
Min 1Q Median 3Q Max
-24.526 -10.566 -2.986 8.774 44.474
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -12.674286 6.389752 -1.984 0.0557 .
atms 0.182600 0.009633 18.956 <2e-16 ***
---
Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
Residual standard error: 16.12 on 33 degrees of freedom
Multiple R-squared: 0.9159, Adjusted R-squared: 0.9133
F-statistic: 359.3 on 1 and 33 DF, p-value: < 2.2e-16
plot the data:
plot(minutes~atms,data=atmruns)
Improving look of plot and adding fit line
plot(minutes~atms,data=atmruns,cex=1.5,cex.axis=1.5,cex.lab=1.5,pch=20)
abline(fit,col="blue",lwd=2)
Examine Results:
plot(fit)
Alternative method for graph enhancements:
Save function to extract equation for plot:
equation = function(x) {
lm_coef <- list(a = round(coef(x)[1], digits = 2),
b = round(coef(x)[2], digits = 2),
r2 = round(summary(x)$r.squared, digits = 2));
lm_eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(R)^2~"="~r2,lm_coef)
as.character(as.expression(lm_eq));
}
Add ggplot2 library
library(ggplot2);
Create Scatterplot
ggplot(atmruns, aes(x=atms, y=minutes)) +
geom_point() +
geom_smooth(method=lm, se=FALSE) +
ggtitle("Optimization Time for ATM Program") +
annotate("rect", xmin = 615, xmax = 1025, ymin = 25, ymax = 50, fill="white", colour="red") +
annotate("text", x = 820, y = 39, label = equation(fit), parse = TRUE)
NA