-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetric_functions.R
74 lines (49 loc) · 1.98 KB
/
metric_functions.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
# File containing functions for various segregation metrics
#
# Gini Coefficient
giniCoeff <- function(city) {
t <- city$pop # total population of each census tract
p <- city$pct.not.white # ratio of non white population to total population
# in each census tract
x <- city$pop.not.white # non white population in each census tract
# help from: http://stackoverflow.com/questions/11388359/
combinations <- expand.grid(1:nrow(city), 1:nrow(city))
i <- combinations[, 1]
j <- combinations[, 2]
# inspired by: https://stat.ethz.ch/pipermail/r-help/2008-August/172358.html
top.sum <- sum(t[i] * t[j] * abs(p[i] - p[j]))
total.pct.not.white <- sum(x)/sum(t)
denominator <- 2 * sum(t)**2 * total.pct.not.white * (1 - total.pct.not.white)
coeff <- top.sum / denominator
return(coeff)
}
# Correlation Ratio
correlationRatio <- function(city) {
x <- city$pop.not.white # non white population in each census tract
t <- city$pop # total population of each census tract
total.pct.not.white <- sum(x) / sum(t)
isolation <- sum((x/sum(x)) * (x/t))
corr <- (isolation - total.pct.not.white) / (1 - total.pct.not.white)
return(corr)
}
# Delta index
deltaIndex <- function(city) {
x <- city$pop.not.white # non white population in each census tract
a <- city$area # area of each census tract
x.tot <- sum(x)
a.tot <- sum(a)
result <- .5 * sum(abs((x/x.tot) - (a/a.tot)))
return(result)
}
# New metric
newMetric <- function(city) {
p <- city$pct.not.white # ratio of non white population to total population
# in each census tract
a <- city$area # area of each census tract
# help from: http://stackoverflow.com/questions/11388359/
combinations <- expand.grid(1:nrow(city), 1:nrow(city))
i <- combinations[, 1]
j <- combinations[, 2]
result <- sum(abs((p[i]/a[i])-(p[j]/a[j])))
return(result)
}