df <- readRDS(test_path("testdata", "md_lorenz3.rds"))
test_that("md_compute_lorenz() returns expected results", {
out <- md_compute_lorenz(
welfare = df$welfare,
weight = df$weight
)
expect_equal(nrow(out), 100)
expect_equal(out$lorenz_weight[100], 1)
expect_equal(out$lorenz_welfare[100], 1)
expect_equal(out$welfare[100], 169400)
})
test_that("md_compute_lorenz() returns same as old version",{
new <- md_compute_lorenz(
welfare = df$welfare,
weight = df$weight
)
# old function (not optimized)
benchmark <- data.frame(
welfare = c(0.000000, 0.000000, 8.171429, 17.600000, 23.480930, 28.193540, 33.000000, 36.802760, 40.857140, 44.000000,
48.073600, 50.603800, 55.000000, 55.916670, 60.000000, 63.250000, 66.000000, 68.750000, 71.880390, 73.333340,
76.388890, 79.750000, 82.500000, 85.250000, 88.000000, 92.006900, 96.250000, 100.000000, 101.207600, 106.333300,
110.000000, 110.000000, 112.666700, 116.666700, 121.000000, 125.000000, 128.333300, 132.000000, 136.666700,
139.333300, 144.320000, 146.666700, 150.700000, 155.711100, 161.333300, 165.000000, 167.750000, 174.285700,
178.750000, 183.333300, 189.750000, 193.333300, 200.000000, 202.415200, 210.000000, 220.000000, 220.000000,
220.000000, 227.717100, 233.750000, 242.000000, 250.000000, 253.550000, 261.800000, 270.000000, 275.000000,
286.000000, 294.250000, 302.500000, 315.000000, 326.700000, 330.000000, 345.025900, 357.500000, 370.700000,
385.000000, 400.000000, 412.500000, 436.333300, 445.500000, 467.500000, 495.000000, 511.500000, 540.833300,
560.000000, 600.000000, 632.500000, 665.500000, 715.000000, 770.000000, 825.000000, 900.000000, 992.600000,
1100.000000, 1237.500000, 1430.000000, 1705.000000, 2129.233000, 2942.500000, 169400.000000),
lorenz_welfare = c(0.000000000000, 0.000000000000, 0.000049802823, 0.000429797393, 0.000994450074, 0.001722589796,
0.002679566906, 0.003566532137, 0.004659957742, 0.005920027366, 0.007156361841, 0.008714597085,
0.011384357137, 0.011556285599, 0.013519887575, 0.014976322250, 0.016960523986, 0.018906942797,
0.020573820729, 0.022897502110, 0.024679246064, 0.026887294125, 0.029642282269, 0.031492109108,
0.034064253229, 0.036602634887, 0.039515370364, 0.043840290246, 0.044685197734, 0.047650495259,
0.055561437593, 0.055561437593, 0.056779312106, 0.060264726856, 0.064002100885, 0.067201183268,
0.070864351490, 0.074403367906, 0.077717817099, 0.081648869432, 0.085531238935, 0.089771063888,
0.093827781108, 0.098095008528, 0.102840582658, 0.109711980441, 0.111890607063, 0.116550616023,
0.121899534229, 0.127546513022, 0.131823007120, 0.137145029869, 0.146446471231, 0.148612137489,
0.154264154516, 0.173724736756, 0.173724736756, 0.173724736756, 0.178945393105, 0.185482410943,
0.192549805711, 0.200842050221, 0.205755718697, 0.212976822685, 0.220578572443, 0.229627861229,
0.236628956912, 0.244056335090, 0.253729602790, 0.261147055686, 0.270027361674, 0.280312662944,
0.288794577942, 0.299262359577, 0.308754691953, 0.322429752180, 0.332193457609, 0.341857294239,
0.353615680104, 0.365963711239, 0.379709126526, 0.395819460285, 0.406143809146, 0.420855398408,
0.436504930276, 0.455010992250, 0.470985211443, 0.487915850496, 0.508009516120, 0.531949414192,
0.551856300772, 0.574773101759, 0.600955771156, 0.635721955276, 0.663339450455, 0.701976854141,
0.744365950937, 0.796631209515, 0.865883174490, 1.000000000000),
lorenz_weight = c(0.026749214, 0.026749214, 0.030000213, 0.040255876, 0.050014351, 0.060002370, 0.071057396,
0.080045954, 0.090030271, 0.100518725, 0.110081086, 0.121254960, 0.138909705, 0.140015811,
0.152059537, 0.160472996, 0.171422003, 0.181739483, 0.190201100, 0.201548160, 0.210026279,
0.220160416, 0.232245778, 0.240108683, 0.250631547, 0.260640213, 0.271638767, 0.287205221,
0.290195356, 0.300368474, 0.326139992, 0.326139992, 0.330037843, 0.340864318, 0.352049550,
0.361280831, 0.371587685, 0.381227815, 0.390053008, 0.400235539, 0.410003132, 0.420354768,
0.430060267, 0.440010794, 0.450681094, 0.465601399, 0.470267612, 0.480002845, 0.490836356,
0.501910390, 0.510096459, 0.520015176, 0.536757002, 0.540594439, 0.550356343, 0.582179898,
0.582179898, 0.582179898, 0.590482527, 0.600572443, 0.611137216, 0.623091822, 0.630038449,
0.640031340, 0.650241412, 0.662018031, 0.670896396, 0.680000557, 0.691523687, 0.700102876,
0.710002058, 0.721139328, 0.730103709, 0.740719235, 0.750009790, 0.762872874, 0.771685154,
0.780148079, 0.790048207, 0.800064092, 0.810790988, 0.822688566, 0.830019192, 0.840028337,
0.850183380, 0.861529907, 0.870774663, 0.880044476, 0.890418597, 0.901861582, 0.910686516,
0.920140363, 0.930007015, 0.941753474, 0.950154719, 0.960495334, 0.970158936, 0.980004646,
0.990012649, 1.000000000)
)
expect_equal(new, benchmark)
})
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.