-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocio-economics_engine.R
170 lines (159 loc) · 4.39 KB
/
socio-economics_engine.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
# --------------------------- Occupation and Income -------------------------- #
employment_generator <- function(age, state, seed_list) {
if (age < 14) {
return(FALSE)
}
region <- state_regions[state]
unemployment_chance <- unemployment_rate[region]
employment_chance <- 1 - unemployment_chance
set.seed(seed_list)
employment <- sample(
c(TRUE, FALSE),
size = 1,
prob = c(employment_chance, unemployment_chance)
)
employment <- unname(employment)
return(employment)
}
# Data generator because I'll create both the occupation and the income
occupation_data_generator <- function(age, state, city, race, employment, seed_list) {
for (group in seq_along(occupation_age_groups)) {
if (age %in% occupation_age_groups[[group]]) {
age_group <- names(occupation_age_groups[group]) # I may need to use as.numeric()
break # if this works, add break to activity_generator()
}
}
# print(age_group)
city_code <- filter(
city_codes,
.data[["state"]] == state,
.data[["city"]] == city
) %>%
pull(code)
city_code <- substring(as.character(city_code), 1, 6)
if (age >= 14) {
database <- occupation_database[[state]] %>%
filter(
.data[["Faixa Etária"]] == age_group,
.data[["Município"]] == city_code,
.data[["Raça Cor"]] == race_codes[race],
.data[["Sexo Trabalhador"]] == sex_codes[sex]
)
if (nrow(database) == 0) employment <- FALSE # some criteria simply don't have data
# print(nrow(database))
}
set.seed(seed_list)
if (employment) {
index <- sample(
x = seq_len(nrow(database)),
size = 1
)
occupation <- database[index, ] %>%
pull("CBO Ocupação 2002")
income <- database[index, ] %>%
pull("Vl Remun Média (SM)")
if (income == 0) income <- 1 # because there are some 0 velues for income
} else {
if (age < 14) {
occupation <- NA
income <- NA
}
occupation <- sample(
x = list("Do lar", NA),
size = 1,
prob = c(house_chance, nothing_chance)
)
income <- NA
}
return(list(occupation, income))
}
# ------------------------- Internet Access and Speed ------------------------ #
has_internet_access <- function(situation, seed_list) {
set.seed(seed_list)
has_internet <- sample(
x = c(TRUE, FALSE),
size = 1,
prob = c(
internet_access_chances[situation],
1 - internet_access_chances[situation]
)
)
has_internet <- unname(has_internet)
return(has_internet)
}
internet_access_generator <- function(has_internet, seed_list) {
if (has_internet) {
set.seed(seed_list)
internet_class <- sample(
x = names(internet_classes),
size = 1,
prob = dnorm(
x = seq_len(length(internet_classes)),
mean = ceiling(length(internet_classes) / 2),
sd = length(internet_classes) / 6
)
)
set.seed(seed_list)
internet_access <- runif(
n = 1,
min = internet_classes[[internet_class]][1],
max = internet_classes[[internet_class]][2]
)
internet_access <- round(internet_access, 1)
} else {
internet_access <- NA
}
internet_access <- unname(internet_access)
return(internet_access)
}
# ----------------------------- Health Insurance ----------------------------- #
health_insurance_generator <- function(state, seed_list) {
region <- state_regions[state]
set.seed(seed_list)
health_insurance <- sample(
x = c("S", "N"),
size = 1,
prob = c(
health_insurance_chances[region],
1 - health_insurance_chances[region]
)
)
health_insurance <- unname(health_insurance)
return(health_insurance)
}
# ------------------------------ Private School ------------------------------ #
generate_school_type <- function(
age,
reading,
state,
city,
sex,
race,
income_minimum_wage,
seed_list
) {
if (!validate_school_type(age, reading)) {
return(NA)
}
city_code <- find_city_code(state, city)
if (!city_code %in% private_school_data$CO_MUNICIPIO) return("Pública")
for (group in seq_along(school_age_groups)) {
if (age %in% school_age_groups[[group]]) {
age_group <- names(school_age_groups[group])
}
}
prob <- calculate_school_type_chances(
sex,
race,
age_group,
income_minimum_wage,
city_code
)
set.seed(seed_list)
school_type <- sample(
x = c("Privada", "Pública"),
size = 1,
prob = prob
)
return(school_type)
}