-->

ggvis scatter plot with 95% prediction interval

2019-07-04 06:45发布

问题:

library(ggvis)
mtcars %>% 
  ggvis(~wt, ~mpg) %>%
  layer_points() %>%
  layer_model_predictions(model = "lm", se = TRUE)

The above produces a scatter plot with a fitted regression line and 95% confidence limits on .

Question: How to draw a scatter plot with a fitted regression line and 95% prediction limits on ?

回答1:

Here is an idea. It probably needs more work to get exactly what you're after though.

mtcars.pi = data.frame(mtcars, predict(lm(mpg~wt,data=mtcars), interval="prediction"))
mtcars.pi %>% 
ggvis(~wt, ~mpg) %>%
layer_points() %>%
layer_ribbons(y=~lwr, y2=~upr, opacity:=.5) %>%
layer_model_predictions(model = "lm", se = TRUE)