Take the cumulative sum of Value
and then run a monotonic spline through it. The first differences split back single-age estimates of Value
. Optionally keep the open age group untouched.
graduate_mono(Value, Age, AgeInt, OAG = TRUE)
Value | numeric vector, presumably counts in grouped ages |
---|---|
Age | integer vector, lower bounds of age groups |
AgeInt | integer vector, age interval widths |
OAG | logical, default = |
Numeric. vector of single smoothed age counts.
The "hyman"
method of stats::splinefun()
is used to fit the spline because 1) it passes exactly through the points, 2) it is monotonic and therefore guarantees positive counts, and 3) it seems to be a bit less wiggly (lower average first differences of split counts). Single-age data is returned as-is. If you want to use this function as a smoother you first need to group to non-single ages. Age
be any age range, it does not need to start at 0.
Fritsch FN, Carlson RE (1980). “Monotone piecewise cubic interpolation.” SIAM Journal on Numerical Analysis, 17(2), 238--246.
Value <- structure(c(88623, 90842, 93439, 96325, 99281, 102051, 104351, 106555, 109170, 112188, 113582, 112614, 108904, 102622, 95867, 80874, 60196, 37523, 17927, 5642, 1110), .Names = c("0", "5", "10", "15", "20", "25", "30", "35", "40", "45", "50", "55", "60", "65", "70", "75", "80", "85", "90", "95", "100")) # if the last age group is closed, then it's best to use AgeInt, otherwise, # one is assumed from the age siphoned from the names attribute of Value. graduate_mono(Value, OAG = FALSE)#> 0 1 2 3 4 5 6 #> 17565.4135 17641.9827 17721.5760 17804.1933 17889.8345 17978.5187 18070.3217 #> 7 8 9 10 11 12 13 #> 18165.2623 18263.3406 18364.5567 18468.8156 18575.7386 18685.2308 18797.2922 #> 14 15 16 17 18 19 20 #> 18911.9229 19028.7708 19146.4281 19264.5427 19383.1146 19502.1439 19621.3811 #> 21 22 23 24 25 26 27 #> 19739.8291 19857.2386 19973.6094 20088.9417 20202.5367 20311.5993 20415.4310 #> 28 29 30 31 32 33 34 #> 20514.0317 20607.4013 20696.3123 20783.8535 20870.7974 20957.1439 21042.8930 #> 35 36 37 38 39 40 41 #> 21128.6463 21216.8106 21307.9874 21402.1768 21499.3788 21600.4704 21708.9601 #> 42 43 44 45 46 47 48 #> 21825.7249 21950.7648 22084.0798 22221.4960 22346.3171 22454.3691 22545.6520 #> 49 50 51 52 53 54 55 #> 22620.1659 22677.5137 22716.1076 22735.5508 22735.8432 22716.9847 22678.8334 #> 56 57 58 59 60 61 62 #> 22620.8204 22542.8037 22444.7833 22326.7592 22186.6569 22016.1788 21813.2503 #> 63 64 65 66 67 68 69 #> 21577.8715 21310.0425 21019.5630 20745.6326 20498.0511 20276.8185 20081.9348 #> 70 71 72 73 74 75 76 #> 19893.0670 19628.8829 19269.0494 18813.5665 18262.4342 17625.0648 16939.1078 #> 77 78 79 80 81 82 83 #> 16213.9754 15449.6676 14646.1844 13806.6336 12943.4459 12059.7291 11155.4832 #> 84 85 86 87 88 89 90 #> 10230.7082 9293.0808 8373.3085 7479.0681 6610.3596 5767.1829 4956.2991 #> 91 92 93 94 95 96 97 #> 4204.7519 3519.3024 2899.9505 2346.6962 1858.6909 1432.5399 1067.3944 #> 98 99 100 101 102 103 104 #> 763.2546 520.1203 338.1613 218.0567 159.9760 163.9193 229.8867# or leave open age group in tact graduate_mono(Value, OAG = TRUE)#> 0 1 2 3 4 5 6 #> 17565.4135 17641.9827 17721.5760 17804.1933 17889.8345 17978.5187 18070.3217 #> 7 8 9 10 11 12 13 #> 18165.2623 18263.3406 18364.5567 18468.8156 18575.7386 18685.2308 18797.2922 #> 14 15 16 17 18 19 20 #> 18911.9229 19028.7708 19146.4281 19264.5427 19383.1146 19502.1439 19621.3811 #> 21 22 23 24 25 26 27 #> 19739.8291 19857.2386 19973.6094 20088.9417 20202.5367 20311.5993 20415.4310 #> 28 29 30 31 32 33 34 #> 20514.0317 20607.4013 20696.3123 20783.8535 20870.7974 20957.1439 21042.8930 #> 35 36 37 38 39 40 41 #> 21128.6463 21216.8106 21307.9874 21402.1768 21499.3788 21600.4704 21708.9601 #> 42 43 44 45 46 47 48 #> 21825.7249 21950.7648 22084.0798 22221.4960 22346.3171 22454.3691 22545.6520 #> 49 50 51 52 53 54 55 #> 22620.1658 22677.5136 22716.1076 22735.5508 22735.8432 22716.9848 22678.8335 #> 56 57 58 59 60 61 62 #> 22620.8205 22542.8038 22444.7833 22326.7589 22186.6564 22016.1783 21813.2500 #> 63 64 65 66 67 68 69 #> 21577.8718 21310.0435 21019.5648 20745.6344 20498.0520 20276.8176 20081.9312 #> 70 71 72 73 74 75 76 #> 19893.0605 19628.8761 19269.0459 18813.5698 18262.4477 17625.0893 16939.1330 #> 77 78 79 80 81 82 83 #> 16213.9883 15449.6553 14646.1340 13806.5422 12943.3518 12059.6807 11155.5289 #> 84 85 86 87 88 89 90 #> 10230.8964 9293.4219 8373.6598 7479.2489 6610.1891 5766.4804 4955.0263 #> 91 92 93 94 95 96 97 #> 4203.4409 3518.6278 2900.5869 2349.3182 1863.4410 1437.4325 1069.9120 #> 98 99 100 #> 760.8795 510.3350 1110.0000