Reputation: 59
for example, if I have a dataset with X
and Y
, I know that I can find the confident interval of the linear model by confint(lm(Y~X))
But I am wondering if there is a way for me to find the CI for Y
given that X = somevalue
?
Upvotes: 0
Views: 28
Reputation: 374
here is a solution
note: replace confidence with prediction if you want prediction interval
x <- 1:10
y <- 1:10 + 5 + runif(10)
model <- lm(y~x)
predict(model, newdata = data.frame(x = 1.5), interval = "confidence")
output>
fit lwr upr
1 7.164623 6.746502 7.582743
or multiple datapoints
predict(model, newdata = data.frame(x = c(1.5,2.5,10)), interval = "confidence")
output>
fit lwr upr
1 7.164623 6.746502 7.582743
2 8.139339 7.786692 8.491986
3 15.449707 14.996426 15.902989
or fitted values
predict(model,interval = "confidence")
output>
fit lwr upr
1 6.677265 6.223983 7.130546
2 7.651981 7.267546 8.036415
3 8.626696 8.303379 8.950014
4 9.601412 9.326281 9.876544
5 10.576128 10.328583 10.823674
6 11.550844 11.303298 11.798389
7 12.525560 12.250428 12.800691
8 13.500276 13.176958 13.823593
9 14.474991 14.090557 14.859426
10 15.449707 14.996426 15.902989
Upvotes: 2