---
title: "Package Functions"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{package-functions}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r setup}
library(HighFrequencyChecks)
```

<!-- WARNING - This vignette is generated by {fusen} from dev/function_documentation.Rmd: do not edit by hand -->

# Demo data 

 



# Pro-active actions - Enumerators rigorous work standards

## Interview Duration

```{r examples-assessmentDuration}

load(system.file("sample_dataset.RData", package = "HighFrequencyChecks"))
ds <- sample_dataset
dates <- c("survey_start","end_survey")

result <- assessmentDuration(ds = ds,  dates=dates)
knitr::kable(head(result[["ret_log"]],10))
print(result[["graph"]])
```

## Interview Duration Outliers

```{r examples-assessmentDurationOutliers}
load(system.file("sample_dataset.RData", package = "HighFrequencyChecks"))
ds <- sample_dataset
dates <- c("survey_start","end_survey")
uniquerespondantID <- "X_uuid"
enumeratorID <- "enumerator_id"
reportingColumns <- c(enumeratorID, uniquerespondantID)
sdval<-2
result <- assessmentDurationOutliers(ds = ds,
                                     dates=dates,
                                     sdval=sdval,
                                     reportingColumns=reportingColumns)

knitr::kable(head(result[["ret_log"]],10))
print(result[["graph"]])

```

## Picked up less than a certain number of answers for select multiple questions

```{r examples-enumeratorIsLazy}
load(system.file("sample_dataset.RData", package = "HighFrequencyChecks")) 
ds <- sample_dataset
enumeratorID <- "enumerator_id"
questionsEnumeratorIsLazy <- c(consent_received.shelter_nfi.non_food_items=3,
                                consent_received.food_security.main_income=3,
                                consent_received.child_protection.boy_risk=3,
                                consent_received.child_protection.girl_risk=3)

result <- enumeratorIsLazy(ds = ds,
                           enumeratorID=enumeratorID,
                           questionsEnumeratorIsLazy=questionsEnumeratorIsLazy)

 knitr::kable(head(result[["ret_log"]], 10))  

```

## Are Interviews Completed?

```{r examples-isInterviewCompleted}
load(system.file("sample_dataset.RData", package = "HighFrequencyChecks")) 
ds <- sample_dataset
surveyConsent <- "survey_consent"
dates <- c("survey_start","end_survey")
uniquerespondantID <- "X_uuid"
enumeratorID <- "enumerator_id"
reportingColumns <- c(enumeratorID, uniquerespondantID)

result <- isInterviewCompleted(ds = ds,
                               surveyConsent=surveyConsent,
                               dates=dates,
                               reportingColumns=reportingColumns,
                               deleteIsInterviewCompleted=FALSE)

knitr::kable(head(result[["ret_log"]], 10))  
print(result[["graph"]])
```

## Do Interviews include Consent?

```{r examples-isInterviewWithConsent}
load(system.file("sample_dataset.RData", package = "HighFrequencyChecks")) 
ds <- sample_dataset
surveyConsent <- "survey_consent"
uniquerespondantID <- "X_uuid"
enumeratorID <- "enumerator_id"
reportingColumns <- c(enumeratorID, uniquerespondantID)

result <- isInterviewWithConsent(ds = ds,
                                 surveyConsent=surveyConsent,
                                 reportingColumns=reportingColumns,
                                 deleteIsInterviewWithConsent=FALSE)
knitr::kable(head(result[["ret_log"]], 10))  
print(result[["graph"]])
```

## Is there Missing Values in the reponse?

```{r examples-surveyMissingValues}
load(system.file("sample_dataset.RData", package = "HighFrequencyChecks")) 
ds <- sample_dataset
enumeratorID <- "enumerator_id"

result <- surveyMissingValues(ds = ds,
                                                    enumeratorID=enumeratorID,
                                                    enumeratorCheck=FALSE)
knitr::kable(head(result[["ret_log"]], 10))  
```

## Number of distinct values (not missing) per fields

```{r examples-surveyDistinctValues}
load(system.file("sample_dataset.RData", package = "HighFrequencyChecks")) 
ds <- sample_dataset
enumeratorID <- "enumerator_id"

