user1140126
user1140126

Reputation: 2649

How to draw the regression line and the scatterplot between observed and predicted in R

How can I draw observed vs predicted scatterplot in R? I also want to show the regression line in it.

library(car)
summary(Prestige)
head(Prestige)
testidx <- which(1:nrow(Prestige)%%4==0)
prestige_train <- Prestige[-testidx,]
prestige_test <- Prestige[testidx,]

model <- glm(prestige~., data=prestige_train)
# Use the model to predict the output of test data 
prediction <- predict(model, newdata=prestige_test)
# Check for the correlation with actual result
plot(prediction, prestige_test$prestige)

Edit:

abline(model)

The above function abline does not seem to work. It does draw a line but it seem to be incorrect.

Thanks

Upvotes: 2

Views: 3287

Answers (1)

Maxim.K
Maxim.K

Reputation: 4180

I don't think there is much need for me to write out the answer in its entirety, when there exists a much more beautiful (and probably more informed) explanation by Hadley Wickham. It is to be found here (first part) and here (second, more advanced part).

EDIT: My apologies for not giving a more specific answer from the start. Here how the plot can be made:

# define training sample
sample <- sample(nrow(Prestige),40,replace=FALSE)

training.set <- Prestige[sample,]
test.set <- Prestige[-sample,]
model <- glm(prestige ~ .,data=training.set)
# getting predicted values
test.set$predicted <- predict(model,newdata=test.set)

# plotting with base plot 
with(test.set,plot(prestige,predicted))
# plotting with ggplot2
require(ggplot2)
qplot(prestige,predicted,data=test.set)

Hope this answers your question.

P.S. The question is indeed more appropriate for SO, it would have been answered there in a matter of minutes :)

Upvotes: 2

Related Questions