Confidence interval for linear regression - r

I need to create confidence interval for linear regression using R-lang. I followed a few tutorials, yet my result is quite different. As far as I am concerned, I should get two lines, one above and one below the main line, as shown here.
Unfortunately what I got is a few stacked lines, as shown here.
Could anyone help me to understand what am I doing wrong?
Here's sample of my code:
speed <- c(61,225,110,51,114,68,24,24,133,83,83,92,93,37,111,172,142,105,143,77,154,108,98,164,124,97,90,87,137,71,73,74,62,88,100,101,126,113,49)
length <- c(58,149,90,55,91,69,31,35,109,77,78,82,86,44,89,121,106,98,116,65,111,88,86,122,104,85,72,80,105,74,71,66,73,72,72,90,91,98,59);
cars <- data.frame(speed, length)
modelReg <- lm(length ~ speed, data = cars)
x <- cars$speed
conf_interval <- predict(modelReg, newdata = data.frame(seq(from=min(x),to=max(x),by = 1)),interval = 'confidence')
lines(x,conf_interval[,2],lty=2)
lines(x,conf_interval[,3],lty=2)

After the first four lines of your code above, use Gosink's plot.add.ci function:
# John Gosink's Intervals Plotter (from http://gosink.org/?page_id=120)
plot.add.ci <- function(x, y, interval='prediction', level=0.9,
regressionColor='red', ...) {
xOrder <- order(x)
x <- x[xOrder]
y <- y[xOrder]
fit <- lm(y ~ x, data=data.frame(x=x, y=y))
newX <- data.frame(x=jitter(x))
fitPred <- predict.lm(fit,newdata=newX,interval=interval,level=level, ...)
abline(lm(y ~ x), col=regressionColor)
lines(newX$x, fitPred[,2], lty=2, ...)
lines(newX$x, fitPred[,3], lty=2, ...)
}
plot(cars$speed,cars$length)
abline(modelReg,col="red")
plot.add.ci(speed, length, level=0.95, interval="confidence", lwd=3)
Which gives this plot (change level if you want a different confidence level, or drop interval= for a prediction interval):

Related

Issue with plotting logistic regression in R [duplicate]

I am building a quadratic model with lm in R:
y <- data[[1]]
x <- data[[2]]
x2 <- x^2
quadratic.model = lm(y ~ x + x2)
Now I want to display both the predicted values and the actual values on a plot. I tried this:
par(las=1,bty="l")
plot(y~x)
P <- predict(quadratic.model)
lines(x, P)
but the line comes up all squiggely. Maybe it has to do with the fact that it's quadratic? Thanks for any help.
You need order():
P <- predict(quadratic.model)
plot(y~x)
reorder <- order(x)
lines(x[reorder], P[reorder])
My answer here is related: Problems displaying LOESS regression line and confidence interval

How to extrapolate loess result? [duplicate]

I am struggling with "out-of-sample" prediction using loess. I get NA values for new x that are outside the original sample. Can I get these predictions?
x <- c(24,36,48,60,84,120,180)
y <- c(3.94,4.03,4.29,4.30,4.63,4.86,5.02)
lo <- loess(y~x)
x.all <- seq(3, 200, 3)
predict(object = lo, newdata = x.all)
I need to model full yield curve, i.e. interest rates for different maturities.
From the manual page of predict.loess:
When the fit was made using surface = "interpolate" (the default), predict.loess will not extrapolate – so points outside an axis-aligned hypercube enclosing the original data will have missing (NA) predictions and standard errors
If you change the surface parameter to "direct" you can extrapolate values.
For instance, this will work (on a side note: after plotting the prediction, my feeling is that you should increase the span parameter in the loess call a little bit):
lo <- loess(y~x, control=loess.control(surface="direct"))
predict(lo, newdata=x.all)
In addition to nico's answer: I would suggest to fit a gam (which uses penalized regression splines) instead. However, extrapolation is not advisable if you don't have a model based on science.
x <- c(24,36,48,60,84,120,180)
y <- c(3.94,4.03,4.29,4.30,4.63,4.86,5.02)
lo <- loess(y~x, control=loess.control(surface = "direct"))
plot(x.all <- seq(3,200,3),
predict(object = lo,newdata = x.all),
type="l", col="blue")
points(x, y)
library(mgcv)
fit <- gam(y ~ s(x, bs="cr", k=7, fx =FALSE), data = data.frame(x, y))
summary(fit)
lines(x.all, predict(fit, newdata = data.frame(x = x.all)), col="green")

