This repository was archived by the owner on Sep 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathutils.R
More file actions
69 lines (62 loc) · 2.12 KB
/
utils.R
File metadata and controls
69 lines (62 loc) · 2.12 KB
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
# utils.R - utility functions
# calculate a single value diversity coefficient in [0,1] from a vector of populations for each ethnicity
diversityCoeff = function(pop) {
# uses a value based on the normalised variance of the population
# if pop all equal, returns 1 (max diversity)
# if pop all one ethnicity, return 0
# This is essentially 1- SAMPLE variance * n
# Or 1 - POPULATION variance * n * n / (n-1)
p = pop/sum(pop)
n = length(p)
return((1 - p %*% p)*n/(n-1))
}
#' diversity
#'
#' Calculates a diversity coefficient for a population, grouped by by MSOA
#' The coefficient is related to the normalised variance of the population
#' and will range from 0 (if only one ethnicity is present) to 1 (if equal
#' populations of each ethnicity).
#' @param synpop the synthesised or projected population
#' @return a two colum data table containing the MSOA and the diversity coefficient
#' @export
#' @examples
#' p = microsynthesise()
#' d = diversity(p)
diversity = function(synpop) {
# get the MSOAs and ethnicities
msoas = unique(synpop$MSOA)
ethnicities = unique(synpop$Ethnicity)
# preallocate the table
div = data.table(MSOA=msoas, Value=rep(-1, length(msoas)))
for (msoa in msoas) {
pops = rep(-1, length(ethnicities))
for (j in 1:length(ethnicities)) {
pops[j] = nrow(synpop[MSOA==msoa & Ethnicity==ethnicities[j]])
}
div[MSOA==msoa]$Value = diversityCoeff(pops)
}
return(div)
}
#' growth
#'
#' Calculates growth rate by MSOA between a two synthetic populations
#' @param pop0 a data table containing the initial population
#' @param pop1 a data table containing the final population
#' @return a two column data table containing the MSOA and the diversity coefficient
#' @export
#' @examples
#' \dontrun{
#' basepop = microsynthesise()
#' projpop = microsimulate(basepop,10)
#' g = growth(basePop, projpop)
#' }
growth = function(pop0, pop1) {
# get the MSOAs
msoas = unique(pop0$MSOA)
# preallocate the table
g = data.table(MSOA=msoas, Value=rep(-1, length(msoas)))
for (msoa in msoas) {
g[MSOA==msoa]$Value = nrow(pop1[MSOA==msoa]) / nrow(pop0[MSOA==msoa]) - 1.0
}
return(g)
}