This repository has been archived by the owner on Oct 24, 2023. It is now read-only.
forked from Edouard-Legoupil/koboloadeR
/
kobo_weight.R
200 lines (155 loc) · 6.28 KB
/
kobo_weight.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
#' @name kobo_weight
#' @rdname kobo_weight
#' @title Weight a datset
#' @description Automatically weight the data according to the information of 0-config.R
#' @param mainDir Path to the project's working directory: mainly for shiny app
#' @author Elliott Messeiller
#'
#'
#' @export kobo_weight
#'
#' @examples
#' \dontrun{
#' kobo_weight()
#' }
#'
#'
kobo_weight <- function(mainDir = '') {
if (mainDir == '') {
mainDir <- getwd()
}
source(paste0(mainDir, "/code/0-config.R"), local = TRUE)
sampling <- readxl::read_excel(path.to.form, sheet = "sampling_frame")
data$weight <- ""
if (usedweight == 'sampling_frame') {
if (usedsampling == '2_stages') {
stratas <- sampling
stratified <- unique(sampling$strata)
name_col_sample <- names(sampling)
normal_col <- c("id_sampl", "Survey","pop", "psu","SumDist","proba","survey_buffer")
if (sum(normal_col %in% name_col_sample) == length(normal_col)) {
if (length(stratified) > 1) {
normal_col <- c("X__1", "id_sampl","Survey","pop","psu","SumDist","proba","survey_buffer")
stratas <- unique(stratas[setdiff(names(stratas), normal_col)])
}
if (length(stratified) == 1) {
normal_col <- c("X__1","id_sampl","Survey","strata","pop","psu","SumDist","proba","survey_buffer")
stratas <- unique(stratas[setdiff(names(stratas), normal_col)])
}
if (length(unique(stratas$strata %in% dico$name)) == 1) {
strat_row_n <- match(stratas[1, "strata"], dico$name)
fullname_strata <- as.character(dico[strat_row_n, "fullname"])
fullname_strata <- data.frame(strsplit(fullname_strata, "\\."))
fullname_strata <- data.frame(fullname_strata[-nrow(fullname_strata), ])
fullname_strata <- as.character(fullname_strata[nrow(fullname_strata), ])
names(stratas)[names(stratas) == "strata"] <- fullname_strata
names(sampling)[names(sampling) == "strata"] <- fullname_strata
fullname_strata <- as.character(dico[strat_row_n,"qlevel"])
}
col_stratas <- data.frame(colnames(stratas), stringsAsFactors = FALSE)
nrow_su <- data.frame(Strata=character(), nsu = numeric(), stringsAsFactors = FALSE)
for (j in 1:nrow(col_stratas)) {
split_temp <- as.character(col_stratas[j,1])
nrow_su[j,"Strata"] <- split_temp
nrow_su[j, "nsu"] <- nrow(unique(sampling[split_temp]))
names(stratas)[names(stratas) == split_temp] <- as.character(dico[dico$name == split_temp, c("fullname"), ])
names(sampling)[names(sampling) == split_temp] <- as.character(dico[dico$name == split_temp, c("fullname"), ])
}
psu_name <- nrow_su[which.max(nrow_su$nsu),"Strata"]
psu_fullname <- as.character(dico[dico$name==psu_name, c("fullname"),])
n_col_stratas <- ncol(stratas)
names_stratas <- names(stratas)
sampling$actual_sample <- ""
sampling$weight <- ""
tot_pop <- sum(sampling$pop)
for (j in 1:nrow(sampling)) {
v_psu <- as.character(sampling[j,psu_fullname])
sampling[j, "actual_sample"] <- as.numeric(sum(data[, psu_fullname] == v_psu))
}
sampling$actual_sample <- as.numeric(sampling$actual_sample)
tot_sample <- sum(sampling$actual_sample)
strata_pop <- data.frame(strata=character(),pop=integer(),sample=integer(), weight=integer(),stringsAsFactors = F)
for (k in 1:length(stratified)){
strata_pop[k,"strata"] <- stratified[k]
strata_pop[k, "pop"] <- sum(sampling[which(sampling[,fullname_strata]==stratified[k]),"pop"])
strata_pop[k, "sample"] <- sum(sampling[which(sampling[,fullname_strata]==stratified[k]),"actual_sample"])
strata_pop[k, "weight"] <- (strata_pop[k, "pop"] / tot_pop) / (strata_pop[k, "sample"] / tot_sample)
}
for (i in 1:nrow(data)) {
stratum <- as.character(data[i,fullname_strata])
result <- as.numeric(strata_pop[strata_pop$strata == stratum, c("weight"), ])
data[i, "weight"] <- result
}
data$weight <- as.numeric(data$weight)
surveydesign <- survey::svydesign(
ids = ~ 1,
strata = data[[fullname_strata]],
weights = ~ weight,
data = data
)
pastedesign <- paste0("survey::svydesign(ids=~1,
strata= data[[strata1]],
weights= ~weight,
data=data)")
# weight2dico <- data.frame(matrix("weight", ncol = 13))
# names(weight2dico) <-
# c(
# "type",
# "name",
# "fullname",
# "label",
# "disaggregation",
# "correlate",
# "listname",
# "qlevel",
# "qgroup",
# "labelchoice",
# "ordinal",
# "weight",
# "formpart"
# )
# dico <- rbind(dico, weight2dico)
#
# # Rewritting dico file
# write.csv(
# dico,
# paste0(mainDir,"/data/dico_", form, ".csv"),
# row.names = FALSE,
# na = ""
# )
#
# Coherce data to a clean dataframe
data <- data.frame(data)
#Write data with weights
readr::write_csv(data, file = paste0(mainDir,"/data/data.csv"))
path.to.data <- paste0(mainDir, "/data/data.csv")
#Fetching the directory
#Path to file
configfile <- paste(mainDir, "/code/0-config.R", sep = "")
#Writting file
sink(configfile, append = TRUE)
cat("\n")
cat(paste0('data <- readr::read_csv("',path.to.data,'")'))
cat("\n")
cat(paste('strata1 <- "', fullname_strata, '"', sep = ""))
cat("\n")
cat(paste('design <- ', pastedesign, sep = ""))
cat("\n")
sink()
}
else{
cat("You didn't use the R Sampling tool or your sampling frame is not valid. ")
cat(
"Copy paste the input of https://oliviercecchi.shinyapps.io/R_sampling_tool_v2/ in the sampling_frame sheet "
)
}
}
if (usedsampling == "cluster_sampling") {
}
if (usedsampling == "simple_random") {
}
}
if (usedweight == "custom") {
}
}
NULL