result <- surveyDistinctValues(ds = ds,
                                                     enumeratorID=enumeratorID,
                                                     enumeratorCheck=FALSE)
knitr::kable(head(result[["ret_log"]], 10))  
```

## Other distinct values (not missing) per fields other with count

```{r examples-surveyOtherValues}
load(system.file("sample_dataset.RData", package = "HighFrequencyChecks")) 
ds <- sample_dataset
otherPattern <- "_other"
enumeratorID <- "enumerator_id"

result <- surveyOtherValues(ds = ds,
                            otherPattern=otherPattern,
                            enumeratorID=enumeratorID,
                            enumeratorCheck=FALSE)
knitr::kable(head(result[["ret_log"]], 10))  
```

## Response Outliers

```{r examples-surveyOutliers}

load(system.file("sample_dataset.RData", package = "HighFrequencyChecks")) 
ds <- sample_dataset
enumeratorID <- "enumerator_id"
uniquerespondantID <- "X_uuid"
reportingColumns <- c(enumeratorID, uniquerespondantID)
sdval <- 2

result <- surveyOutliers(ds = ds,
                         enumeratorID = enumeratorID,
                         sdval = sdval,
                         reportingColumns = reportingColumns,
                         enumeratorCheck = FALSE)

knitr::kable(head(result[["ret_log"]], 10))  
```

## Abnormally High Values

```{r examples-surveyBigValues}
load(system.file("sample_dataset.RData", package = "HighFrequencyChecks")) 
ds <- sample_dataset
questionsSurveyBigValues <-c(consent_received.food_security.spend_food=25000,
                              consent_received.food_security.spend_medication=25000,
                              consent_received.food_security.spend_education=25000,
                              consent_received.food_security.spend_fix_shelter=25000,
                              consent_received.food_security.spend_clothing=25000,
                              consent_received.food_security.spend_hygiene=25000,
                              consent_received.food_security.spend_fuel=25000,
                              consent_received.food_security.spend_hh_items=25000,
                              consent_received.food_security.spend_transport=25000,
                              consent_received.food_security.spend_communication=25000,
                              consent_received.food_security.spend_tobacco=25000,
                              consent_received.food_security.spend_rent=25000,
                              consent_received.food_security.spend_debts=25000,
                              consent_received.food_security.spend_other=25000)
enumeratorID <- "enumerator_id"
uniquerespondantID <- "X_uuid"
reportingColumns <- c(enumeratorID, uniquerespondantID)

result <- surveyBigValues(ds = ds,
                                                questionsSurveyBigValues=questionsSurveyBigValues,
                                                enumeratorID=enumeratorID,
                                                reportingColumns=reportingColumns,
                                                enumeratorCheck=FALSE)
knitr::kable(head(result[["ret_log"]], 10))  
```

## Abnormally Low Values

```{r examples-surveySmallValues}
load(system.file("sample_dataset.RData", package = "HighFrequencyChecks")) 
ds <- sample_dataset
questionsSurveySmallValues <-c(consent_received.food_security.spend_food=25000,
                              consent_received.food_security.spend_medication=25000,
                              consent_received.food_security.spend_education=25000,
                              consent_received.food_security.spend_fix_shelter=25000,
                              consent_received.food_security.spend_clothing=25000,
                              consent_received.food_security.spend_hygiene=25000,
                              consent_received.food_security.spend_fuel=25000,
                              consent_received.food_security.spend_hh_items=25000,
                              consent_received.food_security.spend_transport=25000,
                              consent_received.food_security.spend_communication=25000,
                              consent_received.food_security.spend_tobacco=25000,
                              consent_received.food_security.spend_rent=25000,
                              consent_received.food_security.spend_debts=25000,
                              consent_received.food_security.spend_other=25000)
enumeratorID <- "enumerator_id"
uniquerespondantID <- "X_uuid"
reportingColumns <- c(enumeratorID, uniquerespondantID)

result <- surveySmallValues(ds = ds,
                                        questionsSurveySmallValues=questionsSurveySmallValues,
                                        enumeratorID=enumeratorID,
                                        reportingColumns=reportingColumns,
                                        enumeratorCheck=FALSE)
