Skip to content

Commit

Permalink
PRac 5 small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasdavies committed Sep 9, 2024
1 parent 657ceaa commit 2762449
Show file tree
Hide file tree
Showing 39 changed files with 64 additions and 70 deletions.
3 changes: 1 addition & 2 deletions 05_Metapop_practical.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ state <- c(S1 = S1_0, I1 = I1_0, R1 = R1_0, C1 = C1_0,
output_raw <- ode(y = state,
times = times,
func = SIR_metapop_model,
parms = parameters,
method = rk4)
parms = parameters)
# Convert to data frame for easy extraction of columns
output <- as.data.frame(output_raw)
Expand Down
6 changes: 2 additions & 4 deletions 05_Metapop_solutions.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ state <- c(S1 = S1_0, I1 = I1_0, R1 = R1_0, C1 = C1_0,
output_raw <- ode(y = state,
times = times,
func = SIR_metapop_model,
parms = parameters,
method = rk4)
parms = parameters)
# Convert to data frame for easy extraction of columns
output <- as.data.frame(output_raw)
Expand Down Expand Up @@ -233,8 +232,7 @@ state <- c(S1 = S1_0, I1 = I1_0, R1 = R1_0, C1 = C1_0, S2 = S2_0, I2 = I2_0,
output_raw <- ode(y = state,
times = times,
func = SIR_metapop_model_3,
parms = parameters,
method = rk4)
parms = parameters)
# Convert to data frame for easy extraction of columns
output <- as.data.frame(output_raw)
Expand Down
4 changes: 2 additions & 2 deletions docs/00_02_programming_practical.html
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ <h3 class="anchored" data-anchor-id="the-first-way---using-loops">The first way
</div>
<div class="sourceCode cell-code" id="cb4"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="fu">toc</span>() <span class="co"># output the time that the script took</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>loop: 0.01 sec elapsed</code></pre>
<pre><code>loop: 0.011 sec elapsed</code></pre>
</div>
</div>
<p>Instead of accessing each element of a matrix via for loops, R can also apply operations to matrices or dataframe as chunks. This can speed up the code, reduce the amount of code, and can make it easier to read.</p>
Expand All @@ -331,7 +331,7 @@ <h3 class="anchored" data-anchor-id="the-second-way---vectorisation">The second
</div>
<div class="sourceCode cell-code" id="cb8"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="fu">toc</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>vectorised: 0.007 sec elapsed</code></pre>
<pre><code>vectorised: 0.006 sec elapsed</code></pre>
</div>
</div>
</section>
Expand Down
8 changes: 4 additions & 4 deletions docs/00_02_programming_solutions.html
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ <h3 class="anchored" data-anchor-id="the-first-way---using-loops">The first way
</div>
<div class="sourceCode cell-code" id="cb4"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="fu">toc</span>() <span class="co"># output the time that the script took</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>loop: 0.012 sec elapsed</code></pre>
<pre><code>loop: 0.011 sec elapsed</code></pre>
</div>
</div>
<p>Instead of accessing each element of a matrix via for loops, R can also apply operations to matrices or dataframe as chunks. This can speed up the code, reduce the amount of code, and can make it easier to read.</p>
Expand All @@ -335,7 +335,7 @@ <h3 class="anchored" data-anchor-id="the-second-way---vectorisation">The second
</div>
<div class="sourceCode cell-code" id="cb8"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="fu">toc</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>vectorised: 0.008 sec elapsed</code></pre>
<pre><code>vectorised: 0.006 sec elapsed</code></pre>
</div>
</div>
</section>
Expand All @@ -356,7 +356,7 @@ <h3 class="anchored" data-anchor-id="the-third-way---mapping">The third way -
</div>
<div class="sourceCode cell-code" id="cb12"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a><span class="fu">toc</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>map: 0.008 sec elapsed</code></pre>
<pre><code>map: 0.007 sec elapsed</code></pre>
</div>
</div>
<p>How did each of the three scripts do according to our 2 criteria?</p>
Expand Down Expand Up @@ -491,7 +491,7 @@ <h2 class="anchored" data-anchor-id="functions">(2) Functions</h2>
<span id="cb26-4"><a href="#cb26-4" aria-hidden="true" tabindex="-1"></a> <span class="at">.f=</span> <span class="sc">~</span><span class="fu">min</span>(<span class="fu">uniroot.all</span>(<span class="cf">function</span>(s.inf) <span class="fu">final.size.root.twoargs</span>(.x, s.inf), <span class="fu">c</span>(<span class="dv">0</span>,<span class="dv">1</span>)))))</span>
<span id="cb26-5"><a href="#cb26-5" aria-hidden="true" tabindex="-1"></a><span class="fu">toc</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>root map: 0.008 sec elapsed</code></pre>
<pre><code>root map: 0.005 sec elapsed</code></pre>
</div>
</div>

