Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Predicted vs. Actual plot

I'm new to R and statistics and haven't been able to figure out how one would go about plotting predicted values vs. Actual values after running a multiple linear regression. I have come across similar questions (just haven't been able to understand the code). I would greatly appreciate it if you explain the code. This is what I have done so far:

# Attach file containing variables and responses
q <- read.csv("C:/Users/A/Documents/Design.csv")
attach(q)
# Run a linear regression
model <- lm(qo~P+P1+P4+I)
# Summary of linear regression results
summary(model)

The plot of predicted vs. actual is so I can graphically see how well my regression fits on my actual data.

like image 384
John Avatar asked Aug 29 '16 14:08

John


2 Answers

It would be better if you provided a reproducible example, but here's an example I made up:

set.seed(101)
dd <- data.frame(x=rnorm(100),y=rnorm(100),
                 z=rnorm(100))
dd$w <- with(dd,
     rnorm(100,mean=x+2*y+z,sd=0.5))

It's (much) better to use the data argument -- you should almost never use attach() ..

 m <- lm(w~x+y+z,dd)
 plot(predict(m),dd$w,
      xlab="predicted",ylab="actual")
 abline(a=0,b=1)

enter image description here

like image 80
Ben Bolker Avatar answered Nov 05 '22 22:11

Ben Bolker


Besides predicted vs actual plot, you can get an additional set of plots which help you to visually assess the goodness of fit.

--- execute previous code by Ben Bolker ---

par(mfrow = c(2, 2))
plot(m)

enter image description here

like image 11
Lourdes Hernández Avatar answered Nov 05 '22 20:11

Lourdes Hernández