knitr::kable(head(result[["ret_log"]], 10))  
```

## Enumerator Errors Summary

```{r examples-enumeratorErrorsSummary}

# enumeratorID <- "enumerator_id"
# reports <- c( "isInterviewCompleted",
#               "isInterviewInTheCorrectSite",
#               "isInterviewTooShort",
#               "isInterviewTooShortForTheHouseholdSize",
#               "isInterviewWithConsent",
#               "isSurveyEndBeforeItStarts",
#               "isSurveyMadeInTheFuture",
#               "isSurveyOnMoreThanADay",
#               "isSurveyStartedBeforeTheAssessment",
#               "isuniquerespondantIDDuplicated",
#               "isuniquerespondantIDMissing")
# 
# result <- enumeratorErrorsSummary(enumeratorID=enumeratorID,ds = ds,
                                      # surveyDate=surveyDate,
                                      # dateFormat=dateFormat,
                                      # surveyConsent=surveyConsent
#                                     reports=reports)
# print(result[["graph"]])

```

# Pro-active actions - Enumerators productivity 

## Summary of daily average productivity

```{r examples-assessmentProductivity}
load(system.file("sample_dataset.RData", package = "HighFrequencyChecks"))
ds <- sample_dataset
surveyDate <- "survey_date"
dateFormat <- "%m/%d/%Y"
surveyConsent <- "survey_consent"
result <- assessmentProductivity(ds = ds,
                                                       surveyDate = surveyDate,
                                                       dateFormat = dateFormat,
                                                       surveyConsent = surveyConsent)
knitr::kable(head(result[["ret_log"]], 10))  
print(result[["graph"]])

```

## Daily number of filled forms per attempt output

```{r examples-assessmentDailyValidSurveys}
load(system.file("sample_dataset.RData", package = "HighFrequencyChecks"))
ds <- sample_dataset
surveyDate <- "survey_date"
dateFormat <- "%m/%d/%Y"
surveyConsent <- "survey_consent"
result <- assessmentDailyValidSurveys(
  ds = ds,
  surveyDate = surveyDate,
  dateFormat = dateFormat,
  surveyConsent = surveyConsent)

knitr::kable(head(result[["ret_log"]], 10))  
print(result[["graph"]])

```

  



## Daily number of filled forms per time in the day

```{r examples-assessmentInterviewTime}
load(system.file("sample_dataset.RData", package = "HighFrequencyChecks"))
ds <- sample_dataset
surveyDate <- "survey_date"
dateFormat <- "%m/%d/%Y"
surveyConsent <- "survey_consent"
result <- assessmentInterviewTime(ds = ds,
                                      surveyDate=surveyDate,
                                      dateFormat=dateFormat,
                                      surveyConsent=surveyConsent)

knitr::kable(head(result[["ret_log"]], 10))  
print(result[["graph"]])

```

  




## Percentage of non-completed interviews by enumerator 

```{r examples-enumeratorSurveysConsent}
load(system.file("sample_dataset.RData", package = "HighFrequencyChecks")) 
ds <- sample_dataset
surveyConsent <- "survey_consent"
enumeratorID <- "enumerator_id"

result <- enumeratorSurveysConsent(ds = ds,
                                                     surveyConsent=surveyConsent,
                                                       enumeratorID=enumeratorID)

knitr::kable(head(result[["ret_log"]], 10))  
print(result[["graph"]])

```

## Interview Duration per Enumerator

```{r examples-enumeratorSurveysDuration}

load(system.file("sample_dataset.RData", package = "HighFrequencyChecks")) 
ds <- sample_dataset
dates <- c("survey_start","end_survey")
enumeratorID <- "enumerator_id"
result <- enumeratorSurveysDuration(ds = ds,
                                                          dates=dates,
                                                          enumeratorID=enumeratorID)
knitr::kable(head(result[["ret_log"]], 10))  
print(result[["graph"]])

```

## Number of Interview by enumerator

```{r examples-enumeratorProductivity}
load(system.file("sample_dataset.RData", package = "HighFrequencyChecks")) 
ds <- sample_dataset
surveyDate <- "survey_date"
enumeratorID <- "enumerator_id"

