1- # ' @title Slope.estimate
1+ # ' @title Slope.estimate.quant
2+ # '
3+ # ' @description
4+ # ' Fits a linear model to trajectories returned from 'nowcasting_inla()' within a given window [Default] is 3 weeks. If 'end.week' is missing uses the maximum date in 'trajectories'.
25# '
3- # ' @param end.week [in weeks] The end of the week wanted to the slope estimate
46# ' @param trajectories Data.frame with the predicted or nowcasted estimate
7+ # ' @param end.week [in weeks] The end of the week wanted to the slope estimate
8+ # ' [Default] max. date in 'trajectories'.
59# ' @param window [in weeks] Window of how much time will be used to calculate the slope estimate
10+ # ' [Default] 3 weeks.
611# '
7- # ' @return The slope of the estimate
12+ # ' @return The numerical value of the slope of the estimate
813# ' @export
914# '
1015# ' @examples
16+ # ' # Loading Belo Horizonte SARI dataset
17+ # ' data(sragBH)
18+ # ' now <- nowcasting_inla(dataset = sragBH,
19+ # ' date_onset = DT_SIN_PRI,
20+ # ' date_report = DT_DIGITA,
21+ # ' trajectories = TRUE,
22+ # ' silent = T)
23+ # ' slope.estimate.quant(trajectories = now$trajectories)
1124slope.estimate.quant <- function (end.week , trajectories , window = 3 ){
1225
26+ if (missing(trajectories )){
27+ stop(" 'trajectories' is missing!" )
28+ }
29+
30+ # # If 'end.week' is missing uses the max. date from trajectories
1331 if (missing(end.week )){
1432 end.week <- max(trajectories $ dt_event )
33+ warning(" Using max. date in 'trajectories' as 'end.week'" )
1534 }
1635
36+ # # Handling trajectories object
1737 trajectories <- trajectories | >
1838 dplyr :: rename(Date = dt_event ,
19- Casos = Y )
39+ Cases = Y )
2040
21- # # Testing if the amount of weeks in the data encompasse the size of the window
41+ # # Testing if the amount of weeks in the data encompass the size of the window
2242 base.week <- end.week - (window * 7 - 7 )
2343 if (! base.week %in% trajectories $ Date ){
24- return (NA )
44+ base.week <- min(trajectories $ Date )
45+ warning(" 'base.week' wasn't present in 'trajectories', using min. of date." )
2546 }
2647
2748 # # Normalizing the cases column name
28- norm.casos <- trajectories | >
49+ norm.cases <- trajectories | >
2950 dplyr :: filter(Date == base.week ) | >
30- dplyr :: rename(valorbase = Casos ) | >
31- dplyr :: group_by(sample , valorbase ) | >
32- dplyr :: select(- Time , - Date )
33-
34- norm.casos <- norm.casos | >
51+ dplyr :: rename(database_value = Cases ) | >
52+ dplyr :: group_by(sample , database_value ) | >
53+ dplyr :: select(- Time , - Date ) | >
3554 dplyr :: right_join(trajectories | >
3655 dplyr :: filter(Date > = base.week & Date < = end.week ),
3756 by = ' sample' ) | >
38- dplyr :: mutate(Casos = case_when(
39- valorbase > 0 ~ Casos / valorbase ,
40- TRUE ~ Casos ))
57+ dplyr :: mutate(Cases = dplyr :: case_when(
58+ database_value > 0 ~ Cases / database_value ,
59+ TRUE ~ Cases ))
4160
4261 # # Testing the amount of samples in the trajectories, if it unique, fit the line and get the slope
4362 # # if it is more than one, fit for each sample the line and does statistical estimates for the slope
44- if (length(norm.casos $ sample | > unique()) == 1 ){
63+ if (length(norm.cases $ sample | > unique()) == 1 ){
4564
4665 # # The line model fitting
47- tmp <- stats :: lm(Casos ~ Date , data = norm.casos )
66+ tmp <- stats :: lm(Cases ~ Date , data = norm.cases )
67+
4868 # # Confidence Intervals
4969 l <- stats :: confint(tmp , parm = ' Date' , level = .9 )
5070 q <- stats :: confint(tmp , parm = ' Date' , level = .5 )
71+
5172 # # Slope estimate
5273 slope <- dplyr :: case_when(
5374 l [1 ] > 0 ~ 1 ,
@@ -57,9 +78,12 @@ slope.estimate.quant <- function(end.week, trajectories, window=3){
5778 l [2 ] < 0 ~ - 1
5879 ) | >
5980 as.numeric()
81+
6082 } else {
83+
6184 # # More than one sample
62- tmp <- lme4 :: lmList(Casos ~ Date | sample , data = norm.casos )
85+ tmp <- lme4 :: lmList(Cases ~ Date | sample , data = norm.cases )
86+
6387 slope <- stats :: coefficients(tmp ) | >
6488 dplyr :: select(Date ) | >
6589 # # Quantiles calculations
@@ -78,6 +102,7 @@ slope.estimate.quant <- function(end.week, trajectories, window=3){
78102 as.numeric()
79103
80104 }
105+
81106 # # Return the slope
82107 return (slope )
83108}
0 commit comments