Extract Model Data for Deterministic Compartmental Models
Source:R/as.data.frame.R
as.data.frame.dcm.Rd
This function extracts a model run from an object of class
dcm
into a data frame using the generic
as.data.frame
function.
Usage
# S3 method for class 'dcm'
as.data.frame(x, row.names = NULL, optional = FALSE, run, ...)
Arguments
- x
An
EpiModel
object of classdcm
.- row.names
- optional
- run
Run number for model; used for multiple-run sensitivity models. If not specified, will output data from all runs in a stacked data frame.
- ...
Details
Model output from dcm
simulations are available as a data
frame with this helper function. The output data frame will include
columns for time, the size of each compartment, the overall population
size (the sum of compartment sizes), and the size of each flow.
For models with multiple runs (i.e., varying parameters - see example below),
the default with the run
parameter not specified will output all runs
in a single stacked data frame.
Examples
## Example 1: One-group SIS model with varying act.rate
param <- param.dcm(inf.prob = 0.2, act.rate = seq(0.05, 0.5, 0.05),
rec.rate = 1/50)
init <- init.dcm(s.num = 500, i.num = 1)
control <- control.dcm(type = "SIS", nsteps = 10)
mod1 <- dcm(param, init, control)
as.data.frame(mod1)
#> run time s.num i.num si.flow is.flow num
#> 1 1 1 500.0000 1.0000000 0.009930306 0.01990013 501
#> 2 1 2 500.0100 0.9900302 0.009831499 0.01970174 501
#> 3 1 3 500.0198 0.9801599 0.009733674 0.01950532 501
#> 4 1 4 500.0296 0.9703883 0.009636823 0.01931086 501
#> 5 1 5 500.0393 0.9607142 0.009540936 0.01911835 501
#> 6 1 6 500.0489 0.9511368 0.009446003 0.01892776 501
#> 7 1 7 500.0583 0.9416551 0.009352014 0.01873907 501
#> 8 1 8 500.0677 0.9322680 0.009258961 0.01855227 501
#> 9 1 9 500.0770 0.9229747 0.009166833 0.01836734 501
#> 10 1 10 500.0862 0.9137742 0.009166833 0.01836734 501
#> 11 2 1 500.0000 1.0000000 0.019959682 0.01999960 501
#> 12 2 2 500.0000 0.9999601 0.019958887 0.01999880 501
#> 13 2 3 500.0001 0.9999202 0.019958092 0.01999800 501
#> 14 2 4 500.0001 0.9998803 0.019957297 0.01999721 501
#> 15 2 5 500.0002 0.9998403 0.019956502 0.01999641 501
#> 16 2 6 500.0002 0.9998004 0.019955707 0.01999561 501
#> 17 2 7 500.0002 0.9997605 0.019954912 0.01999481 501
#> 18 2 8 500.0003 0.9997206 0.019954118 0.01999401 501
#> 19 2 9 500.0003 0.9996807 0.019953323 0.01999322 501
#> 20 2 10 500.0004 0.9996408 0.019953323 0.01999322 501
#> 21 3 1 500.0000 1.0000000 0.030089115 0.02009973 501
#> 22 3 2 499.9900 1.0099894 0.030389067 0.02030051 501
#> 23 3 3 499.9799 1.0200779 0.030691985 0.02050328 501
#> 24 3 4 499.9697 1.0302667 0.030997897 0.02070806 501
#> 25 3 5 499.9594 1.0405565 0.031306833 0.02091488 501
#> 26 3 6 499.9491 1.0509484 0.031618821 0.02112375 501
#> 27 3 7 499.9386 1.0614435 0.031933892 0.02133469 501
#> 28 3 8 499.9280 1.0720427 0.032252076 0.02154772 501
#> 29 3 9 499.9173 1.0827471 0.032573401 0.02176287 501
#> 30 3 10 499.9064 1.0935576 0.032573401 0.02176287 501
#> 31 4 1 500.0000 1.0000000 0.040319599 0.02020053 501
#> 32 4 2 499.9799 1.0201191 0.041129087 0.02060692 501
#> 33 4 3 499.9594 1.0406412 0.041954724 0.02102147 501
#> 34 4 4 499.9384 1.0615745 0.042796828 0.02144431 501
#> 35 4 5 499.9171 1.0829270 0.043655722 0.02187562 501
#> 36 4 6 499.8953 1.1047071 0.044531739 0.02231557 501
#> 37 4 7 499.8731 1.1269233 0.045425213 0.02276433 501
#> 38 4 8 499.8504 1.1495842 0.046336487 0.02322206 501
#> 39 4 9 499.8273 1.1726986 0.047265913 0.02368896 501
#> 40 4 10 499.8037 1.1962755 0.047265913 0.02368896 501
#> 41 5 1 500.0000 1.0000000 0.050652141 0.02030199 501
#> 42 5 2 499.9696 1.0303501 0.052186144 0.02091813 501
#> 43 5 3 499.9384 1.0616182 0.053766337 0.02155290 501
#> 44 5 4 499.9062 1.0938316 0.055394094 0.02220686 501
#> 45 5 5 499.8730 1.1270188 0.057070828 0.02288058 501
#> 46 5 6 499.8388 1.1612091 0.058797994 0.02357467 501
#> 47 5 7 499.8036 1.1964324 0.060577091 0.02428972 501
#> 48 5 8 499.7673 1.2327198 0.062409658 0.02502637 501
#> 49 5 9 499.7299 1.2701031 0.064297279 0.02578527 501
#> 50 5 10 499.6914 1.3086151 0.064297279 0.02578527 501
#> 51 6 1 500.0000 1.0000000 0.061087754 0.02040414 501
#> 52 6 2 499.9593 1.0406836 0.063567589 0.02123420 501
#> 53 6 3 499.9170 1.0830170 0.066147533 0.02209792 501
#> 54 6 4 499.8729 1.1270666 0.068831582 0.02299665 501
#> 55 6 5 499.8271 1.1729016 0.071623886 0.02393179 501
#> 56 6 6 499.7794 1.2205936 0.074528756 0.02490483 501
#> 57 6 7 499.7298 1.2702176 0.077550673 0.02591727 501
#> 58 6 8 499.6781 1.3218510 0.080694288 0.02697070 501
#> 59 6 9 499.6244 1.3755746 0.083964434 0.02806677 501
#> 60 6 10 499.5685 1.4314722 0.083964434 0.02806677 501
#> 61 7 1 500.0000 1.0000000 0.071627466 0.02050697 501
#> 62 7 2 499.9489 1.0511205 0.075280927 0.02155522 501
#> 63 7 3 499.8952 1.1048462 0.079119721 0.02265688 501
#> 64 7 4 499.8387 1.1613091 0.083153143 0.02381465 501
#> 65 7 5 499.7794 1.2206475 0.087390941 0.02503139 501
#> 66 7 6 499.7170 1.2830071 0.091843342 0.02631005 501
#> 67 7 7 499.6515 1.3485404 0.096521070 0.02765378 501
#> 68 7 8 499.5826 1.4174077 0.101435370 0.02906587 501
#> 69 7 9 499.5102 1.4897772 0.106598031 0.03054974 501
#> 70 7 10 499.4342 1.5658255 0.106598031 0.03054974 501
#> 71 8 1 500.0000 1.0000000 0.082272308 0.02061048 501
#> 72 8 2 499.9383 1.0616618 0.087333822 0.02188125 501
#> 73 8 3 499.8729 1.1271144 0.092705033 0.02323013 501
#> 74 8 4 499.8034 1.1965893 0.098404673 0.02466189 501
#> 75 8 5 499.7297 1.2703321 0.104452584 0.02618158 501
#> 76 8 6 499.6514 1.3486031 0.110869772 0.02779457 501
#> 77 8 7 499.5683 1.4316783 0.117678477 0.02950653 501
#> 78 8 8 499.4801 1.5198502 0.124902240 0.03132351 501
#> 79 8 9 499.3866 1.6134290 0.132565973 0.03325187 501
#> 80 8 10 499.2873 1.7127431 0.132565973 0.03325187 501
#> 81 9 1 500.0000 1.0000000 0.093023327 0.02071469 501
#> 82 9 2 499.9277 1.0723086 0.099734099 0.02221239 501
#> 83 9 3 499.8502 1.1498303 0.106926344 0.02381805 501
#> 84 9 4 499.7671 1.2329386 0.114634208 0.02553939 501
#> 85 9 5 499.6780 1.3220335 0.122894202 0.02738470 501
#> 86 9 6 499.5825 1.4175430 0.131745353 0.02936283 501
#> 87 9 7 499.4801 1.5199255 0.141229367 0.03148327 501
#> 88 9 8 499.3703 1.6296716 0.151390806 0.03375617 501
#> 89 9 9 499.2527 1.7473062 0.162277263 0.03619239 501
#> 90 9 10 499.1266 1.8733911 0.162277263 0.03619239 501
#> 91 10 1 500.0000 1.0000000 0.103881575 0.02081960 501
#> 92 10 2 499.9169 1.0830620 0.112489747 0.02254872 501
#> 93 10 3 499.8270 1.1730030 0.121807299 0.02442101 501
#> 94 10 4 499.7296 1.2703893 0.131892009 0.02644825 501
#> 95 10 5 499.6242 1.3758331 0.142806244 0.02864317 501
#> 96 10 6 499.5100 1.4899961 0.154617300 0.03101954 501
#> 97 10 7 499.3864 1.6135939 0.167397776 0.03359223 501
#> 98 10 8 499.2526 1.7473994 0.181225955 0.03637733 501
#> 99 10 9 499.1078 1.8922481 0.196186222 0.03939219 501
#> 100 10 10 498.9510 2.0490421 0.196186222 0.03939219 501
as.data.frame(mod1, run = 1)
#> run time s.num i.num si.flow is.flow num
#> 1 1 1 500.0000 1.0000000 0.009930306 0.01990013 501
#> 2 1 2 500.0100 0.9900302 0.009831499 0.01970174 501
#> 3 1 3 500.0198 0.9801599 0.009733674 0.01950532 501
#> 4 1 4 500.0296 0.9703883 0.009636823 0.01931086 501
#> 5 1 5 500.0393 0.9607142 0.009540936 0.01911835 501
#> 6 1 6 500.0489 0.9511368 0.009446003 0.01892776 501
#> 7 1 7 500.0583 0.9416551 0.009352014 0.01873907 501
#> 8 1 8 500.0677 0.9322680 0.009258961 0.01855227 501
#> 9 1 9 500.0770 0.9229747 0.009166833 0.01836734 501
#> 10 1 10 500.0862 0.9137742 0.009166833 0.01836734 501
as.data.frame(mod1, run = 10)
#> run time s.num i.num si.flow is.flow num
#> 1 10 1 500.0000 1.000000 0.1038816 0.02081960 501
#> 2 10 2 499.9169 1.083062 0.1124897 0.02254872 501
#> 3 10 3 499.8270 1.173003 0.1218073 0.02442101 501
#> 4 10 4 499.7296 1.270389 0.1318920 0.02644825 501
#> 5 10 5 499.6242 1.375833 0.1428062 0.02864317 501
#> 6 10 6 499.5100 1.489996 0.1546173 0.03101954 501
#> 7 10 7 499.3864 1.613594 0.1673978 0.03359223 501
#> 8 10 8 499.2526 1.747399 0.1812260 0.03637733 501
#> 9 10 9 499.1078 1.892248 0.1961862 0.03939219 501
#> 10 10 10 498.9510 2.049042 0.1961862 0.03939219 501
## Example 2: Two-group SIR model with vital dynamics
param <- param.dcm(inf.prob = 0.2, inf.prob.g2 = 0.1,
act.rate = 3, balance = "g1",
rec.rate = 1/50, rec.rate.g2 = 1/50,
a.rate = 1/100, a.rate.g2 = NA,
ds.rate = 1/100, ds.rate.g2 = 1/100,
di.rate = 1/90, di.rate.g2 = 1/90,
dr.rate = 1/100, dr.rate.g2 = 1/100)
init <- init.dcm(s.num = 500, i.num = 1, r.num = 0,
s.num.g2 = 500, i.num.g2 = 1, r.num.g2 = 0)
control <- control.dcm(type = "SIR", nsteps = 10)
mod2 <- dcm(param, init, control)
as.data.frame(mod2)
#> time s.num i.num r.num s.num.g2 i.num.g2 r.num.g2 si.flow
#> 1 1 500.0000 1.000000 0.00000000 500.0000 1.000000 0.00000000 0.6956944
#> 2 2 499.3175 1.654896 0.02610779 499.6193 1.356308 0.02314214 0.9683946
#> 3 3 498.3704 2.558561 0.06726741 499.0138 1.927958 0.05517913 1.3911960
#> 4 4 497.0118 3.851292 0.12960355 498.0930 2.800201 0.10109329 2.0247675
#> 5 5 495.0361 5.728883 0.22248700 496.7194 4.103195 0.16793811 2.9577317
#> 6 6 492.1513 8.468474 0.35985371 494.6902 6.028843 0.26586939 4.3143671
#> 7 7 487.9449 12.461179 0.56209206 491.7134 8.853692 0.40956118 6.2614805
#> 8 8 481.8422 18.250486 0.85862307 487.3770 12.967442 0.62012165 9.0093775
#> 9 9 473.0651 26.570405 1.29123344 481.1156 18.903705 0.92758946 12.7966119
#> 10 10 460.6056 38.367368 1.91801153 472.1838 27.363408 1.37396802 12.7966119
#> ir.flow a.flow ds.flow di.flow dr.flow si.flow.g2 ir.flow.g2
#> 1 0.02622751 5.009993 4.996760 0.01457084 0.0001197282 0.3924783 0.02325207
#> 2 0.04161165 5.009975 4.988717 0.02311758 0.0004520193 0.6220793 0.03241927
#> 3 0.06329928 5.009946 4.977333 0.03516626 0.0009631428 0.9448588 0.04668126
#> 4 0.09461312 5.009903 4.960866 0.05256285 0.0017296721 1.4090334 0.06816864
#> 5 0.14023353 5.009838 4.936855 0.07790752 0.0028668137 2.0813106 0.10006879
#> 6 0.20678268 5.009743 4.901807 0.11487926 0.0045443299 3.0535515 0.14702269
#> 7 0.30353980 5.009603 4.850824 0.16863322 0.0070087866 4.4491919 0.21564154
#> 8 0.44322350 5.009398 4.777173 0.24623528 0.0106131318 6.4264337 0.31510923
#> 9 0.64263140 5.009099 4.671930 0.35701745 0.0158533222 9.1717556 0.45774804
#> 10 0.64263140 5.009099 4.671930 0.35701745 0.0158533222 9.1717556 0.45774804
#> a.flow.g2 ds.flow.g2 di.flow.g2 dr.flow.g2 num num.g2
#> 1 5.009993 4.998258 0.01291782 0.0001099331 501.0000 501.0000
#> 2 5.009975 4.993387 0.01801070 0.0003822785 500.9985 500.9987
#> 3 5.009946 4.985849 0.02593403 0.0007670955 500.9962 500.9969
#> 4 5.009903 4.974517 0.03787147 0.0013238256 500.9927 500.9943
#> 5 5.009838 4.957707 0.05559377 0.0021375038 500.9875 500.9905
#> 6 5.009743 4.932968 0.08167927 0.0033308964 500.9797 500.9849
#> 7 5.009603 4.896808 0.11980085 0.0050810708 500.9682 500.9767
#> 8 5.009398 4.844367 0.17506069 0.0076414259 500.9513 500.9646
#> 9 5.009099 4.769105 0.25430447 0.0113694776 500.9267 500.9469
#> 10 5.009099 4.769105 0.25430447 0.0113694776 500.8910 500.9212