result <- enumeratorProductivity(ds = ds,
                                 surveyDate=surveyDate,
                                 enumeratorID=enumeratorID)

knitr::kable(head(result[["ret_log"]], 10))  
print(result[["graph"]])

```

## Enumerators with very low or high productivity

```{r examples-enumeratorProductivityOutliers}
load(system.file("sample_dataset.RData", package = "HighFrequencyChecks")) 
ds <- sample_dataset
enumeratorID <- "enumerator_id"
surveyDate <- "survey_date"
sdval<-2

result <- enumeratorProductivityOutliers(ds = ds,
                                         enumeratorID=enumeratorID,
                                         surveyDate=surveyDate,
                                         sdval=sdval)

knitr::kable(head(result[["ret_log"]], 10))  
print(result[["graph"]])

```

# Corrective actions - Correct set-up of data collection devices and encoding of the forms

## How many  Attempt for each SampleRespondant ID?

```{r examples-isuniquerespondantIDDuplicated}
load(system.file("sample_dataset.RData", package = "HighFrequencyChecks"))
ds <- sample_dataset
uniquerespondantID <- "X_uuid"
surveyConsent <- "survey_consent"
enumeratorID <- "enumerator_id"
reportingColumns <- c(enumeratorID, uniquerespondantID)

result <- isuniquerespondantIDDuplicated(ds = ds,
                               uniquerespondantID=uniquerespondantID,
                               surveyConsent=surveyConsent,
                               reportingColumns=reportingColumns,
                               deleteIsuniquerespondantIDDuplicated=FALSE)
knitr::kable(head(result[["ret_log"]], 10)) 
print(result[["graph"]])

```

## Are there unique respondantID Missing?

```{r examples-isuniquerespondantIDMissing}

load(system.file("sample_dataset.RData", package = "HighFrequencyChecks"))
ds <- sample_dataset
uniquerespondantID <- "X_uuid"
surveyConsent <- "survey_consent"
enumeratorID <- "enumerator_id"
reportingColumns <- c(enumeratorID, uniquerespondantID)

result <- isuniquerespondantIDMissing(ds = ds,
                            uniquerespondantID=uniquerespondantID,
                            surveyConsent=surveyConsent,
                            reportingColumns=reportingColumns,
                            deleteIsuniquerespondantIDMissing=FALSE)
knitr::kable(head(result[["ret_log"]], 10)) 
print(result[["graph"]])
```

## Is there any survey interview lasting on more than a day?

```{r examples-isSurveyOnMoreThanADay}
load(system.file("sample_dataset.RData", package = "HighFrequencyChecks")) 
ds <- sample_dataset
surveyConsent <- "survey_consent"
dates <- c("survey_start","end_survey")
uniquerespondantID <- "X_uuid"
enumeratorID <- "enumerator_id"
reportingColumns <- c(enumeratorID, uniquerespondantID)

result <- isSurveyOnMoreThanADay(ds = ds,
                                 surveyConsent=surveyConsent,
                                 dates=dates,
                                 reportingColumns=reportingColumns,
                                 deleteIsSurveyOnMoreThanADay=FALSE)

knitr::kable(head(result[["ret_log"]], 10))
print(result[["graph"]])
```

## Is there any survey interview with end date time before start time?

```{r examples-isSurveyEndBeforeItStarts}
load(system.file("sample_dataset.RData", package = "HighFrequencyChecks")) 
ds <- sample_dataset
surveyConsent <- "survey_consent"
dates <- c("survey_start","end_survey")
uniquerespondantID <- "X_uuid"
enumeratorID <- "enumerator_id"
reportingColumns <- c(enumeratorID, uniquerespondantID)

result <- isSurveyEndBeforeItStarts(ds = ds,
                                    surveyConsent=surveyConsent,
                                    dates=dates,
                                    reportingColumns=reportingColumns,
                                    deleteIsSurveyEndBeforeItStarts=FALSE)
knitr::kable(head(result[["ret_log"]], 10))
print(result[["graph"]])
```

## Is there any survey interview with start date after today?

```{r examples-isSurveyMadeInTheFuture}
load(system.file("sample_dataset.RData", package = "HighFrequencyChecks")) 
ds <- sample_dataset
dates <- c("survey_start","end_survey")
surveyConsent <- "survey_consent"
uniquerespondantID <- "X_uuid"
enumeratorID <- "enumerator_id"
reportingColumns <- c(enumeratorID, uniquerespondantID)