How get plot from nlrq in R?

Following workflow for nonlinear quantile regression seems to work. However I don´t know how to plot the resulting curve.
btw.: I´d prefer to use the function graphics::curve() instead of graphics::lines()
require(quantreg)
# load sample data
dat <- DNase
# introduce variable
x <- DNase$conc
y <- DNase$density
# introduce function
f <- function(a, b, x) {(a*x/(b+x))}
# fit the model
fm0 <- nls(log(y) ~ log(f(a,b,x)), dat, start = c(a = 1, b = 1))
# fit a nonlinear least-square regression
fit <- nls(y ~ f(a,b,x), dat, start = coef(fm0))
# receive coeffientes
co <- coef(fit)
a=co[1]
b=co[2]
# plot
plot(y~x)
# add curve
curve((a*x/(b+x)), add=T)
# then fit the median using nlrq
dat.nlrq <- nlrq(y ~ SSlogis(x, Asym, mid, scal), data=dat, tau=0.5)
# add curve
???
EDIT: What I´m looking for is a way to plot various quantile regressions of a formula, like a*x/(b+x).
Inserting the formula leads me to the question what to put as 'start' argument
dat.nlrq.075 <- nlrq(formula=fit, data = dat, start=???, tau = 0.75)
curve uses lines so there is really no reason to use curve when it's easier to use lines.
First ensure that data is sorted so plots come out right. Then fit with nls or nlrq and use fitted for the fitted line.
library(quantreg)
dat <- DNase[order(DNase$conc), ]
fit.nlrq <- nlrq(density ~ SSlogis(conc, Asym, mid, scal), data = dat, tau = 0.5)
plot(density ~ conc, dat)
lines(fitted(fit.nlrq) ~ conc, dat)
If you want to plot the fit at a different number of equally spaced points such as 250 then do the same except use predict instead of fitted:
x <- seq(min(dat$conc), max(dat$conc), length = 250)
lines(predict(fit.nlrq, list(conc = x)) ~ x, lty = 2, col = "red")
The same style works with nls.
Note that if you use require its value should be checked. If you don't want to do that use library instead.

Non-Linear Modeling with nls in R [duplicate]

I am building a quadratic model with lm in R:
y <- data[[1]]
x <- data[[2]]
x2 <- x^2
quadratic.model = lm(y ~ x + x2)
Now I want to display both the predicted values and the actual values on a plot. I tried this:
par(las=1,bty="l")
plot(y~x)
P <- predict(quadratic.model)
lines(x, P)
but the line comes up all squiggely. Maybe it has to do with the fact that it's quadratic? Thanks for any help.
You need order():
P <- predict(quadratic.model)
plot(y~x)
reorder <- order(x)
lines(x[reorder], P[reorder])
My answer here is related: Problems displaying LOESS regression line and confidence interval

Messy plot when plotting predictions of a polynomial regression using lm() in R

I am building a quadratic model with lm in R:
y <- data[[1]]
x <- data[[2]]
x2 <- x^2
quadratic.model = lm(y ~ x + x2)
Now I want to display both the predicted values and the actual values on a plot. I tried this:
par(las=1,bty="l")
plot(y~x)
P <- predict(quadratic.model)
lines(x, P)
but the line comes up all squiggely. Maybe it has to do with the fact that it's quadratic? Thanks for any help.
You need order():
P <- predict(quadratic.model)
plot(y~x)
reorder <- order(x)
lines(x[reorder], P[reorder])
My answer here is related: Problems displaying LOESS regression line and confidence interval

Resources