forked from Edouard-Legoupil/HighFrequencyChecks
-
Notifications
You must be signed in to change notification settings - Fork 2
/
functions_a.R
384 lines (368 loc) · 18.2 KB
/
functions_a.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#' @name assessmentDuration
#' @rdname assessmentDuration
#' @title Compute the average and total time for the surveys
#' @description This function compute the average and total time for the surveys
#' Warning: If there are uncorrected mistakes in the survey dates, it can lead to have the length of the survey in seconds and this check will not performed well
#'
#' @param ds dataset containing the survey (from kobo): data.frame
#' @param dates name of the fields where the information about the start and end date of the survey is stored: list of string (c('start_date','end_date'))
#'
#' @return dst same dataset as the inputed one but with survey marked for deletion if errors are found and delete=TRUE (or NULL)
#' @return ret_log list of the errors found (or NULL)
#' @return var a list of value (or NULL)
#' @return graph graphical representation of the results (or NULL)
#'
#' @author Yannick Pascaud
#'
#' @examples
#' {
#' ds <- HighFrequencyChecks::sample_dataset
#' dates <- c("survey_start","end_survey")
#'
#' list[dst,ret_log,var,graph] <- assessmentDuration(ds=ds,
#' dates=dates)
#' print(var)
#'}
#' @export assessmentDuration
assessmentDuration <- function(ds=NULL,
dates=NULL){
if(is.null(ds) | nrow(ds)==0 | !is.data.frame(ds)){
stop("Please provide the dataset")
}
if(is.null(dates) | !is.character(dates) | length(dates)!=2){
stop("Please provide the fields where the survey start and end date is stored (c('start_date','end_date'))")
}
surveytime <- as.double.difftime((readr::parse_datetime(as.character(ds[,dates[2]])) -
readr::parse_datetime(as.character(ds[,dates[1]]))),
units = "secs") / 60
# surveytime <- as.double.difftime((strptime(ds[,dates[2]],"%Y-%m-%dT%R") - strptime(ds[,dates[1]],"%Y-%m-%dT%R")), units = "secs")/60
# avg <- round(mean(surveytime), digits = 2)
# tot <- round(sum(surveytime), digits = 2)
msg<-paste0("The total time of data collection is ", round(sum(surveytime), digits = 2),
" minutes and the average time per survey is ", round(mean(surveytime), digits = 2), " minutes")
return(list(NULL,NULL,msg,NULL))
}
#' @name assessmentDurationOutliers
#' @rdname assessmentDurationOutliers
#' @title Report the outlier durations for the surveys
#' @description This function report the outlier durations for the surveys
#'
#' @param ds dataset containing the survey (from kobo): data.frame
#' @param dates name of the fields where the information about the start and end date of the survey is stored: list of string (c('start_date','end_date'))
#' @param sdval (Optional, by default set to 2) number of standard deviation for which the data within is considered as acceptable: integer
#' @param uniqueID name of the field where the survey unique ID is stored: string
#' @param enumeratorID name of the field where the enumerator ID is stored: string
#' @param reportingColumns (Optional, by default it is built from the enumeratorID and the UniqueID) name of the columns from the dataset you want in the result: list of string (c('col1','col2',...))
#'
#' @return dst same dataset as the inputed one but with survey marked for deletion if errors are found and delete=TRUE (or NULL)
#' @return ret_log list of the errors found (or NULL)
#' @return var a list of value (or NULL)
#' @return graph graphical representation of the results (or NULL)
#'
#' @author Yannick Pascaud
#'
#' @examples
#' {
#' ds <- HighFrequencyChecks::sample_dataset
#' dates <- c("survey_start","end_survey")
#' uniqueID <- "X_uuid"
#' enumeratorID <- "enumerator_id"
#' reportingColumns <- c(enumeratorID, uniqueID)
#' sdval<-2
#'
#' list[dst,ret_log,var,graph] <- assessmentDurationOutliers(ds=ds,
#' dates=dates,
#' sdval=sdval,
#' reportingColumns=reportingColumns)
#' head(ret_log,10)
#' print(graph)
#'}
#' @export assessmentDurationOutliers
assessmentDurationOutliers <- function(ds=NULL,
dates=NULL,
sdval=2,
reportingColumns=c(enumeratorID, uniqueID)){
if(is.null(ds) | nrow(ds)==0 | !is.data.frame(ds)){
stop("Please provide the dataset")
}
if(is.null(dates) | !is.character(dates) | length(dates)!=2){
stop("Please provide the fields where the survey start and end date is stored (c('start_date','end_date'))")
}
if(is.null(sdval) | !is.numeric(sdval)){
stop("Please provide the number of standard deviations you want to check for")
}
if(is.null(reportingColumns) | !is.character(reportingColumns)){
stop("Please provide the columns you want in the result (include the enumerator id column if you want to check by enumerator)")
}
surveytime <- data.frame(duration= as.double.difftime((readr::parse_datetime(as.character(ds[,dates[2]])) -
readr::parse_datetime(as.character(ds[,dates[1]]))),
units = "secs") / 60)
# surveytime <- data.frame(duration=as.double.difftime((strptime(ds[,dates[2]],"%Y-%m-%dT%R") - strptime(ds[,dates[1]],"%Y-%m-%dT%R")), units = "secs")/60)
duration_outliers <- data.frame(outliers::scores(surveytime, type = "z"))
tmp <- data.frame(ds[,reportingColumns],surveytime,duration_outliers)
colnames(tmp)[length(tmp)] <- "Zscore"
logf <- subset(tmp, abs(Zscore)>sdval)
graph <- ggplot2::ggplot(surveytime) + ggplot2::geom_boxplot(ggplot2::aes(duration), outlier.colour = "red") +
ggplot2::theme_light() +
ggplot2::theme(axis.text.y=element_blank(),
axis.line.y=element_blank(),
axis.ticks.y=element_blank(),
panel.grid.major.y=element_blank(),
panel.grid.minor.y=element_blank())
return(list(NULL,logf,NULL,graph))
}
#' @name assessmentProductivity
#' @rdname assessmentProductivity
#' @title Summary of daily average productivity
#' @description This function display the number of surveys conducted per day.
#'
#' @param ds dataset containing the survey (from kobo): data.frame
#' @param surveyDate name of the field in the dataset where the date of the survey is stored: string
#' @param dateFormat format used for the date: string ('\%m/\%d/\%Y')
#' @param surveyConsent name of the field in the dataset where the survey consent is stored: string
#'
#' @return dst same dataset as the inputed one but with survey marked for deletion if errors are found and delete=TRUE (or NULL)
#' @return ret_log list of the errors found (or NULL)
#' @return var a list of value (or NULL)
#' @return graph graphical representation of the results (or NULL)
#'
#' @author Yannick Pascaud
#'
#' @examples
#' {
#' ds <- HighFrequencyChecks::sample_dataset
#' surveyDate <- "survey_date"
#' dateFormat <- "%m/%d/%Y"
#' surveyConsent <- "survey_consent"
#'
#' list[dst,ret_log,var,graph] <- assessmentProductivity(ds=ds,
#' surveyDate=surveyDate,
#' dateFormat=dateFormat,
#' surveyConsent=surveyConsent)
#' head(ret_log,10)
#' print(graph)
#'}
#' @export assessmentProductivity
assessmentProductivity <- function(ds=NULL,
surveyDate=NULL,
dateFormat=NULL,
surveyConsent=NULL){
if(is.null(ds) | nrow(ds)==0 | !is.data.frame(ds)){
stop("Please provide the dataset")
}
if(is.null(surveyDate) | !is.character(surveyDate)){
stop("Please provide the field where the survey date is stored")
}
if(is.null(dateFormat) | !is.character(dateFormat)){
stop("Please provide the format used for the date ('%m/%d/%Y')")
}
if(is.null(surveyConsent) | !is.character(surveyConsent)){
stop("Please provide the field where the survey consent is stored")
}
tmp<-ds %>%
# group_by(surveydate=surveydate) %>%
group_by(surveydate=.data[[ surveyDate ]]) %>%
summarize(NbSurvey=n())
tmp$surveydate<-as.Date(tmp$surveydate, dateFormat)
logf<-tmp[with(tmp, order(surveydate)), ]
graph <- ggplot2::ggplot(tmp) +
ggplot2::geom_col(ggplot2::aes(x=surveydate, y=NbSurvey)) +
ggplot2::theme_minimal() +
ggplot2::theme(panel.grid.major.x=ggplot2::element_blank(), panel.grid.minor.x=ggplot2::element_blank()) +
ggplot2::labs(x = "Dates", y="Number of surveys")
return(list(NULL,logf,NULL,graph))
}
#' @name assessmentDailyValidSurveys
#' @rdname assessmentDailyValidSurveys
#' @title Daily number of survey per consent status
#' @description This function display the number of surveys conducted per day per constent status.
#'
#' @param ds dataset containing the survey (from kobo): data.frame
#' @param surveyDate name of the field in the dataset where the date of the survey is stored: string
#' @param dateFormat format used for the date: string ('\%m/\%d/\%Y')
#' @param surveyConsent name of the field in the dataset where the survey consent is stored: string
#'
#' @return dst same dataset as the inputed one but with survey marked for deletion if errors are found and delete=TRUE (or NULL)
#' @return ret_log list of the errors found (or NULL)
#' @return var a list of value (or NULL)
#' @return graph graphical representation of the results (or NULL)
#'
#' @author Yannick Pascaud
#'
#' @examples
#' {
#' ds <- HighFrequencyChecks::sample_dataset
#' surveyDate <- "survey_date"
#' dateFormat <- "%m/%d/%Y"
#' surveyConsent <- "survey_consent"
#'
#' list[dst,ret_log,var,graph] <- assessmentDailyValidSurveys(ds=ds,
#' surveyDate=surveyDate,
#' dateFormat=dateFormat,
#' surveyConsent=surveyConsent)
#' head(ret_log,10)
#' print(graph)
#'}
#' @export assessmentDailyValidSurveys
assessmentDailyValidSurveys <- function(ds=NULL,
surveyDate=NULL,
dateFormat=NULL,
surveyConsent=NULL){
if(is.null(ds) | nrow(ds)==0 | !is.data.frame(ds)){
stop("Please provide the dataset")
}
if(is.null(surveyDate) | !is.character(surveyDate)){
stop("Please provide the field where the survey date is stored")
}
if(is.null(dateFormat) | !is.character(dateFormat)){
stop("Please provide the format used for the date ('%m/%d/%Y')")
}
if(is.null(surveyConsent) | !is.character(surveyConsent)){
stop("Please provide the field where the survey consent is stored")
}
tmp <- ds %>% group_by(surveydate=.data[[surveyDate]]) %>% count(.data[[surveyConsent]])
colnames(tmp)[2] <- "surveyConsent"
tmp$surveydate <- as.Date(tmp$surveydate, dateFormat)
tmp <- tmp[with(tmp, order(surveydate)), ]
logf <- reshape2::dcast(tmp,surveydate ~ surveyConsent, value.var="n")
logf[is.na(logf)] <- 0
graph <- ggplot2::ggplot(tmp) +
ggplot2::geom_col(ggplot2::aes(x=surveydate, y=n, fill=surveyConsent)) +
ggplot2::theme_minimal() +
ggplot2::theme(panel.grid.major.x=ggplot2::element_blank(), panel.grid.minor.x=ggplot2::element_blank()) +
ggplot2::labs(x = "Dates", y="Number of surveys", fill="Consent status")
return(list(NULL,logf,NULL,graph))
}
#' @name assessmentTrackingSheet
#' @rdname assessmentTrackingSheet
#' @title Overall tracking sheet
#' @description This function display the overall tracking sheet.
#'
#' @param ds dataset containing the survey (from kobo): data.frame
#' @param dsSite name of the field in the dataset where the site is stored: string
#' @param sampleSizeTable dataset containing the sampling frame: data.frame
#' @param sampleSizeTableSite name of the field in the sampling frame where the site is stored: string
#' @param sampleSizeTableTarget name of the field where the target number of survey is stored in the sampling frame: string
#' @param sampleSizeTableAvailable name of the field where the number of points generated is stored in the sampling frame: string
#' @param surveyConsent name of the field in the dataset where the survey consent is stored: string
#' @param consentForValidSurvey value defined in the kobo form to acknowledge the surveyed person gave his consent: string
#'
#' @return dst same dataset as the inputed one but with survey marked for deletion if errors are found and delete=TRUE (or NULL)
#' @return ret_log list of the errors found (or NULL)
#' @return var a list of value (or NULL)
#' @return graph graphical representation of the results (or NULL)
#'
#' @author Yannick Pascaud
#'
#' @examples
#' {
#' ds <- HighFrequencyChecks::sample_dataset
#' dsSite <- "union_name"
#' sampleSizeTable <- HighFrequencyChecks::SampleSize
#' sampleSizeTableSite <- "Union"
#' sampleSizeTableTarget <- "SS"
#' sampleSizeTableAvailable <- "TotPts" # Usually the Target + a buffer
#' surveyConsent <- "survey_consent"
#' consentForValidSurvey <- "yes" # consent value for yes
#'
#' list[dst,ret_log,var,graph] <- assessmentTrackingSheet(ds=ds,
#' dsSite=dsSite,
#' sampleSizeTable=sampleSizeTable,
#' sampleSizeTableSite=sampleSizeTableSite,
#' sampleSizeTableTarget=sampleSizeTableTarget,
#' sampleSizeTableAvailable=sampleSizeTableAvailable,
#' surveyConsent=surveyConsent,
#' consentForValidSurvey=consentForValidSurvey)
#' head(ret_log,10)
#' print(graph)
#'}
#' @export assessmentTrackingSheet
assessmentTrackingSheet <- function(ds=NULL,
dsSite=NULL,
sampleSizeTable=NULL,
sampleSizeTableSite=NULL,
sampleSizeTableTarget=NULL,
sampleSizeTableAvailable=NULL,
surveyConsent=NULL,
consentForValidSurvey=NULL){
if(is.null(ds) | nrow(ds)==0 | !is.data.frame(ds)){
stop("Please provide the dataset")
}
if(is.null(sampleSizeTable) | nrow(sampleSizeTable)==0 | !is.data.frame(sampleSizeTable)){
stop("Please provide the sampling frame")
}
if(is.null(dsSite) | !is.character(dsSite)){
stop("Please provide the field where the site is stored in the dataset")
}
if(is.null(sampleSizeTableSite) | !is.character(sampleSizeTableSite)){
stop("Please provide the field where the site is stored in the sampling frame")
}
if(is.null(surveyConsent) | !is.character(surveyConsent)){
stop("Please provide the field where the survey consent is stored")
}
if(is.null(sampleSizeTableTarget) | !is.character(sampleSizeTableTarget)){
stop("Please provide the field where the target number of survey is stored in the sampling frame")
}
if(is.null(sampleSizeTableAvailable) | !is.character(sampleSizeTableAvailable)){
stop("Please provide the field where the number of points generated is stored in the sampling frame")
}
if((is.null(consentForValidSurvey) | !is.character(consentForValidSurvey))){
stop("Please provide either the value for in the consent for the valid surveys")
}
df1<-data.frame(sampleSizeTable[,sampleSizeTableSite], sampleSizeTable[,sampleSizeTableTarget], sampleSizeTable[,sampleSizeTableAvailable], stringsAsFactors = FALSE)
colnames(df1)<-c("site",sampleSizeTableTarget,sampleSizeTableAvailable)
ds[,surveyConsent][ds$survey_consent!=consentForValidSurvey]<-"no"
df2 <- data.frame(ds %>%
group_by(site=.data[[ dsSite ]],
consent=.data[[ surveyConsent ]]) %>%
summarize(n=n()) %>%
mutate(done=sum(n)), stringsAsFactors = FALSE)
tmp <- merge(df1[c("site",sampleSizeTableTarget,sampleSizeTableAvailable)],
df2[df2$consent==consentForValidSurvey,c('site', "n", "done")],
by=c("site"))
tmp$toDo <- tmp[,sampleSizeTableTarget]-tmp$n
tmp$RemainingPoints <- tmp[,sampleSizeTableAvailable]-tmp$done
colnames(tmp) <- c("Site", "Target", "PointsAvailable", "ValidSurveys", "TotalSurveyDone", "ToDo", "RemainingPoints")
logf <- tmp
alertText <- "There is not enough points available to reach the sample size for: \n"
alertTextToBeDisplayed <- FALSE
for(i in 1:length(tmp[,1])){
if(((tmp[i,]$RemainingPoints - tmp[i,]$ToDo) < 0)){
alertText <- paste(alertText, tmp[i,1], sep = " \n")
alertTextToBeDisplayed <- TRUE
}
}
if(!alertTextToBeDisplayed){
alertText <- NULL
}
tmp <- reshape2::melt(tmp[,c("Site", "ValidSurveys", "TotalSurveyDone", "ToDo", "RemainingPoints")], id="Site")
tmp$grph[tmp$variable=="ValidSurveys" | tmp$variable=="ToDo"]<-"Graph1"
tmp$grph[tmp$variable=="TotalSurveyDone" | tmp$variable=="RemainingPoints"]<-"Graph2"
tmp <- tmp %>%
mutate(
gap = case_when(
variable == "ValidSurveys" ~ 0.2,
variable == "TotalSurveyDone" ~ 0.6,
variable == "ToDo" ~ 0.2,
variable == "RemainingPoints" ~ 0.6
)
)
cpt=0
for(i in unique(tmp$Site)){
tmp$x[tmp$Site==i] <- cpt + tmp[tmp$Site==i,]$gap
cpt=cpt+1
}
graph <- ggplot2::ggplot() +
ggplot2::geom_col(ggplot2::aes(x = x, y = value, fill = variable), data=subset(tmp, grph=="Graph2"), orientation = "x", width = 0.4,
position = ggplot2::position_stack(reverse = TRUE)) +
ggplot2::geom_col(ggplot2::aes(x = x, y = value, fill = variable), data=subset(tmp, grph=="Graph1"), orientation = "x", width = 0.4,
position = ggplot2::position_stack(reverse = TRUE)) +
ggplot2::scale_x_continuous(
breaks = tmp$x[tmp$variable == "ToDo"] + 0.2,
labels = tmp$Site[tmp$variable == "ToDo"]) +
ggplot2::theme_minimal() +
ggplot2::theme(panel.grid.major.y=ggplot2::element_blank()) +
ggplot2::labs(x = "Sites", y="Number of surveys", fill="Categories") +
ggplot2::coord_flip()
return(list(NULL,logf,alertText,graph))
}