library(ggplot2) library(tidyr) library(gridExtra) library(rstanarm) library(brms) library(bayesplot) theme_set(bayesplot::theme_default(base_family = "sans", base_size = 16)) library(patchwork) library(loo) library(rprojroot) root<-has_file(".BDA_R_demos_root")$make_fix_file()
This notebook demonstrates time series analysis for traffic deaths per year in Finland. Currently when the the number of traffic deaths during previous year are reported, the press release claims that the the traffic safety in Finland has improved or worsened depending whether the number is smaller or larger than the year before. Time series analysis can be used to separate random fluctuation from the slowly changing traffic safety.
Read the data (there would data for earlier years, too, but this is sufficient for the demonstration)
# file preview shows a header row deaths <- read.csv(root("demos_rstan", "trafficdeaths.csv"), header = TRUE) head(deaths)
## year deaths ## 1 1993 434 ## 2 1994 423 ## 3 1995 411 ## 4 1996 355 ## 5 1997 391 ## 6 1998 367
First plot just the data.
deaths |> ggplot(aes(x=year, y=deaths)) + geom_point() + labs(y = 'Traffic deaths', x= "Year") + guides(linetype = "none")