Sean Murphy
Sean Murphy

Reputation: 1247

Adding error variance to output of predict()

I am attempting to take a linear model fitted to empirical data, eg:

set.seed(1)
x <- seq(from = 0, to = 1, by = .01)
y <- x + .25*rnorm(101)
model <- (lm(y ~ x))
summary(model)
# R^2 is .6208

Now, what I would like to do is use the predict function (or something similar) to create, from x, a vector y of predicted values that shares the error of the original relationship between x and y. Using predict alone gives perfectly fitted values, so R^2 is 1 e.g:

y2 <- predict(model)
summary(lm(y2 ~ x))
# R^2 is 1

I know that I can use predict(model, se.fit = TRUE) to get the standard errors of the prediction, but I haven't found an option to incorporate those into the prediction itself, nor do I know exactly how to incorporate these standard errors into the predicted values to give the correct amount of error.

Hopefully someone here can point me in the right direction!

Upvotes: 1

Views: 1793

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226027

How about simulate(model) ?

set.seed(1)
x <- seq(from = 0, to = 1, by = .01)
y <- x + .25*rnorm(101)
model <- (lm(y ~ x))
y2 <- predict(model)
y3 <- simulate(model)
matplot(x,cbind(y,y2,y3),pch=1,col=1:3)

If you need to do it it by hand you could use

y4 <- rnorm(nobs(model),mean=predict(model),
            sd=summary(model)$sigma)

Upvotes: 2

Related Questions