In R, I am trying to overlay an abline onto a plot, the result of linear regression. I want to create a scatter plot showing
TrainRegRpt$train.data.Price
(original price) on the x-axis,
TrainRegress$fitted.values
(the projected price that came from the lm model) on the y-axis and draw the line of best fit through the plotted points.
Here is some of my code:
TrainRegress <- lm(PriceBH.df$Price ~ ., data=PriceBH.df, subset = train.rows)
TrainRegRpt <- data.frame(train.data$Price, TrainRegress$fitted.values, TrainRegress$residuals)
x <- as.vector(TrainRegRpt$TrainRegress.fitted.values) # on the x-axis
y <- as.vector(TrainRegRpt$train.data.Price) #on the y-axis
plot(TrainRegRpt$train.data.Price ~ TrainRegRpt$TrainRegress.fitted.values)
abline(x,y)
The scatter plot came out the same:
x <- as.vector(newdf$fv)
y <- as.vector(newdf$p)
p <-as.vector(TrainRegRpt$train.data.Price) # my y-axis in the scatter plot
fv <- as.vector(round(TrainRegRpt$TrainRegress.fitted.values,2) # my y-axis in the scatter plot
newdf<- dfrm <- data.frame(p,fv)
plot(newdf$p ~ newdf$fv)
abline(x,y)
summary(TrainRegress)
The following is the summary of TrainRegress: Coefficients obtained from the summary of TrainRegress:
Intercept Estimate
................30.318
CRIM.........0.245
CHAS......5.8368
RM..........8.4846
I extracted the y-intercept as follows:
y.interceptval <-summary(TrainRegress)$coefficients[1]
I will use y.interceptval
in the abline(y.interceptval,***?slope***)
but I need to know how to calculate the slope. How do I calculate the slope to pass to abline(y.interceptval, slope)
?
I have 5 textbooks here that are no help and my professor refuses to help me and I really want this to be perfect! Thank you!!!
plot(TrainRegRpt$train.data.Price ~ TrainRegRpt$TrainRegress.fitted.values)<br>
abline(x,y)