@@ -59,57 +59,44 @@ df <- base::subset(
5959
6060# uses the incidence function from the incidence2 package to compute the
6161# incidence data
62- df_incid <- incidence2::incidence(
63- df,
64- date_index = "date",
65- groups = "sex"
66- ) %>%
62+ df_incid <- incidence2::incidence(df, date_index = "date", groups = "sex") %>%
6763 filter(sex != "unknown")
6864
6965# fit a curve to the incidence data. The model chosen is the negative binomial
7066# distribution with a significance level (alpha) of 0.05.
7167fitted_curve_nb <-
7268 df_incid %>%
7369 nest(.key = "data") %>%
74- mutate(
75- model = lapply(
76- data,
77- function(x) MASS::glm.nb(count ~ date_index, data = x)
78- )
79- )
70+ mutate(model = lapply(
71+ X = data,
72+ FUN = function(x)
73+ MASS::glm.nb(count ~ date_index, data = x)
74+ ))
8075
8176intervals <-
82- fitted_curve_nb %>%
83- mutate(result = Map(
84- function(data, model) {
85- data %>%
86- ciTools::add_ci(
87- model,
88- alpha = 0.05,
89- names = c("lower_ci", "upper_ci")
90- ) %>%
91- as_tibble()
92- },
93- data,
94- model
95- )) %>%
96- select(sex,result) %>%
97- unnest()
77+ fitted_curve_nb %>%
78+ mutate(result = Map(function(data, model) {
79+ data %>%
80+ ciTools::add_ci(model,
81+ alpha = 0.05,
82+ names = c("lower_ci", "upper_ci")) %>%
83+ as_tibble()
84+ }, data, model)) %>%
85+ select(sex, result) %>%
86+ unnest()
9887
9988# plot fitted curve
10089plot(df_incid, angle = 45) +
101- ggplot2::geom_line(
102- ggplot2::aes(date_index, y = pred),
103- data = intervals,
104- inherit.aes = FALSE
105- ) +
106- ggplot2::geom_ribbon(
107- ggplot2::aes(date_index, ymin = lower_ci, ymax = upper_ci),
108- alpha = 0.2,
109- data = intervals,
110- inherit.aes = FALSE,
111- fill = "#BBB67E"
112- ) +
90+ ggplot2::geom_line(ggplot2::aes(date_index, y = pred),
91+ data = intervals,
92+ inherit.aes = FALSE) +
93+ ggplot2::geom_ribbon(
94+ ggplot2::aes(date_index, ymin = lower_ci, ymax = upper_ci),
95+ alpha = 0.2,
96+ data = intervals,
97+ inherit.aes = FALSE,
98+ fill = "#BBB67E"
99+ ) +
113100 ggplot2::labs(x = "Date", y = "Cases")
114101```
115102
@@ -126,45 +113,35 @@ Repeat the above analysis using Poisson distribution.
126113fitted_curve_poisson <-
127114 df_incid %>%
128115 nest(.key = "data") %>%
129- mutate(
130- model = lapply(
131- data,
132- function(x) glm(count ~ date_index, data = x, family = poisson)
133- )
134- )
116+ mutate(model = lapply(data, function(x)
117+ glm(
118+ count ~ date_index, data = x, family = poisson
119+ )))
135120
136121intervals <-
137- fitted_curve_poisson %>%
138- mutate(result = Map(
139- function(data, model) {
140- data %>%
141- ciTools::add_ci(
142- model,
143- alpha = 0.05,
144- names = c("lower_ci", "upper_ci")
145- ) %>%
146- as_tibble()
147- },
148- data,
149- model
150- )) %>%
151- select(sex,result) %>%
152- unnest()
122+ fitted_curve_poisson %>%
123+ mutate(result = Map(function(data, model) {
124+ data %>%
125+ ciTools::add_ci(model,
126+ alpha = 0.05,
127+ names = c("lower_ci", "upper_ci")) %>%
128+ as_tibble()
129+ }, data, model)) %>%
130+ select(sex, result) %>%
131+ unnest()
153132
154133# plot fitted curve
155134plot(df_incid, angle = 45) +
156- ggplot2::geom_line(
157- ggplot2::aes(date_index, y = pred),
158- data = intervals,
159- inherit.aes = FALSE
160- ) +
161- ggplot2::geom_ribbon(
162- ggplot2::aes(date_index, ymin = lower_ci, ymax = upper_ci),
163- alpha = 0.2,
164- data = intervals,
165- inherit.aes = FALSE,
166- fill = "#BBB67E"
167- ) +
135+ ggplot2::geom_line(ggplot2::aes(date_index, y = pred),
136+ data = intervals,
137+ inherit.aes = FALSE) +
138+ ggplot2::geom_ribbon(
139+ ggplot2::aes(date_index, ymin = lower_ci, ymax = upper_ci),
140+ alpha = 0.2,
141+ data = intervals,
142+ inherit.aes = FALSE,
143+ fill = "#BBB67E"
144+ ) +
168145 ggplot2::labs(x = "Date", y = "Cases")
169146```
170147
@@ -225,7 +202,7 @@ library(ggplot2)
225202df_incid %>%
226203 mutate(
227204 rolling_average = data.table::frollmean(count, n = 7L, align = "right")
228- ) %>%
205+ ) %>%
229206 plot(border_colour = "white", angle = 45) +
230207 ggplot2::geom_line(ggplot2::aes(x = date_index, y = rolling_average)) +
231208 ggplot2::labs(x = "Date", y = "Cases")
@@ -243,7 +220,7 @@ Compute and visualize the monthly moving average of cases on `df_incid`?
243220df_incid %>%
244221 mutate(
245222 rolling_average = data.table::frollmean(count, n = 30L, align = "right")
246- ) %>%
223+ ) %>%
247224 plot(border_colour = "white", angle = 45) +
248225 ggplot2::geom_line(ggplot2::aes(x = date_index, y = rolling_average)) +
249226 ggplot2::labs(x = "Date", y = "Cases")
0 commit comments