forked from PYannick/HighFrequencyChecks
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathisinterviewinthecorrectsite.R
143 lines (130 loc) · 7.56 KB
/
isinterviewinthecorrectsite.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
# WARNING - Generated by {fusen} from dev/function_documentation.Rmd: do not edit by hand
#' @name isInterviewInTheCorrectSite
#' @rdname isInterviewInTheCorrectSite
#' @title GIS check surveys for site
#' @description This function check that all interviews in the dataset were made in the correct site.
#' It is based on a GIS shapefile providing the boundaries of each site with their names.
#' The function is based on the GPS data filled in the survey to determine their location.
#' There is an option to automatically correct the site in the surveys whith the correct location.
#'
#' @param ds dataset containing the survey (from kobo): labelled data.frame
#' @param checkperiod if not null number of day before today when the check should be made
#' @param dsSite name of the field in the dataset where the site is stored: string
#' @param dsCoordinates name of the fields from the dataset where the information about the GPS coordinates are stored: list of string (c('Long','Lat'))
#' @param adminBoundaries dataset containing the shapefile of the site boundaries - Regardless the projection used for the shapefile, it is transformed to WGS84
#' @param adminBoundariesSite name of the field in the shapefile where the site is stored: 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
#' @param uniquerespondantID 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 uniquerespondantID) name of the columns from the dataset you want in the result: list of string (c('col1','col2',...))
#' @param correctIsInterviewInTheCorrectSite (Optional, by default set as FALSE) if TRUE, the site in the survey which is wrong will be replaced by the real one: boolean (TRUE/FALSE)
#'
#' @return result a list that includes:
#' * dst same dataset as the inputed one but with survey marked for deletion if errors are found and delete=TRUE (or NULL)
#' * ret_log list of the errors found (or NULL)
#' * var a list of value (or NULL)
#' * graph graphical representation of the results (or NULL)
#' @export isInterviewInTheCorrectSite
#'
#' @examples
#' 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"]])
isInterviewInTheCorrectSite <- function(ds=NULL,
dsSite=NULL,
dsCoordinates=NULL,
adminBoundaries=NULL,
adminBoundariesSite=NULL,
surveyConsent=NULL,
reportingColumns=c(enumeratorID, uniquerespondantID),
correctIsInterviewInTheCorrectSite=FALSE){
if(is.null(adminBoundaries) | !isS4(adminBoundaries) | nrow(adminBoundaries)==0){
stop("Please provide the spatial dataset of the boundaries shapefile")
}
if(is.null(ds) | nrow(ds)==0 | !is.data.frame(ds)){
stop("Please provide the dataset")
}
if(is.null(dsSite) | !is.character(dsSite)){
stop("Please provide the field where the site to check against is stored")
}
if(is.null(dsCoordinates) | !is.character(dsCoordinates) | length(dsCoordinates)!=2){
stop("Please provide the fields where the coordinates are stored (c('Long','Lat'))")
}
if(is.null(adminBoundariesSite) | !is.character(dsSite)){
stop("Please provide the field where the site in the shapefile is stored")
}
if(is.null(surveyConsent) | !is.character(surveyConsent)){
stop("Please provide the field where the survey consent is stored")
}
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)")
}
if(is.null(correctIsInterviewInTheCorrectSite) | !is.logical(correctIsInterviewInTheCorrectSite)){
stop("Please provide the correction action to be done (TRUE/FALSE)")
}
if(sp::is.projected(adminBoundaries)){
adm <- sp::spTransform(adminBoundaries, sp::CRS("+proj=longlat +ellps=WGS84 +datum=WGS84"))
}
dfsp <- ds
sp::coordinates(dfsp) <- dfsp[,c(dsCoordinates[1],dsCoordinates[2])]
sp::proj4string(dfsp) <- sp::proj4string(adminBoundaries)
dfsp_over_adm <- sp::over(dfsp,adminBoundaries)
fm <- data.frame(ds,dfsp_over_adm, stringsAsFactors = FALSE)
fm[,adminBoundariesSite][is.na(fm[,adminBoundariesSite])] <- ""
fm$check <- ifelse(fm[,dsSite] != fm[,adminBoundariesSite],"NOk","Ok")
if(correctIsInterviewInTheCorrectSite){
ds[,dsSite][fm$check=="NOk"] <- fm[,adminBoundariesSite][fm$check=="NOk"]
}
ret_log <- subset(fm,check=="NOk") %>%
dplyr::select(all_of(reportingColumns),
SiteRec=all_of(dsSite), SiteReal=all_of(adminBoundariesSite))
check <- data.frame(categories=c("OK", "NOK"),
Nb = c( length(ds[,1])-length(ret_log[,1]),
length(ret_log[,1])))
t1 <- check
t1$fraction <- t1$Nb / sum(t1$Nb)
t1 <- t1[order(t1$fraction), ]
t1$ymax <- cumsum(t1$fraction)
t1$ymin <- c(0, utils::head(t1$ymax, n=-1))
graph <- ggplot2::ggplot(t1, ggplot2::aes(fill=categories,
ymax=ymax,
ymin=ymin,
xmax=4,
xmin=3)) +
ggplot2::geom_rect(colour="grey30") +
ggplot2::coord_polar(theta="y") +
ggplot2::xlim(c(1, 4)) +
unhcrthemes::theme_unhcr(font_size = 14) +
ggplot2::theme(panel.grid=ggplot2::element_blank()) +
ggplot2::theme(axis.text=ggplot2::element_blank()) +
ggplot2::theme(axis.ticks=ggplot2::element_blank()) +
ggplot2::theme(legend.position='none') +
ggplot2::labs(title= "Are interviews taking place in the expected site?",
subtitle = paste0("Errors: ", round(t1$fraction[t1$categories == 'NOK']*100,2), "%"))
result <- list( dst = ds, # same dataset as the inputed one but with survey marked for deletion if errors are found and delete=TRUE (or NULL)
ret_log = ret_log , # list of the errors found (or NULL)
var = NULL, # a list of value (or NULL)
graph = graph) # graphical representation of the results (or NULL)
return(result)
}