result <- isSurveyMadeInTheFuture(ds = ds,
                                                        surveyConsent=surveyConsent,
                                                        dates=dates,
                                                        reportingColumns=reportingColumns,
                                                        deleteIsSurveyMadeInTheFuture = FALSE)
knitr::kable(head(result[["ret_log"]], 10))
print(result[["graph"]])
```

# Corrective actions - Data collected according the plan

## Is there any survey interview starting before official first day?

```{r examples-isSurveyStartedBeforeTheAssessment}
load(system.file("sample_dataset.RData", package = "HighFrequencyChecks"))
ds <- sample_dataset
dates <- c("survey_start","end_survey")
surveyConsent <- "survey_consent"
startDataCollection <- "2018-11-11"
uniquerespondantID <- "X_uuid"
enumeratorID <- "enumerator_id"
reportingColumns <- c(enumeratorID, uniquerespondantID)

result <- isSurveyStartedBeforeTheAssessment(
  ds = ds,
  dates=dates,
  surveyConsent=surveyConsent,
  startDataCollection=startDataCollection,
  reportingColumns=reportingColumns,
  deleteIsSurveyStartedBeforeTheAssessment = FALSE)
knitr::kable(head(result[["ret_log"]], 10))
print(result[["graph"]])
```

## Are there interviews appearing too short?

```{r examples-isInterviewTooShort}
load(system.file("sample_dataset.RData", package = "HighFrequencyChecks"))
ds <- sample_dataset
surveyConsent <- "survey_consent"
dates <- c("survey_start","end_survey")
uniquerespondantID <- "X_uuid"
enumeratorID <- "enumerator_id"
minimumSurveyDuration <- 30
reportingColumns <- c(enumeratorID, uniquerespondantID)

result <- isInterviewTooShort(ds = ds,
                                                    surveyConsent=surveyConsent,
                                                    dates=dates,
                                                    minimumSurveyDuration=minimumSurveyDuration,
                                                    reportingColumns=reportingColumns,
                                                    deleteIsInterviewTooShort=FALSE)
knitr::kable(head(result[["ret_log"]], 10))
print(result[["graph"]])
```

## Are there interviews appearing too short based on household Size?

```{r examples-isInterviewTooShortForTheHouseholdSize}
load(system.file("sample_dataset.RData", package = "HighFrequencyChecks")) 
ds <- sample_dataset
surveyConsent <- "survey_consent"
dates <- c("survey_start","end_survey")
householdSize <-"consent_received.respondent_info.hh_size"
uniquerespondantID <- "X_uuid"
enumeratorID <- "enumerator_id"
minimumSurveyDurationByIndividual <- 10
reportingColumns <- c(enumeratorID, uniquerespondantID)

result <- isInterviewTooShortForTheHouseholdSize(ds = ds,
                            surveyConsent=surveyConsent,
                            dates=dates,
                            householdSize=householdSize,
                            minimumSurveyDurationByIndividual=minimumSurveyDurationByIndividual,
                          reportingColumns=reportingColumns,
                            deleteIsInterviewTooShortForTheHouseholdSize=FALSE)
knitr::kable(head(result[["ret_log"]], 10))
print(result[["graph"]])
```

## Are interviews taking place in the expected site?

```{r examples-isInterviewInTheCorrectSite}
load(system.file("sample_dataset.RData", package = "HighFrequencyChecks")) 
ds <- sample_dataset
dsSite <- "union_name"
dsCoordinates <- c("X_gps_reading_longitude","X_gps_reading_latitude")
load(system.file("admin.RData", package = "HighFrequencyChecks"))
adminBoundaries <- admin
adminBoundariesSite <- "Union"
surveyConsent <- "survey_consent"
uniquerespondantID <- "X_uuid"
enumeratorID <- "enumerator_id"
reportingColumns <- c(enumeratorID, uniquerespondantID)

