data(cars)
main="Scatterplot of Speed vs Distance",
xlab="Speed",
ylab="Stopping Distance",
col="blue",
pch=19)
set.seed(10)
index <- sample(1:nrow(cars), 0.7*nrow(cars))
train_data <- cars[index, ]
test_data <- cars[-index, ]
model <- lm(dist ~ speed, data=train_data)
summary(model)
coef(model)
predicted_values <- predict(model, newdata=test_data)
plot(train_data$speed, train_data$dist,
main="Training Data with Regression Line",
xlab="Speed",
ylab="Distance",
col="black")
abline(model, col="red", lwd=2)
plot(test_data$speed, test_data$dist,
main="Test Data Prediction",
xlab="Speed",
ylab="Distance",
col="blue")
points(test_data$speed, predicted_values,col="red", pch=19)
plot(model, which=1)
plot(model, which=2)
