0

So I have prediction data from:

predictedParams <- predict(parameters, 
                           list(independentVar = log(median_income)), 
                           interval='prediction', level=0.95)

predictedParamsDataFrame <-  as.data.frame(predictedParams)

abline(independentVar, predictedParamsDataFrame$upr, lwd=4, col = 'red')

but that doesn't work. I've also tried doing this-

uprParams <- lm(independentVar ~ predictedParamsDataFrame$upr)
abline(uprParams, lwd=4, col = 'red')

But that results in a line that is way too high.

What should I do?

jbaums
  • 27,115
  • 5
  • 79
  • 119
praks5432
  • 7,246
  • 32
  • 91
  • 156
  • 3
    Without a reproducible example (not only code, but also sample data) it is hard to say what is going wrong here. See http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example for details regarding how to do that. – Paul Hiemstra Feb 04 '14 at 07:46

2 Answers2

0

abline draws straight lines, so it is not usually what you want to draw for other than the fit of a linear model. Also, it receives as parameters the intercept and slope, which is again not what you have here.

This will get you a matrix with the predicted values and interval:

predictedParams <- predict(parameters, 
                           list(independentVar = log(median_income)), 
                           interval='prediction', level=0.95)

Now you simply need to plot independentVar and the upper/lower interval:

lines(log(median_income), predictedParams[,"upr"], lwd=4, col = 'red')

See that we use the lines function. In the x-axis we use log(median_income) because it is the actual data given to predict, and in the y-axis we use the upper end of the interval predictedParams[,"upr"].

Julián Urbano
  • 8,378
  • 1
  • 30
  • 52
-1

You first need to plot. Then use abline.

plot(concentration, signal)
res <- lm(signal ~ concentration)
abline(res)
Robert
  • 5,278
  • 43
  • 65
  • 115
user3117837
  • 87
  • 1
  • 8