-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_Prep_Wtsample_Freq_Table.R
254 lines (198 loc) · 7.02 KB
/
2_Prep_Wtsample_Freq_Table.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
# Run Custom_Path.R if necessary.
rm(list = ls()) # Remove objects
getwd()
# Run the code file with functions.
source("Functions/1_Functions.R")
# Disable scientific notation.
options(scipen = 999)
library("dplyr") # data manipulation
library("ggplot2") # visualisation
library("readr") # read large csv file
library("readxl") # read Excel file
library("writexl") # export to Excel
library("janitor") # cross-tabulation
# If the Census data or a weighted sample data
# are available, users consult Part 1A.
# If these data are unavailable, see
# Part 1B, Generate a weighted sample data.
##H ----------------------------------------
### > PREP PART 1A. ----
### Use the existing weighted sample data ----
### Steps 1-3
##H ----------------------------------------
##H ----------------------------------
### > Step 1: Read in the data ----
##H ----------------------------------
# Please customise
# df <- read_csv("custom_wtsample.csv")
##H ----------------------------------
## > For demonstration
##H ----------------------------------
load("pop_u_short_public_release_5vars.RData")
df <- pop_u_short_public_release_5vars
# rename, keep variables of interest and sort
df <- df %>%
arrange(geog1, sex, agecode1, eth_code5, econg)
# head(df)
#H ---------------------------------------
## > Summary table ----
#H--------------------------------------
# ALLOW A MINUTE TO EXECUTE fn_CreateTableOne_table()
# fn_CreateTableOne_table
# One needs to install "tableone" package.
# Ignore this if installation is unsuccessful.
# The function prints a summary
# distribution table and save it as .txt file.
# The generated txt file launches automatically.
# txtfile <- "wtsample_summary.txt"
# fn_CreateTableOne_table()
##H ----------------------------------
### > Step 3: Obtain the frequency table ----
##H ----------------------------------
fn_maxvar5_freq_table()
##H ----------------------------------
## > Step 4. Check ----
## Check whether the total adds up to 1
##H ----------------------------------
dim(freq_table)
tail(freq_table)
freq_table[1:8, 1:9]
# View(freq_table)
# Manual, check the total.
sum(freq_table[1:6, "n"])
sum(freq_table[1:6, "p"])
# check the total.
freq_table %>% group_by(by3, by1) %>%
summarise(sum_freq = sum(n), sum_p = sum(p))
##H ----------------------------------
### > Testing with 4 variables
##H ----------------------------------
# Please customise depending on the maxvar.
# Users may use fn_maxvar4_freq_table() or
# fn_maxvar6_freq_table().
# df <- df %>%
# select(geog1, sex, agecode1, eth_code5)
# fn_maxvar4_freq_table()
#H---------------------------------------
## > Step 5. Rename and save ----
#H--------------------------------------
Weightedsample_freq_table <- freq_table %>%
dplyr::select(seq, twdigits,
wtsample_n = n,
wtsample_perc = p,
everything())
# View(Weightedsample_freq_table)
##H ----------------------------------------
## > Step 6. Export outputs ----
##H ----------------------------------------
xlsxfile <- "Weightedsample_freq_table.xlsx"
fn_xlsx_path_file()
write_xlsx(Weightedsample_freq_table, xlsx_path_file)
# fn_xlsx_open()
##H ----------------------------------------
## > Step 7. Save RData (use for distance metrics)
##H ----------------------------------------
save(Weightedsample_freq_table,
file="Output/04-RData/Weightedsample_freq_table.RData")
#H-----------------------------------------------
#H This is for demo version. So, it may be overwritten.
#H----------------------------------------------
#===============================================
##H ----------------------------------------
## > PREP PART 1B. ----
## > Generate a weighted sample survey data ----
##> 0. Recap. Launch output files
##H ----------------------------------------
# xlsxfile <- "Weightedsample_freq_table.xlsx"
# fn_xlsx_open()
# file <- "WeightedSample_Data_Summary.txt"
#H---------------------------------------
## > Step 1. Load Census microdata ----
#H--------------------------------------
load("pop_u_short_public_release_5vars.RData")
ls()
df <- pop_u_short_public_release_5vars
dim(df) # obs = 1163659
names(df)
glimpse(df) ; head(df)
#H ---------------------------------------
## > Summary table ----
#H--------------------------------------
# SLOW. ALLOW A MINUTE TO EXECUTE fn_CreateTableOne_table
# One needs to install "tableone" package.
# Ignore this if installation is unsuccessful.
# The function prints a summary
# distribution table and save it as .txt file.
# The generated txt file launches automatically.
# txtfile <- "census_summary.txt"
# fn_CreateTableOne_table()
#H ---------------------------------------
## > Step 2. Draw a random sample ----
#H--------------------------------------
# 1. Draw a random sample 1:50 from census
# Prep random sampling
random50sample <- round(nrow(df)/50)
nrow(df)
random50sample # 23273
# Generate random numbers
# from uniform distribution
set.seed(-345)
df$decide <- runif(nrow(df), min = 0, max = 1)
summary(df$decide)
# hist(df$decide)
randomsample <- df %>% arrange(decide) %>%
mutate(decide_seq = row_number()) %>%
filter(decide_seq <= random50sample)
df <- randomsample %>%
dplyr::select(-starts_with("decide"))
dim(df); head(df) ;
#H---------------------------------------
##> Step 3. Obtain oneway, twoway Freq_tables ----
#H--------------------------------------
fn_maxvar5_freq_table()
# Now, n * 50, rename after checking's done!
freq_table <- freq_table %>%
rename(raw_n = n) %>%
mutate(n = raw_n * 50) %>%
dplyr::select(seq, twdigits,
raw_n, n, p, everything() )
# View(freq_table)
##H ----------------------------------
## > Step 4. Check ----
## Check whether the total adds up to 1
##H ----------------------------------
dim(freq_table)
tail(freq_table)
freq_table[1:8, 1:9]
# View(freq_table)
# Manual, check the total.
sum(freq_table[1:6, "n"])
sum(freq_table[1:6, "p"])
# automatic, check the total.
freq_table %>% group_by(by3, by1) %>%
summarise(sum_freq = sum(n), sum_p = sum(p))
#H---------------------------------
## > Step 5. Rename and save ----
#H---------------------------------
Weightedsample_freq_table <- freq_table %>%
dplyr::select(seq, twdigits,
wtsample_n = n,
wtsample_perc = p, everything())
# View(Weightedsample_freq_table)
##H ---------------------------------
## > Step 6. Export outputs ----
##H ---------------------------------
sheets <- list(
"Weightedsample_freq_table" = Weightedsample_freq_table,
"Random_sample" = randomsample)
xlsxfile <- "Weightedsample_freq_table.xlsx"
fn_xlsx_path_file()
write_xlsx(sheets, xlsx_path_file)
# fn_xlsx_open()
##H --------------------------------
### > Step 7. Save RData ----
##H --------------------------------
save(Weightedsample_freq_table,
file="Output/04-RData/Weightedsample_freq_table.RData")
save(var, file="Output/04-RData/var.RData")
### End ###