result <- isInterviewInTheCorrectSite(ds = ds,
                                      dsSite=dsSite,
                                      dsCoordinates = dsCoordinates,
                                      adminBoundaries=adminBoundaries,
                                      adminBoundariesSite=adminBoundariesSite,
                                      surveyConsent=surveyConsent,
                                      reportingColumns=reportingColumns,
                                      correctIsInterviewInTheCorrectSite=FALSE)

knitr::kable(head(result[["ret_log"]], 10)) 
print(result[["graph"]])
```

## Are interviews taking place in the expected point?

```{r examples-isInterviewAtTheSamplePoint}
load(system.file("sample_dataset.RData", package = "HighFrequencyChecks")) 
ds <- sample_dataset
load(system.file("SamplePts.RData", package = "HighFrequencyChecks"))
sampledPoints <- SamplePts

dsCoordinates <- c("X_gps_reading_longitude","X_gps_reading_latitude")
buffer <- 10
surveyConsent <- "survey_consent"
uniquerespondantID <- "X_uuid"
enumeratorID <- "enumerator_id"
reportingColumns <- c(enumeratorID, uniquerespondantID)

# result <- isInterviewAtTheSamplePoint(ds = ds,
#                                       dsCoordinates = dsCoordinates,
#                                       sampledPoints=sampledPoints,
#                                       buffer=buffer,
#                                       surveyConsent=surveyConsent,
#                                       reportingColumns=reportingColumns,
#                                       deleteIsInterviewAtTheSamplePoint=FALSE)
# knitr::kable(head(result[["ret_log"]], 10))
# print(result[["graph"]])
```

## Overall tracking sheet

```{r examples-assessmentTrackingSheet}
load(system.file("sample_dataset.RData", package = "HighFrequencyChecks"))
ds <- sample_dataset
dsSite <- "union_name"
load(system.file("SampleSize.RData", package = "HighFrequencyChecks"))
sampleSizeTable <- SampleSize

sampleSizeTableSite <- "Union"
sampleSizeTableTarget <- "SS"
sampleSizeTableAvailable <- "TotPts"                # Usually the Target + a buffer
surveyConsent <- "survey_consent"
consentForValidSurvey <- "yes"                      # consent value for yes

result <- assessmentTrackingSheet(ds = ds,
                                  dsSite = dsSite,
                                  sampleSizeTable = sampleSizeTable,
                                  sampleSizeTableSite = sampleSizeTableSite,
                                  sampleSizeTableTarget = sampleSizeTableTarget,
                                  sampleSizeTableAvailable = sampleSizeTableAvailable,
                                  surveyConsent = surveyConsent,
                                  consentForValidSurvey = consentForValidSurvey)

knitr::kable(head(result[["ret_log"]], 10))  
print(result[["graph"]])

```

# run_app

<!--
Create a chunk for the core of the function

- The chunk needs to be named `function` at least
- It contains the code of a documented function
- The chunk can also be named `function-my_function` to make it easily
findable in your Rmd
- Let the `@examples` part empty, and use the next `examples` chunk instead to present reproducible examples

After inflating the template

-  This function code will automatically be added in a new file in the "R/" directory
-->
    

<!--
Create a chunk with an example of use for your function

- The chunk needs to be named `examples` at least
- It contains working examples of your function
- The chunk is better be named `examples-my_median` to be handled
correctly when inflated as a vignette

After inflating the template

-  This example will automatically be added in the '@examples' part of our function above in the "R/" directory
- This example will automatically be added in the vignette created from this Rmd template
-->


```{r example-run_app}
# run_app()
```

<!--
Create a chunk with a test of use for your function

- The chunk needs to be named `tests` at least
- It contains working tests of your function
- The chunk is better be named `tests-my_median` to be handled
correctly when inflated as a vignette

After inflating the template

-  This test code will automatically be added in the "tests/testthat/" directory
-->
 

  



<!--
 Once you have created your back office functions , run the next chunk to install and package them


# There can be development actions

Create a chunk with 'development' actions

- The chunk needs to be named `development` or `dev`
- It contains functions that are used for package development only
- Note that you may want to store most of these functions in the 0-dev_history.Rmd file

These are only included in the present flat template file, their content will not be part of the package anywhere else.
-->





 