Expand Down
29 changes: 14 additions & 15 deletions docs/05_Metapop_practical.html
Original file line number Diff line number Diff line change
Expand Up @@ -277,21 +277,20 @@ <h1 class="title">05. Metapopulations with ODEs</h1>
<span id="cb1-54"><a href="#cb1-54" aria-hidden="true" tabindex="-1"></a>output_raw <span class="ot">&lt;-</span> <span class="fu">ode</span>(<span class="at">y =</span> state, </span>
<span id="cb1-55"><a href="#cb1-55" aria-hidden="true" tabindex="-1"></a> <span class="at">times =</span> times, </span>
<span id="cb1-56"><a href="#cb1-56" aria-hidden="true" tabindex="-1"></a> <span class="at">func =</span> SIR_metapop_model, </span>
<span id="cb1-57"><a href="#cb1-57" aria-hidden="true" tabindex="-1"></a> <span class="at">parms =</span> parameters,</span>
<span id="cb1-58"><a href="#cb1-58" aria-hidden="true" tabindex="-1"></a> <span class="at">method =</span> rk4)</span>
<span id="cb1-59"><a href="#cb1-59" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-60"><a href="#cb1-60" aria-hidden="true" tabindex="-1"></a><span class="co"># Convert to data frame for easy extraction of columns</span></span>
<span id="cb1-61"><a href="#cb1-61" aria-hidden="true" tabindex="-1"></a>output <span class="ot">&lt;-</span> <span class="fu">as.data.frame</span>(output_raw)</span>
<span id="cb1-62"><a href="#cb1-62" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-63"><a href="#cb1-63" aria-hidden="true" tabindex="-1"></a><span class="co"># Plot output</span></span>
<span id="cb1-64"><a href="#cb1-64" aria-hidden="true" tabindex="-1"></a><span class="fu">par</span>(<span class="at">mfrow =</span> <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">1</span>))</span>
<span id="cb1-65"><a href="#cb1-65" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(output<span class="sc">$</span>time, output<span class="sc">$</span>I1, <span class="at">type =</span> <span class="st">"l"</span>, <span class="at">col =</span> <span class="dv">4</span>, <span class="at">lwd =</span> <span class="dv">2</span>, <span class="at">ylim =</span> <span class="fu">c</span>(<span class="dv">0</span>, N1),</span>
<span id="cb1-66"><a href="#cb1-66" aria-hidden="true" tabindex="-1"></a> <span class="at">xlab =</span> <span class="st">"Time"</span>, <span class="at">ylab =</span> <span class="st">"Number"</span>, <span class="at">main =</span> <span class="st">""</span>)</span>
<span id="cb1-67"><a href="#cb1-67" aria-hidden="true" tabindex="-1"></a><span class="fu">lines</span>(output<span class="sc">$</span>time, output<span class="sc">$</span>I2, <span class="at">lwd =</span> <span class="dv">2</span>, <span class="at">col =</span> <span class="dv">2</span>, <span class="at">type =</span> <span class="st">"l"</span>)</span>
<span id="cb1-68"><a href="#cb1-68" aria-hidden="true" tabindex="-1"></a><span class="fu">legend</span>(<span class="st">"topright"</span>, </span>
<span id="cb1-69"><a href="#cb1-69" aria-hidden="true" tabindex="-1"></a> <span class="at">legend =</span> <span class="fu">c</span>(<span class="st">"Infected in population 1"</span>, </span>
<span id="cb1-70"><a href="#cb1-70" aria-hidden="true" tabindex="-1"></a> <span class="st">"Infected in population 2"</span>),</span>
<span id="cb1-71"><a href="#cb1-71" aria-hidden="true" tabindex="-1"></a> <span class="at">lty =</span> <span class="fu">rep</span>(<span class="dv">1</span>, <span class="dv">2</span>), <span class="at">col =</span> <span class="fu">c</span>(<span class="dv">4</span>, <span class="dv">2</span>), <span class="at">lwd =</span> <span class="dv">2</span>, <span class="at">bty =</span> <span class="st">"n"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<span id="cb1-57"><a href="#cb1-57" aria-hidden="true" tabindex="-1"></a> <span class="at">parms =</span> parameters)</span>
<span id="cb1-58"><a href="#cb1-58" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-59"><a href="#cb1-59" aria-hidden="true" tabindex="-1"></a><span class="co"># Convert to data frame for easy extraction of columns</span></span>
<span id="cb1-60"><a href="#cb1-60" aria-hidden="true" tabindex="-1"></a>output <span class="ot">&lt;-</span> <span class="fu">as.data.frame</span>(output_raw)</span>
<span id="cb1-61"><a href="#cb1-61" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-62"><a href="#cb1-62" aria-hidden="true" tabindex="-1"></a><span class="co"># Plot output</span></span>
<span id="cb1-63"><a href="#cb1-63" aria-hidden="true" tabindex="-1"></a><span class="fu">par</span>(<span class="at">mfrow =</span> <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">1</span>))</span>
<span id="cb1-64"><a href="#cb1-64" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(output<span class="sc">$</span>time, output<span class="sc">$</span>I1, <span class="at">type =</span> <span class="st">"l"</span>, <span class="at">col =</span> <span class="dv">4</span>, <span class="at">lwd =</span> <span class="dv">2</span>, <span class="at">ylim =</span> <span class="fu">c</span>(<span class="dv">0</span>, N1),</span>
<span id="cb1-65"><a href="#cb1-65" aria-hidden="true" tabindex="-1"></a> <span class="at">xlab =</span> <span class="st">"Time"</span>, <span class="at">ylab =</span> <span class="st">"Number"</span>, <span class="at">main =</span> <span class="st">""</span>)</span>
<span id="cb1-66"><a href="#cb1-66" aria-hidden="true" tabindex="-1"></a><span class="fu">lines</span>(output<span class="sc">$</span>time, output<span class="sc">$</span>I2, <span class="at">lwd =</span> <span class="dv">2</span>, <span class="at">col =</span> <span class="dv">2</span>, <span class="at">type =</span> <span class="st">"l"</span>)</span>
<span id="cb1-67"><a href="#cb1-67" aria-hidden="true" tabindex="-1"></a><span class="fu">legend</span>(<span class="st">"topright"</span>, </span>
<span id="cb1-68"><a href="#cb1-68" aria-hidden="true" tabindex="-1"></a> <span class="at">legend =</span> <span class="fu">c</span>(<span class="st">"Infected in population 1"</span>, </span>
<span id="cb1-69"><a href="#cb1-69" aria-hidden="true" tabindex="-1"></a> <span class="st">"Infected in population 2"</span>),</span>
<span id="cb1-70"><a href="#cb1-70" aria-hidden="true" tabindex="-1"></a> <span class="at">lty =</span> <span class="fu">rep</span>(<span class="dv">1</span>, <span class="dv">2</span>), <span class="at">col =</span> <span class="fu">c</span>(<span class="dv">4</span>, <span class="dv">2</span>), <span class="at">lwd =</span> <span class="dv">2</span>, <span class="at">bty =</span> <span class="st">"n"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
Expand Down
Binary file modified docs/05_Metapop_practical_files/figure-html/unnamed-chunk-1-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/05_Metapop_practical_files/figure-html/unnamed-chunk-2-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 2762449

Please sign in to comment.