CHIA
CHIA

Reputation: 71

How to plot the confidence interval of times series in R?

I have some problems about drawing confidence interval for time series in R. The data is down below...

dat <- as.data.frame(cbind(time=c(1980:2019), value=rnorm(40)))
head(dat)
  time      value
1 1980 -1.7196037
2 1981 -0.8135920
3 1982 -0.7451891
4 1983  0.7011315
5 1984  0.5657109

I transformed the "value" become time series,

ts(dat$value, start=1980, end=2019)

but when I try to plot the confidence intervals, it doesn't work,

ggplot(dat, aes(x=time, y=value)) + 
  geom_line() + geom_hline(yintercept=0) + 
  geom_ribbon(aes(ymin=mean(value)-sd(value), 
                  ymax=mean(value)+sd(value)), alpha=0.05)

but it returned... enter image description here

Can someone helps me, thanks.

Upvotes: 0

Views: 745

Answers (1)

Dan Adams
Dan Adams

Reputation: 5204

First of all, I'm not sure why you converted to ts as {ggplot2} can't use it and you didn't assign it back to dat in any case.

Also in your data you have only one y value at each x value. Therefore it doesn't make sense to calculate any error or confidence interval. For example sd(1) returns NA.

But the main issue is that you are calculating your ribbon off of all the data points so it's just one big rectangle.

If you have multiple y values at each x value, then you can use stat_summary() to calculate the mean and error as desired. See example below:

library(tidyverse)

d <- data.frame(x = rep(1:5, 5), y = runif(25))

d %>% 
  ggplot(aes(x, y)) +
  geom_point() +
  geom_line(stat = "summary", size = 2) +
  stat_summary(fun = mean,
               fun.min = function(x) mean(x) - sd(x), 
               fun.max = function(x) mean(x) + sd(x), 
               geom = "ribbon", 
               alpha = 0.5)

Created on 2022-04-07 by the reprex package (v2.0.1)

Upvotes: 3

Related Questions