Chapter 1 from Bjornstad (2018): original code in R

Interact

Libraries

using RCall
using DataFrames
using StatsBase: mean, std
using Plots
@rlibrary epimdr

Figure 1.2

ccs = rcopy(R"epimdr::ccs")
fade3extsizefadese3sen3nnames
Float64Float64Float64Float64Float64Float64Float64Float64String
112.120.64598511740.05.701611.761870.8527150.0124.0Abingdon
214.78570.674276190.05.9041.911980.85469742.0125.0Abram
312.00.5036539610.04.338582.206560.73626435.0127.0Accrington
48.464290.34124165790.03.215521.184650.39936328.0116.0Acton
522.77780.7974454110.011.48683.715712.1411236.076.0Adlington
612.88640.61405118580.05.94691.984670.93040444.0113.0Adwick.le.Street
721.18180.8859492690.014.26472.631382.0462844.068.0Aldeburgh
821.19510.8503653722.011.35373.450032.0340641.082.0Alderley.Edge
911.48780.54653338180.04.708661.343970.60122341.0127.0Aldershot
106.468750.33485431440.02.738810.8131780.27006632.0134.0Aldridge
1137.29630.9361312150.027.70275.938155.0489927.037.0Alford
1212.480.64689823130.06.495411.786850.97353950.0109.0Alfreton
1329.19350.8631397310.015.49185.621473.3564731.061.0Alnwick
1410.93480.57116827520.04.960321.233750.60572946.0126.0Alreborough
1512.91380.7664235875.07.359651.761541.0400458.0114.0Alsager
1620.31580.7737238660.09.735632.860961.5971238.087.0Alton
179.232560.44525540070.04.594341.097580.58103943.0106.0Altrincham
1831.00.8841244799.017.65.218453.4601730.055.0Amble
1932.44440.8585773000.013.42865.499632.7732527.070.0Amblecote
2025.13160.9032853050.016.76273.450972.6613238.059.0Ampthill
2119.97060.70985415600.08.265962.931631.393734.094.0Andover
2233.03450.8950731698.021.30436.743814.8018629.046.0Appleby
2313.5610.59671522000.06.339811.680610.88449141.0103.0Arnold
2429.76670.8667882640.013.55715.372922.8388530.070.0Arundel
2519.69230.7709855470.09.483153.323571.7370639.089.0Ashbourne
2648.80.924272695.024.09529.8415.908520.042.0Ashburton
2716.42550.7709856552.08.525252.277821.3168747.099.0Ashby.de.la.Zouch
2820.16670.8284673340.010.79762.590951.648842.084.0Ashby.Woulds
2911.37210.55474525630.05.152541.546980.71120543.0118.0Ashford
3015.50.54653328370.05.805832.282910.95442132.0103.0Ashington
plot(ccs[:size], ccs[:ext]*100,
    seriestype=:scatter,
    xscale = :log10,
    xlabel = "Community size",
    ylabel = "Percent of time extinct")

svg

Fig 1.3a

magono = rcopy(R"epimdr::magono")
plot(magono[:time], magono[:number], ylabel = "Cases", xlabel = "Year")
smooth = rcopy(rcall(:lowess,x=magono[:time],y=magono[:number],f=0.4))
plot!(smooth[:x],smooth[:y])

svg

Fig 1.3b

cholera = rcopy(R"epimdr::cholera")
YearMonthDaccaPopulation
Int64Int64Int64Int64
11891126412420656
2189129392422563
3189139052424470
41891412192426378
5189153682428285
618916782430192
718917292432099
818918122434007
918919302435914
10189110442437821
111891112702439728
1218911211492441635
13189216332443543
14189225012445450
15189238552447357
161892412712449264
17189256662451171
18189261012453079
1918927622454986
2018928232456893
2118929202458800
22189210282460708
231892114612462615
241892128922464522
25189317512466429
26189321702468336
27189332532470244
28189349062472151
29189357002474058
3018936982475965
err(x)=1.96std(x)/sqrt(length(x))
ses = aggregate(cholera[[:Dacca,:Month]],:Month,[mean,err])
ses[:Month][1:6] = 7:12
ses[:Month][7:12] = 1:6
sort!(ses,(:Month))
MonthDacca_meanDacca_err
Int64Float64Float64
1135.188.18342
2228.486.44006
3395.5837.4001
44436.96204.182
551062.58316.323
661419.38296.542
77798.74166.801
88341.6467.465
99808.04180.589
10101507.04312.602
1111471.0288.1247
121280.8617.6863
plot(ses[:Month],ses[:Dacca_mean],yerr=ses[:Dacca_err],xlabel="Month",xticks=1:12,ylabel="Deaths")

svg