我已经看到了几个关于拐点计算的 SO 问题。我仍然不确定我是否做得对。根据实验室确认的当前疫情中心的累积病例数据,我们试图确定拐点。我使用了该inflection
软件包并将拐点计算为“2020 年 2 月 8 日”。我还尝试计算第一个和第二个指令作为估计的每个增加和变化率。我对它的数学理解很少,但只是遵循不同 SO 帖子中的示例。我的问题:下图中的结果是否一致?如果不是如何改进我的代码。
df<-structure(list(date = structure(c(18277, 18278, 18279, 18280,
18281, 18282, 18283, 18284, 18285, 18286, 18287, 18288, 18289,
18290, 18291, 18292, 18293, 18294, 18295, 18296, 18297, 18298,
18299, 18300, 18301, 18302, 18303, 18304, 18305, 18306, 18307),
class = "Date"),
cases = c(45, 62, 121, 198, 258, 363, 425,
495, 572, 618, 698, 1590, 1905, 2261, 2639, 3125, 4109, 5142,
6384, 8351, 10117, 11618, 13603, 14982, 16903, 18454, 19558,
20630, 21960, 22961, 23621)),
class = "data.frame", row.names = c(NA, -31L))
xlb_0<- structure(c(18281, 18285, 18289, 18293,
18297, 18301, 18305,
18309), class = "Date")
library(tidyverse)
# Smooth cumulative cases over time
df$x = as.numeric(df$date)
fit_1<- loess(cases ~ x, span = 1/3, data = df)
df$case_sm <-fit_1$fitted
# use inflection to obtain inflection point
library(inflection)
guai_0 <- check_curve(df$x, df$case_sm)
check_curve(df$x, df$cases)
#> $ctype
#> [1] "convex_concave"
#>
#> $index
#> [1] 0
guai_1 <- bese(df$x, df$cases, guai_0$index)
structure(guai_1$iplast, class = "Date")
#> [1] "2020-02-08"
# Plot cumulativew numbers of cases
df %>%
ggplot(aes(x = date, y = cases ))+
geom_line(aes(y = case_sm), color = "red") +
geom_point() +
geom_vline(xintercept = guai_1$iplast) +
labs(y = "Cumulative lab confirmed infections")
# Daily new cases (first derivative) and changing rate (second derivative)
df$dt1 = c(0, diff(df$case_sm)/diff(df$x))
fit_2<- loess(dt1 ~ x, span = 1/3, data = df)
df$change_sm <-fit_2$fitted
df$dt2 <- c(NA, diff(df$change_sm)/diff(df$x))
df %>%
ggplot(aes(x = date, y = dt1))+
geom_line(aes(y = dt1,
color = "Estimated number of new cases")) +
geom_point(aes(y = dt2*2, color = "Changing rate")) +
geom_line(aes(y = dt2*2, color = "Changing rate"))+
geom_vline(xintercept = guai_1$iplast) +
labs(y = "Estimatede number of new cases") +
scale_x_date(breaks = xlb_0,
date_labels = "%b%d") +
theme(legend.title = element_blank())
#> Warning: Removed 1 rows containing missing values (geom_point).
#> Warning: Removed 1 row(s) containing missing values (geom_path).
由reprex 包于 2020-02-17 创建(v0.3.0)