@@ -224,10 +224,10 @@ ithim_setup_parameters <- function(NSAMPLES = 1,
224224 " SIN_EXPONENT_SUM_NOV" ,
225225 " SIN_EXPONENT_SUM_CYCLE" ,
226226 " SIN_EXPONENT_SUM_PED" ,
227- " CHRONIC_DISEASE_SCALAR" ,
228- " DISTANCE_SCALAR_CAR_TAXI" ,
229227 " DISTANCE_SCALAR_WALKING" ,
228+ " CHRONIC_DISEASE_SCALAR" ,
230229 " DISTANCE_SCALAR_PT" ,
230+ " DISTANCE_SCALAR_CAR_TAXI" ,
231231 " DISTANCE_SCALAR_CYCLING" ,
232232 " DISTANCE_SCALAR_MOTORCYCLE"
233233 )
@@ -244,6 +244,7 @@ ithim_setup_parameters <- function(NSAMPLES = 1,
244244 }
245245
246246
247+
247248 # MMET values with lognormal distribution
248249 # Define those variables and loop through them, sampling
249250 # from a pre-defined lognormal distribution if needed and add 1
@@ -307,15 +308,6 @@ ithim_setup_parameters <- function(NSAMPLES = 1,
307308
308309
309310
310-
311-
312-
313-
314-
315-
316-
317-
318-
319311 # Variables with beta distribution
320312 # Define those variables and loop through them, sampling
321313 # from a pre-defined beta distribution if needed
@@ -376,63 +368,62 @@ ithim_setup_parameters <- function(NSAMPLES = 1,
376368 # if PM_EMISSION_INVENTORY_CONFIDENCE<1, then sample those PM inventory values by
377369 # using Beta distributions
378370 if (PM_EMISSION_INVENTORY_CONFIDENCE < 1 ) {
379- pm_inv <- unlist(PM_EMISSION_INVENTORY )
380-
371+ pm_inv <- sort( unlist(PM_EMISSION_INVENTORY ) )
372+
381373 total <- sum(pm_inv )
382-
374+
383375 # ensure working with proportions that add up to 1
384376 if (total != 1 ){
385377 pm_inv <- pm_inv / total
386378 total <- sum(pm_inv )
387379 }
388-
380+
389381 # initialise output values
390382 estimate <- 0
391383 estimate_list <- list ()
392384 estimate_list [[' sum' ]] <- c(rep(0 , NSAMPLES ))
393-
385+
394386 conf <- PM_EMISSION_INVENTORY_CONFIDENCE
395387 # define standard deviation of beta distributions (sd also depends on emission proportion, see below)
396388 std <- (1 - conf )^ 1.2 / 16
397-
389+
398390 # loop through proportions in the inventory
399- for (i in 1 : (length(pm_inv ))){
391+ for (i in 1 : (length(pm_inv )- 1 )){
400392 if (pm_inv [i ]== 0 ){
401393 estimate <- 0
402394 estimate_list [[i + 1 ]] <- c(rep(0 , NSAMPLES ))
403395 } else {
404396 total <- total - estimate # calculate new total by subtracting the newest estimate
405- new_prop <- pm_inv [[i ]]
397+ new_prop <- pm_inv [[i ]]/ total
406398 mean <- new_prop # set mean to the be this new proportion
407399 sd <- std * mean ^ 0.25 # define standard deviation
408400 # calculate alpha and beta values of Beta distribution with above mean and std
409401 alpha <- (mean * (1 - mean )/ sd ^ 2 - 1 )* mean
410402 beta <- (mean * (1 - mean )/ sd ^ 2 - 1 )* (1 - mean )
411403 sample <- rbeta(NSAMPLES ,alpha , beta ) # sample from beta distribution
412- estimate <- sample
404+ estimate <- sample * total # re-scale to original proportion
413405 estimate_list [[i + 1 ]] <- estimate # save new estimate
414406 estimate_list [[' sum' ]] <- estimate_list [[' sum' ]] + estimate # sum across all estimates
415407 }
416408 }
417-
418- # scale back to 1
419- # sum <- c(rep(0, NSAMPLES)) # check
420-
421- for (i in 2 : length(estimate_list )){
422- estimate_list [[i ]] <- estimate_list [[i ]]/ estimate_list [[' sum' ]]
423- # sum <- sum + estimate_list[[i]] # check
424- }
425-
426- # get into correct format
409+
410+ # for final proportion
411+ estimate_list [[length(PM_EMISSION_INVENTORY )+ 1 ]] <- 1 - estimate_list [[' sum' ]]
412+
413+ # get into correct format
427414 for (j in 1 : NSAMPLES ){
428- PM_inventory_updated <- PM_EMISSION_INVENTORY
415+ pm_inv_updated <- pm_inv
429416 for (i in 2 : length(estimate_list )){
430- PM_inventory_updated [i - 1 ] <- estimate_list [[i ]][[j ]]
417+ pm_inv_updated [i - 1 ] <- estimate_list [[i ]][[j ]]
418+ }
419+ # get into correct order again
420+ PM_inventory_updated <- PM_EMISSION_INVENTORY
421+ for (k in names(PM_inventory_updated )){
422+ PM_inventory_updated [k ] <- pm_inv_updated [k ]
431423 }
432424 parameters $ PM_EMISSION_INVENTORY [[j ]] <- PM_inventory_updated
433425 }
434-
435-
426+
436427 }
437428
438429
@@ -451,76 +442,67 @@ ithim_setup_parameters <- function(NSAMPLES = 1,
451442 # if CO2_EMISSION_INVENTORY_CONFIDENCE<1, then sample those CO2 inventory values by
452443 # using Beta distributions
453444 if (CO2_EMISSION_INVENTORY_CONFIDENCE < 1 ) {
454- CO2_inv <- unlist(CO2_EMISSION_INVENTORY )
455-
445+ CO2_inv <- sort( unlist(CO2_EMISSION_INVENTORY ) )
446+
456447 total <- sum(CO2_inv )
457-
448+
458449 # ensure working with proportions that add up to 1
459450 if (total != 1 ){
460451 CO2_inv <- CO2_inv / total
461452 total <- sum(CO2_inv )
462453 }
463-
454+
464455 # initialise output values
465456 estimate <- 0
466457 estimate_list <- list ()
467458 estimate_list [[' sum' ]] <- c(rep(0 , NSAMPLES ))
468-
459+
469460 conf <- CO2_EMISSION_INVENTORY_CONFIDENCE
470461 # define standard deviation of beta distributions (sd also depends on emission proportion, see below)
471462 std <- (1 - conf )^ 1.2 / 16
472-
463+
473464 # loop through proportions in the inventory
474- for (i in 1 : (length(CO2_inv ))){
465+ for (i in 1 : (length(CO2_inv )- 1 )){
475466 if (CO2_inv [i ]== 0 ){
476467 estimate <- 0
477468 estimate_list [[i + 1 ]] <- c(rep(0 , NSAMPLES ))
478469 } else {
479470 total <- total - estimate # calculate new total by subtracting the newest estimate
480- new_prop <- CO2_inv [[i ]]
471+ new_prop <- CO2_inv [[i ]] / total # re-calculate proportions of ith value in inventory
481472 mean <- new_prop # set mean to the be this new proportion
482473 sd <- std * mean ^ 0.25 # define standard deviation
483474 # calculate alpha and beta values of Beta distribution with above mean and std
484475 alpha <- (mean * (1 - mean )/ sd ^ 2 - 1 )* mean
485476 beta <- (mean * (1 - mean )/ sd ^ 2 - 1 )* (1 - mean )
486477 sample <- rbeta(NSAMPLES ,alpha , beta ) # sample from beta distribution
487- estimate <- sample
478+ estimate <- sample * total # re-calculate the proportion of this sample from 1
488479 estimate_list [[i + 1 ]] <- estimate # save new estimate
489480 estimate_list [[' sum' ]] <- estimate_list [[' sum' ]] + estimate # sum across all estimates
490481 }
491482 }
492-
493- # scale back to 1
494- # sum <- c(rep(0, NSAMPLES)) # check
495-
496- for (i in 2 : length(estimate_list )){
497- estimate_list [[i ]] <- estimate_list [[i ]]/ estimate_list [[' sum' ]]
498- # sum <- sum + estimate_list[[i]] # check
499- }
500-
483+
484+ # for final proportion
485+ estimate_list [[length(CO2_EMISSION_INVENTORY )+ 1 ]] <- 1 - estimate_list [[' sum' ]]
486+
501487 # get into correct format
502488 for (j in 1 : NSAMPLES ){
503- CO2_inventory_updated <- CO2_EMISSION_INVENTORY
489+ co2_inv_updated <- CO2_inv
504490 for (i in 2 : length(estimate_list )){
505- CO2_inventory_updated [i - 1 ] <- estimate_list [[i ]][[j ]]
491+ co2_inv_updated [i - 1 ] <- estimate_list [[i ]][[j ]]
492+ }
493+ # get into correct order again
494+ CO2_inventory_updated <- CO2_EMISSION_INVENTORY
495+ for (k in names(CO2_inventory_updated )){
496+ CO2_inventory_updated [k ] <- co2_inv_updated [k ]
506497 }
507498 parameters $ CO2_EMISSION_INVENTORY [[j ]] <- CO2_inventory_updated
508499 }
509-
500+
510501
511502 }
512503
513504
514505
515-
516-
517-
518-
519-
520-
521-
522-
523-
524506
525507 # PA DOSE RESPONSE
526508 # if PA_DOSE_RESPONSE_QUANTILE == T, find all diseases that are related to
0 commit comments