1

I have a time series of 190 observations, I am using the first 180 to perform Kalman filter and the last 10 to compare different models forecatsing. I am trying to plot a data of the last 20 observations of my dataset, from 170 to 190, and then the forecasted level from 181 to 190 together with their confidence interval using ggplot. So in total I have 4 time series: the observations with 20 elements, the forecasts and the two confidence intervals with 10 elements each.

These are the data

track_28_only_95bpm$bpm[170:190]
[1] 154.2031 150.0625 158.8750 153.0000 147.1250 148.1797 151.0000 150.0000 
153.8125 155.0000
[11] 151.7375 157.1875 155.7500 160.2500 151.3906 149.0000 149.7500 155.1328 
163.0000 162.7500
[21] 160.0000

f_compl[181:190]
152.3169 156.2046 155.8417 159.3604 152.9990 149.8070 149.7615 154.0488 
161.1935 162.4359

upper_compl[181:190]
160.9422 164.8298 164.4670 167.9856 161.6243 158.4323 158.3868 162.6741 
169.8188 171.0612

lower_compl[181:190]
143.6917 147.5793 147.2165 150.7351 144.3737 141.1818 141.1362 145.4235 
152.5683 153.8106

Here is my code.

df_obs_compl = data.frame(time = seq(170,190,length=21),
                      M = track_62_only_95bpm$bpm[170:190], legend = 
                      "observations")
df_f = data.frame(time = seq(181,190,length=10), M = f_compl[181:190], 
                     legend = "forecast")
df_u_compl = data.frame(time = seq(181,190,length=10), M = 
                        upper_compl[181:190] ,legend = "upper")
df_l_compl = data.frame(time = seq(181,190,length=10), M = 
                        lower_compl[181:190], legend = "lower")
df_compl = rbind(df_obs_compl, df_f, df_u_compl, df_l_compl)

ggplot(df_compl, aes(x = time, y = M, color = legend)) + geom_line() + 
        scale_colour_manual(values=c(observations='black', one_step='red', 
        upper='blue', lower='blue')) 

With this I am able to achieve what I was saying, i.e. I plot 20 observations together with 10 forecasts and their confidence interval. Now, I'd like the confidence interval to be shaded, and I have tried the following piece of code.

ggplot(df, aes(x = time, y = M)) + geom_line(colour='black') +
            geom_smooth(aes(x=time, y=M, ymax=upper, ymin=lower), 
            colour='red')      

However I get the following error

 Error: Aesthetics must be either length 1 or the same as the data (51): x, 
 y, ymax, ymin

Any idea how I can fix this? Or any other method to obtain shaded confidence intervals for the forecasts?

Mr James
  • 21
  • 2
  • Please make your example [minimal & reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). I imagine `geom_ribbon` is what you're after but it'd be much easier to help if you were to provide sample data. – Maurits Evers May 30 '18 at 21:19
  • Datasets added, I hope in the correct format. I already tried with geom_ribbon, but I got the same error message. Could you provide the full code? – Mr James May 30 '18 at 22:54

0 Answers0