Skip to content

Commit 8627661

Browse files
committed
merging
2 parents 1e4afad + a11a192 commit 8627661

3 files changed

Lines changed: 54 additions & 14 deletions

File tree

code/scenarios/accra/master_script_rj.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ ithim_object <- run_ithim_setup(NSAMPLES = 1024,
9191
CHRONIC_DISEASE_SCALAR = c(log(1), log(1.2)),
9292
BACKGROUND_PA_SCALAR = c(log(1), log(1.2)),
9393
MC_TO_CAR_RATIO = c(-1.4,0.4),
94-
PA_DOSE_RESPONSE_QUANTILE = T,
95-
AP_DOSE_RESPONSE_QUANTILE = T)
94+
PA_DOSE_RESPONSE_QUANTILE = F,
95+
AP_DOSE_RESPONSE_QUANTILE = F)
9696

9797
numcores <- detectCores()
9898
ithim_object$outcomes <- mclapply(1:NSAMPLES, FUN = ithim_uncertainty, ithim_object = ithim_object, mc.cores = ifelse(Sys.info()[['sysname']] == "Windows", 1, numcores))

code/scenarios/sao paulo/streamline_travel_survey.R

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,12 @@ rd <- rename(rd, participant_id = ID_PESS ,
4747
# Recode modes as strings
4848
mode_df <- data.frame(
4949
mode_int = append(c(1:17), NA),
50-
mode_string = c(rep('bus', 5), 'car_driver',
51-
'car_passenger', 'taxi',
52-
rep('van', 3), 'subway',
50+
mode_string = c(rep('bus', 5), rep('car', 2),
51+
'taxi', rep('van', 3),
52+
'subway',
5353
'train', 'motorbike',
5454
'bicycle', 'walk', 'others', NA)
5555

56-
57-
5856
)
5957

6058
# Convert numeric to string modes
@@ -76,10 +74,27 @@ ggplot(rd %>%
7674
geom_text(aes(label = perc), position = position_dodge(width=0.9), vjust=-0.25, color = "blue") +
7775
theme_minimal() +
7876
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
79-
labs(x = "", y = "percentage(%)", title = "Main Mode distribution")
77+
labs(x = "", y = "percentage(%)", title = "Main Mode distribution - without weights")
78+
# )
79+
80+
sum_total_trip_weight <- sum(rd$FE_VIA, na.rm = T)
81+
82+
# plotly::ggplotly(
83+
ggplot(rd %>%
84+
filter(!is.na(trip_mode)) %>%
85+
group_by(trip_mode) %>%
86+
summarise(sum_trip_weights = sum(FE_VIA)) %>%
87+
mutate(perc = round(sum_trip_weights/sum(sum_trip_weights) * 100, 1)),
88+
aes(x = trip_mode, y = perc)) +
89+
geom_bar(position = 'dodge', stat='identity') +
90+
geom_text(aes(label = perc), position = position_dodge(width=0.9), vjust=-0.25, color = "blue") +
91+
theme_minimal() +
92+
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
93+
labs(x = "", y = "percentage(%)", title = "Main Mode distribution - with weights")
8094
# )
8195

8296

97+
8398
# Define distance categories
8499
dist_cat <- c("0-6 km", "7-9 km", "10+ km")
85100

@@ -99,4 +114,33 @@ ggplot(rd %>%
99114
geom_text(aes(label = perc), position = position_dodge(width=0.9), vjust=-0.25, color = "blue") +
100115
theme_minimal() +
101116
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
102-
labs(x = "", y = "percentage(%)", title = "Main Mode Distance distribution")
117+
labs(x = "", y = "percentage(%)", title = "Main Mode Distance distribution")
118+
119+
# Calculate mode speed from the dataset, but using mean distance and duration
120+
# Remove all trips with multiple modes
121+
# Using only commute mode as a proxy
122+
mode_speed <- rd %>% filter(is.na(MODO2) & is.na(MODO3) & is.na(MODO4) &
123+
((MOTIVO_O %in% c(1, 2, 3) & MOTIVO_D %in% 8) |
124+
(MOTIVO_D %in% c(1, 2, 3) & MOTIVO_O %in% 8))
125+
) %>% group_by(trip_mode) %>%
126+
summarise(mean (trip_distance),
127+
speed = (mean(trip_distance)) / (mean(trip_duration) / 60))
128+
129+
130+
# source_modes <- c('Bus', 'Walking')
131+
# target_modes <- c('Private Car')
132+
#
133+
# source_percentages <- c(0.16, 0.49)
134+
#
135+
# tt <- nrow(filter(rdr, ! trip_mode %in% c('99', 'Short Walking')))
136+
#
137+
# rdr <- create_scenario(rdr, scen_name = 'Scenario 1', source_modes = source_modes,
138+
# target_modes = target_modes, source_distance_cats = dist_cat,
139+
# source_trips = c(round(source_percentages[1] * tt),
140+
# round(source_percentages[2] * tt)))
141+
#
142+
# rdfinal <- rbind(rd, rdr)
143+
144+
#rdr %>% filter(rdfinal, scenario == 'Scenario 1' & ! trip_mode %in% c('Short Walking', "99", "Train", "Other", "Unspecified")) %>%
145+
# group_by(trip_mode) %>% summarise(count = n(), pert = n() / nrow(.) * 100)
146+

ithim_r_functions.R

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,6 @@ ithim_setup_parameters <- function(NSAMPLES = 1,
181181
for(age in unique(dr_ap$age_code)){
182182
dr_ap_age <- subset(dr_ap,age_code==age)
183183
#######################################
184-
185-
186184
lbeta <- log(dr_ap_age$beta)
187185
lgamma <- log(dr_ap_age$gamma)
188186
gamma_val <- quantile(density(lgamma),quant1)
@@ -393,7 +391,7 @@ edit_accra_trips <- function(raw_trip_set){
393391
total_mc_distance <- total_car_distance*VEHICLE_INVENTORY$distance_ratio_to_car[VEHICLE_INVENTORY$trip_mode==new_mode]
394392
mc_duration <- total_mc_distance/VEHICLE_INVENTORY$speed[VEHICLE_INVENTORY$trip_mode==new_mode]*60
395393
residual_mc_duration <- mc_duration - sum(subset(raw_trip_set,trip_mode==new_mode)$trip_duration)
396-
duration_range <- 15:100
394+
#duration_range <- 15:100
397395
nTrips <- 1
398396
nPeople <- 20#round(residual_mc_duration/nTrips/mean(duration_range))
399397
duration <- residual_mc_duration/nPeople
@@ -636,7 +634,6 @@ create_all_scenarios <- function(trip_set){
636634
mcycle_trips_sample <- create_scenario(rdr, scen_name = 'Scenario 3', source_modes = source_modes,
637635
combined_modes = T, target_modes = target_modes,
638636
source_distance_cats = DIST_CAT, source_trips = target_new_trips)
639-
640637
# Update selected rows for mode and duration
641638
rdr$trip_mode[match(mcycle_trips_sample$row_id,rdr$row_id)] <- mcycle_trips_sample$trip_mode
642639
rdr$trip_duration[match(mcycle_trips_sample$row_id,rdr$row_id)] <- mcycle_trips_sample$trip_duration
@@ -1219,7 +1216,6 @@ combined_rr_pa_pa <- function(ind_pa,ind_ap){
12191216
}
12201217

12211218
injuries_function_2 <- function(true_distances,injuries_list,reg_model){
1222-
12231219
## For predictive uncertainty, we could sample a number from the predicted distribution
12241220
injuries <- true_distances
12251221
injuries$Bus_driver <- 0

0 commit comments

Comments
 (0)