Skip to content

Commit def14bf

Browse files
committed
Adding slope.estimate.quant function
1 parent 1ec72a3 commit def14bf

File tree

7 files changed

+80
-35
lines changed

7 files changed

+80
-35
lines changed

R/nowcasting_age.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#' @export
1010
nowcasting_age <- function(dataset,
1111
zero_inflated=FALSE){
12-
## Cehcl for zero-inflated
12+
## [Not in use] Check for zero-inflated
1313
if (zero_inflated){
1414
family <- "zeroinflatednbinomial1"
1515
control.family <- list(

R/nowcasting_inla.R

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
#' @param age_col Column for ages
3232
#' @param date_onset Column of dates of onset of the events, normally date of onset of first symptoms of cases
3333
#' @param date_report Column of dates of report of the event, normally date of digitation of the notification of cases
34-
#' @param trajectories Returns the trajectories estimated on the inner 'INLA' model
34+
#' @param trajectories Returns the trajectories estimated from the inner 'INLA' model
35+
#' [Default] FALSE.
3536
#' @param zero_inflated [Experimental] In non-structured models, fit a model that deals with zero-inflated data.
3637
#' [Default] FALSE. If the [age_col] is not missing this flag is ignored.
3738
#' @param ... list parameters to other functions
@@ -82,7 +83,7 @@ nowcasting_inla <- function(dataset,
8283

8384
}
8485
if(missing(date_onset) | missing(date_report)){
85-
stop("date_onset or date_report missing! Please give a column name for each of this parameters")
86+
stop("'date_onset' or 'date_report' missing! Please give a column name for each of this parameters")
8687
}
8788
if(K < 0 ){
8889
stop("K less than 0, we cannot produce backcasting! \n
@@ -113,34 +114,34 @@ nowcasting_inla <- function(dataset,
113114
## Missing trim.data warning
114115
if(missing(trim.data)){
115116
trim.data <- 0
116-
warning("Using default to trim dates, trim.data = 0")
117+
warning("Using default to trim dates, 'trim.data = 0'")
117118
}else{
118119
trim.data<-trim.data
119120
message("Using trim.data inputed")
120121
}
121122
## Missing Dmax warning
122123
if(missing(Dmax)){
123124
Dmax <- 15
124-
warning("Using default to maximum delay, Dmax = 15")
125+
warning("Using default to maximum delay, 'Dmax = 15'")
125126
}else{
126127
Dmax<-Dmax
127128
message("Using Dmax inputed")
128129
}
129130
## Missing wdw warning
130131
if(missing(wdw)){
131132
wdw <- 30
132-
warning("Using default to window of action, wdw = 30")
133+
warning("Using default to window of action, 'wdw = 30'")
133134
}else{
134135
wdw<-wdw
135136
message("Using wdw inputed")
136137
}
137138
## Missing data.by.week warning
138139
if(missing(data.by.week)){
139140
data.by.week <- FALSE
140-
warning("Using default to returning option for the data, data.by.week = FALSE")
141+
warning("Using default to returning option for the data, 'data.by.week = FALSE'")
141142
}else{
142143
data.by.week<-data.by.week
143-
message("Returning data.by.week")
144+
message("Returning 'data.by.week'")
144145
}
145146
# ## Missing return.age warning
146147
# if(missing(return.age)){
@@ -149,9 +150,9 @@ nowcasting_inla <- function(dataset,
149150
# }
150151
## Missing age_col warning
151152
if(missing(age_col)){
152-
warning("Age_col missing, nowcasting with unstructured model")
153+
warning("'age_col' missing, nowcasting with unstructured model")
153154
}else{
154-
message("Age col inputed, nowcasting with structured model")
155+
message("'age_col' inputed, nowcasting with structured model")
155156
}
156157

157158
if(missing(trajectories) | trajectories == FALSE){
@@ -162,7 +163,7 @@ nowcasting_inla <- function(dataset,
162163

163164
if(!missing(age_col) & !missing(zero_inflated)){
164165
zero_inflated<-FALSE
165-
warning("age_col parsed, zero_inflated ignored!")
166+
warning("'age_col' parsed, 'zero_inflated' ignored!")
166167
}
167168
}
168169
}

R/nowcasting_no_age.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@
66
#' @param dataset data pre formatted in to age classes and delays by week for each cases,
77
#' delay triangle format
88
#' @param zero_inflated zero-inflated model
9+
#' [Default] FALSE.
910
#'
1011
#' @return Trajectories from the inner 'INLA' model
1112
#' @export
1213
nowcasting_no_age <- function(dataset,
1314
zero_inflated=FALSE){
15+
## Safe test
16+
if(missing(dataset)){
17+
stop("'dataset' is missing in 'nowcasting_no_age()'.")
18+
}
1419

1520
## Check for the zero-inflated
1621
if (zero_inflated){

R/slope.estimate.quant.R

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,74 @@
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)
1124
slope.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
}

man/nowcasting_inla.Rd

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/nowcasting_no_age.Rd

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/slope.estimate.quant.Rd

Lines changed: 16 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)