library(dplyr)
library(stringr)
library(ggplot2)
devtools::load_all()

Workflow

  1. Compute the flow matrix using the parameters of the specified
centroids <- here::here(params$centroids) %>%
    readr::read_csv()

##centroids <- mutate(centroids, district_clean = clean_names(district))

Flow Matrix

distances <- geosphere::distm(cbind(centroids$Centroid_Lon,
                                    centroids$Centroid_Lat))
distances <- as.matrix(distances) 
distvec <- distances[lower.tri(distances)]

Also need the populations for estimating flows.

idx <- mRIIDS:::lower_tri_idx(nrow(distances))
n_from <- centroids$Pop[idx[, 1]]
n_to <- centroids$Pop[idx[, 2]]

Now determine flow matrix.

flow_from_to <- flow_matrix(
    distvec, 
    n_from,
    n_to,
    place_names = centroids$ADM0,
    model = params$model,
    params = params$modelpars
)


## Normalising Relative Flow 
for (i in seq_len(nrow(flow_from_to))) {
    flow_from_to[i, ] <- flow_from_to[i, ] / (sum(flow_from_to[i, ],
                                                  na.rm = TRUE))
}    

Write it out in a clean format. The rownames will be the cleaned district names.

## relative_risk <- flow_from_to / rowSums(flow_from_to, na.rm = TRUE)
relative_risk <- flow_from_to                                        
rel_risk_df <- tibble::rownames_to_column(data.frame(relative_risk),
                                            var = "flow_from")
from_source <- filter(rel_risk_df,
                      flow_from == params$from) %>%
    tidyr::gather(flow_to, relative_flow, -flow_from) 

## from_source <- left_join(from_source,
##                          centroids,
##                          by = c("flow_to" = "district_clean")) %>%
##     select(flow_from, flow_to, district, ADM0, relative_flow)
idx <- which(from_source$flow_to == "louvakou.loubomo.")
from_source$flow_to[idx] <- "louvakou(loubomo)"

outfile_prefix <- paste0("flow_from_", params$from, "_")
outfile_suffix <- paste(sapply(params$modelpars, paste, collapse=""),
                        collapse = "_")

here::here("output",
           paste0(outfile_prefix, outfile_suffix, ".csv")) %>%
    readr::write_csv(x = from_source,
                     path = .)


annecori/mRIIDSprocessData documentation built on May 29, 2019, 1:16 p.m.