## 2 populations, R=ab
eg2.1 <- data.frame(
pop = c("black", "white"),
avg_earnings = c(10930, 16591),
earner_prop = c(.717892, .825974)
)
dgnpop(eg2.1, pop = "pop", factors = c("avg_earnings", "earner_prop")) |>
dg_table()
## 2 populations, R=abc
eg2.2 <- data.frame(
pop = c("austria", "chile"),
birthsw1549 = c(51.78746, 84.90502),
propw1549 = c(.45919, .75756),
propw = c(.52638, .51065)
)
dgnpop(eg2.2, pop = "pop", factors = c("birthsw1549", "propw1549", "propw")) |>
dg_table()
## 2 populations, R=abcd
eg2.3 <- data.frame(
pop = c(1971, 1979),
birth_preg = c(25.3, 32.7),
preg_actw = c(.214, .290),
actw_prop = c(.279, .473),
w_prop = c(.949, .986)
)
dgnpop(eg2.3,
pop = "pop",
factors = c("birth_preg", "preg_actw", "actw_prop", "w_prop")
) |>
dg_table()
## 2 populations, R=abcde
eg2.4 <- data.frame(
pop = c(1970, 1980),
prop_m = c(.58, .72),
noncontr = c(.76, .97),
abort = c(.84, .97),
lact = c(.66, .56),
fecund = c(16.573, 16.158)
)
dgnpop(eg2.4,
pop = "pop",
factors = c("prop_m", "noncontr", "abort", "lact", "fecund")
) |>
dg_table()
## 2 populations, vector factors, R=sum(abc)
eg4.3 <- data.frame(
agegroup = rep(1:7, 2),
pop = rep(c(1970, 1960), e = 7),
bm = c(
488, 452, 338, 156, 63, 22, 3,
393, 407, 369, 274, 184, 90, 16
),
mw = c(
.082, .527, .866, .941, .942, .923, .876,
.122, .622, .903, .930, .916, .873, .800
),
wp = c(
.058, .038, .032, .030, .026, .023, .019,
.043, .041, .036, .032, .026, .020, .018
)
)
dgnpop(eg4.3,
pop = "pop", factors = c("bm", "mw", "wp"),
ratefunction = "sum(bm*mw*wp)"
) |>
dg_table()
## 2 populations, R=f(ab)
eg3.1 <- data.frame(
pop = c(1940, 1960),
crude_birth = c(19.4, 23.7),
crude_death = c(10.8, 9.5)
)
dgnpop(eg3.1,
pop = "pop",
factors = c("crude_birth", "crude_death"),
ratefunction = "crude_birth-crude_death"
) |>
dg_table()
## 2 populations, vector factors, R=f(abcd)
eg4.4 <- data.frame(
pop = rep(c(1963, 1983), e = 6),
agegroup = c("15-19", "20-24", "25-29", "30-34", "35-39", "40-44"),
A = c(
.200, .163, .146, .154, .168, .169,
.169, .195, .190, .174, .150, .122
),
B = c(
.866, .325, .119, .099, .099, .121,
.931, .563, .311, .216, .199, .191
),
C = c(
.007, .021, .023, .015, .008, .002,
.018, .026, .023, .016, .008, .002
),
D = c(
.454, .326, .195, .107, .051, .015,
.380, .201, .149, .079, .025, .006
)
)
dgnpop(eg4.4,
pop = "pop", factors = c("A", "B", "C", "D"),
id_vars = "agegroup",
ratefunction = "sum(A*B*C) / (sum(A*B*C) + sum(A*(1-B)*D))"
) |>
dg_table()
### alternatively:
myratef <- function(a, b, c, d) {
return(sum(a * b * c) / (sum(a * b * c) + sum(a * (1 - b) * d)))
}
dgnpop(eg4.4,
pop = "pop", factors = c("A", "B", "C", "D"),
id_vars = "agegroup",
ratefunction = "myratef(A,B,C,D)"
) |>
dg_table()
## using crossclassified for relative size:
eg5.1 <- data.frame(
age_group = rep(c(
"15-19", "20-24", "25-29", "30-34", "35-39",
"40-44", "45-49", "50-54", "55-59", "60-64",
"65-69", "70-74", "75+"
), 2),
pop = rep(c(1970, 1985), e = 13),
size = c(
12.9, 10.9, 9.5, 8.0, 7.8, 8.4, 8.6, 7.8, 7.0, 5.9, 4.7, 3.6, 4.9,
10.1, 11.2, 11.6, 10.9, 9.4, 7.7, 6.3, 6.0, 6.3, 5.9, 5.1, 4.0, 5.5
),
rate = c(
1.9, 25.8, 45.7, 49.6, 51.2, 51.6, 51.8, 54.9, 58.7, 60.4, 62.8, 66.6, 66.8,
2.2, 24.3, 45.8, 52.5, 56.1, 55.6, 56.0, 57.4, 57.2, 61.2, 63.9, 68.6, 72.2
)
)
dgnpop(eg5.1,
pop = "pop", factors = c("rate"),
id_vars = "age_group",
crossclassified = "size"
) |>
dg_table()
## 2 cross-classified variables, 2 populations, R=sum(w*r)
eg5.3 <- data.frame(
race = rep(rep(1:2, e = 11), 2),
age = rep(rep(1:11, 2), 2),
pop = rep(c(1985, 1970), e = 22),
size = c(
3041, 11577, 27450, 32711, 35480, 27411, 19555, 19795, 15254, 8022, 2472,
707, 2692, 6473, 6841, 6547, 4352, 3034, 2540, 1749, 804, 236,
2968, 11484, 34614, 30992, 21983, 20314, 20928, 16897, 11339, 5720, 1315,
535, 2162, 6120, 4781, 3096, 2718, 2363, 1767, 1149, 448, 117
),
rate = c(
9.163, 0.462, 0.248, 0.929, 1.084, 1.810, 4.715, 12.187, 27.728, 64.068, 157.570,
17.208, 0.738, 0.328, 1.103, 2.045, 3.724, 8.052, 17.812, 34.128, 68.276, 125.161,
18.469, 0.751, 0.391, 1.146, 1.287, 2.672, 6.636, 15.691, 34.723, 79.763, 176.837,
36.993, 1.352, 0.541, 2.040, 3.523, 6.746, 12.967, 24.471, 45.091, 74.902, 123.205
)
)
dgnpop(eg5.3,
pop = "pop", factors = c("rate"),
id_vars = c("race", "age"),
crossclassified = "size"
) |>
dg_table()
## 5 populations, R = f(abcd)
eg6.5 <- data.frame(
pop = rep(c(1963, 1968, 1973, 1978, 1983), e = 6),
agegroup = c("15-19", "20-24", "25-29", "30-34", "35-39", "40-44"),
A = c(
.200, .163, .146, .154, .168, .169,
.215, .191, .156, .137, .144, .157,
.218, .203, .175, .144, .127, .133,
.205, .200, .181, .162, .134, .118,
.169, .195, .190, .174, .150, .122
),
B = c(
.866, .325, .119, .099, .099, .121,
.891, .373, .124, .100, .107, .127,
.870, .396, .158, .125, .113, .129,
.900, .484, .243, .176, .155, .168,
.931, .563, .311, .216, .199, .191
),
C = c(
.007, .021, .023, .015, .008, .002,
.010, .023, .023, .015, .008, .002,
.011, .016, .017, .011, .006, .002,
.014, .019, .015, .010, .005, .001,
.018, .026, .023, .016, .008, .002
),
D = c(
.454, .326, .195, .107, .051, .015,
.433, .249, .159, .079, .037, .011,
.314, .181, .133, .063, .023, .006,
.313, .191, .143, .069, .021, .004,
.380, .201, .149, .079, .025, .006
)
)
dgnpop(eg6.5,
pop = "pop", factors = c("A", "B", "C", "D"),
id_vars = "agegroup",
ratefunction = "1000*sum(A*B*C) / (sum(A*B*C) + sum(A*(1-B)*D))"
) |>
dg_table()
dgnpop(eg6.5,
pop = "pop", factors = c("A", "B", "C", "D"),
id_vars = "agegroup",
ratefunction = "1000*sum(A*B*C) / (sum(A*B*C) + sum(A*(1-B)*D))"
) |>
dg_plot()
Run the code above in your browser using DataLab