In: Statistics and Probability
Data: Linear Regression
x=c(1,4,8,19,6,20)
y=c(32,13,67,18,15,29)
Using R, Plot the 95% prediction and confidence bands. How would you plot these bands? Can you please provide an example picture of the plot?
The following R code can be used to plot the 95% confidence bands and the 95% prediction bands respectively.
**********************************************************************************************************************************************
x=c(1,4,8,19,6,20)
y=c(32,13,67,18,15,29)
fit = lm(y ~ x)
#### plot 95% confidence band:
newx = seq(min(x), max(x), length.out=100)
preds = predict(mod, newdata = data.frame(x=newx), interval =
'confidence')
plot(y ~ x, type = 'n')
polygon(c(rev(newx), newx), c(rev(preds[ ,3]), preds[ ,2]), col
= 'grey90', border = NA) ### adding fill to the plot
abline(mod) ## plotting the model in the graph
lines(newx, preds[ ,3], lty = 'dashed', col = 'red') ## plotting
intervals
lines(newx, preds[ ,2], lty = 'dashed', col = 'red') ## plotting
intervals
*********************************************
x=c(1,4,8,19,6,20)
y=c(32,13,67,18,15,29)
fit = lm(y ~ x)
#### plot 95% prediction band:
newx = seq(min(x), max(x), length.out=100)
preds = predict(fit, newdata = data.frame(x=newx), interval =
'prediction')
plot(y ~ x, type = 'n')
polygon(c(rev(newx), newx), c(rev(preds[ ,3]), preds[ ,2]), col
= 'grey90', border = NA) ### adding fill to the plot
abline(fit) ## plotting the model in the graph
lines(newx, preds[ ,3], lty = 'dashed', col = 'red') ## plotting
intervals
lines(newx, preds[ ,2], lty = 'dashed', col = 'red') ## plotting
intervals
*********************************************************************************************************************************************
PLEASE COMMENT ON ANY FURTHER QUERIES REGARDING THIS CODE. PLEASE UPVOTE.