`r params$disease`: Report of signals"

##******************************************************************************
## Chunk options
##******************************************************************************

knitr::opts_chunk$set(eval=TRUE, echo=FALSE, 
               fig.align='center',fig.width=13,fig.height=5,
               message=FALSE, warning=FALSE)

knitr::knit_hooks$set(inline = function(x) {
  format(x, big.mark=",")
})

##******************************************************************************
## Definition of the main color of graphs and table
##******************************************************************************

maincolor="#69AE23"


##******************************************************************************
## Libraries
##******************************************************************************
library(ggplot2)
library(dplyr)
## ~~~~~~~~~~~~~~~~
## Setting parameters
## ~~~~~~~~~~~~~~~~

if(is.null(params$file)) {
  dataset <- EpiSignalDetection::SignalData
} else {
  inFile <- params$file
  dataset <- EpiSignalDetection::importAtlasExport(inFile$datapath)
  dataset <- EpiSignalDetection::cleanAtlasExport(dataset)
}


input <- list(
  disease = params$disease,
  country = params$country,
  indicator = params$indicator,
  stratification = params$stratification,
  unit = params$unit,
  daterange = params$daterange,
  algo = params$algo,
  testingperiod = params$testingperiod
)

tempPath <- params$tempPath

#--- Filtering on country, stratification and time unit
dataset <- EpiSignalDetection::filterAtlasExport(dataset, input)
#--- Defining the study period
StudyPeriod <- EpiSignalDetection::studyPeriod(input)
#--- Defining the testing period period
TestingPeriod <- StudyPeriod$Time[length(StudyPeriod$Time):(length(StudyPeriod$Time) - input$testingperiod + 1)]
#-- Excluding countries with gaps and EU pre-computed values from the TS
excluded <- unique(dataset$RegionName[ is.na(dataset$NumValue) ])

if (input$country == "EU-EEA - complete series") {
  included <- unique(dataset$RegionName[!(dataset$RegionName %in% c(excluded, "EU", "EU/EEA"))])
  if (length(included) != 0) {
    dataset <- dplyr::filter(dataset, dataset$RegionName %in% included )
  } else {
    warning("All countries present with at least one gap in the time series")
  }
} else if (length(excluded) !=0) {
  warning("The selected country presents with at least one gap in the time series")
}

#-- Dataset for the testing period (TP) only
datasetTP <- dplyr::filter(dataset, Time %in% TestingPeriod)

Study period: r StudyPeriod$Time[1] to r StudyPeriod$Time[length(StudyPeriod$Time)]

Signal detection period: r TestingPeriod[input$testingperiod] to r TestingPeriod[1]

if(input$country == "EU-EEA - complete series") {

  datasetByCountry <- datasetTP %>%
    dplyr::group_by(RegionName) %>%
    dplyr::summarise( NumValue = sum(NumValue)  ) %>%
    dplyr::ungroup()

#--- Sort by number cases by countries
  datasetByCountry <- datasetByCountry %>%
    dplyr::arrange(NumValue) %>%
    dplyr::mutate(RegionName = factor(datasetByCountry$RegionName, levels = datasetByCountry$RegionName))

  p <- ggplot(datasetByCountry, aes(x = RegionName, y = NumValue)) +
    geom_bar(stat = "identity", fill = maincolor) +
    scale_y_continuous(expand = c(0,0)) + 
    coord_flip() +
    labs(title = "Graph: Number of cases observed by country in the signal detection period", 
         x = "Reporting country", y = "Number of cases") +
    theme(title = element_text(size = 14), 
          axis.text = element_text(size = 12),
          axis.text.x = element_text(size = 12),
          axis.title = element_text(size = 14, face = "bold"), 
          plot.title = element_text(color = "grey", hjust = 0, vjust = 0),
          panel.grid.major = element_blank(), 
          panel.grid.minor = element_blank())
  p

}

Signal detection: r TestingPeriod[input$testingperiod] to r TestingPeriod[1]

#--- Preparation of the result table that will be append through the following loop
Result <- data.frame( Time = "" ,  
                      Place = "" , 
                      Observed = "" ,
                      Threshold.ALGO = "", 
                      ALGO = "",  
                      stringsAsFactors=FALSE)

#-- Creation of a EU/EEA
if(input$country == "EU-EEA - complete series"){
  datasetEU <- dataset %>%
    dplyr::group_by(StudyPeriod) %>%
    dplyr::summarise( NumValue = sum(NumValue)  ) %>%
    dplyr::ungroup()
  datasetEU <- data.frame(RegionName = "EU-EEA - complete series", datasetEU)
  dataset <- dataset[, c("RegionName", "StudyPeriod", "NumValue")]
  dataset <- rbind(datasetEU, dataset)
}

out <- NULL
countries <- na.omit(unique(dataset$RegionName))

for(i in 1:length(countries)){
  country = countries[i]
  dataset_1 = dplyr::filter(dataset, RegionName == country)

  #-------------------------------------------------------
  #---- Sts object
  #-------------------------------------------------------

  dataset.sts <- EpiSignalDetection::stsSD(observedCases = dataset_1$NumValue,
                                           studyPeriod = dataset_1$StudyPeriod,
                                           timeUnit = input$unit,
                                           startYM = c(
                                             as.numeric(format(as.Date(input$daterange[1], "%Y-%m-%d"), "%Y")),
                                             as.numeric(format(as.Date(input$daterange[1], "%Y-%m-%d"), "%m"))))

  #-------------------------------------------------------
  #---- Detection algorithm
  #-------------------------------------------------------

  dataset.algo <- EpiSignalDetection::algoSD(dataset.sts,
                                             algo = input$algo,
                                             timeUnit = input$unit,
                                             testingPeriod = input$testingperiod)

  #-------------------------------
  #--- Saving the plot
  #-------------------------------


  if (!file.exists(paste(tempPath, "\\plots", sep = ""))){
    dir.create(file.path(tempPath, "plots"))
  }

  png(file = paste(tempPath, "/plots/", sub("/", "-", country), "_TimeSeries.png", sep = ""), 
      width = 1450, height = 500, res=90)
  EpiSignalDetection::plotSD(dataset_1, input, subRegionName = country, 
                             x.sts = dataset.sts, x.algo = dataset.algo)
  dev.off()

  #-------------------------------
  #--- Incrementation of the Result table
  #------------------------------- 

  for(l in 1:(input$testingperiod)){
    Result <- rbind(Result,
                    data.frame(Time = TestingPeriod[input$testingperiod +1 -l], 
                               Place = country,
                               Observed = as.character(dataset.algo@observed[l]),
                               Threshold.ALGO = as.character(round(dataset.algo@upperbound[l],0)),
                               ALGO = ifelse(dataset.algo@alarm[l],
                                             "<span style=\"color:#DD4814\">Signal</span>",
                                             "no")

                    )
    )
  } 

}

Result <- Result[-1,]

r paste(out, collapse='\n')

out=NULL

for(i in countries){  
  Result_c <- dplyr::filter(Result, Place == i & grepl("Signal", ALGO))
  row.names(Result_c) <- NULL
  names(Result_c) <- c(paste("Time (agg. by ", tolower(input$unit), ")", sep = ""),
                       "Place",
                       "Reported cases",
                       "Threshold value",
                       "Signal")

  out <- c(out,knitr::knit_child('subsection.Rmd'))

}

r paste(out, collapse='\n')



Try the EpiSignalDetection package in your browser

Any scripts or data that you put into this service are public.

EpiSignalDetection documentation built on Dec. 11, 2021, 9:27 a.m.