<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/computing-se-for-psd-indices/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://derekogle.com/fishR/2016-12-09-PSD_SE"> fishR Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<hr>
<p>A user of my <a target="_blank" rel="nofollow" href="http://derekogle.com/IFAR/">Introductory Fisheries Analyses with R book</a> recently asked me how to compute standard errors (SE) for the various PSD indices by using the usual equation for the SE of a proportion (square root of p times (1-p) divided by n). Below is my response using the <a target="_blank" rel="nofollow" href="http://derekogle.com/IFAR/scripts/InchLake1113.csv">Inch Lake data</a> from the book.</p>
<ul>
<li>Load required packages.</li>
</ul>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>FSA</span><span>)</span><span>
</span><span>library</span><span>(</span><span>magrittr</span><span>)</span><span>
</span><span>library</span><span>(</span><span>dplyr</span><span>)</span></code></pre>
</figure>
<ul>
<li>Load (and prepare) the Inch Lake data used in the Size Structure chapter of the book (this and next step is from <a target="_blank" rel="nofollow" href="http://derekogle.com/IFAR/scripts/SizeStructure.R">this script</a>).</li>
</ul>
<figure>
<pre><code data-lang="r"><span>inchAlls</span><span> </span><span>&lt;-</span><span> </span><span>read.csv</span><span>(</span><span>&quot;http://derekogle.com/IFAR/scripts/InchLake1113.csv&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>gcat</span><span>=</span><span>psdAdd</span><span>(</span><span>tl</span><span>,</span><span>species</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>filterD</span><span>(</span><span>gcat</span><span>!=</span><span>&quot;substock&quot;</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## No known Gabelhouse (PSD) lengths for Iowa Darter</code></pre>
</figure>
<ul>
<li>Compute the PSD-X values.</li>
</ul>
<figure>
<pre><code data-lang="r"><span>(</span><span> </span><span>freq</span><span> </span><span>&lt;-</span><span> </span><span>xtabs</span><span>(</span><span>~</span><span>species</span><span>+</span><span>gcat</span><span>,</span><span>data</span><span>=</span><span>inchAlls</span><span>)</span><span> </span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">##                  gcat
## species           stock quality preferred memorable
##   Black Crappie      33      40         8        81
##   Bluegill          571     199       223        43
##   Largemouth Bass    26      36         0         0
##   Pumpkinseed         5       2         0         0
##   Yellow Perch        4       0         0         1</code></pre>
</figure>
<figure>
<pre><code data-lang="r"><span>iPSDs</span><span> </span><span>&lt;-</span><span> </span><span>prop.table</span><span>(</span><span>freq</span><span>,</span><span>margin</span><span>=</span><span>1</span><span>)</span><span>*</span><span>100</span><span>
</span><span>(</span><span> </span><span>PSDs</span><span> </span><span>&lt;-</span><span> </span><span>t</span><span>(</span><span>apply</span><span>(</span><span>iPSDs</span><span>,</span><span>MARGIN</span><span>=</span><span>1</span><span>,</span><span>FUN</span><span>=</span><span>rcumsum</span><span>))</span><span> </span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">##                  gcat
## species           stock  quality preferred memorable
##   Black Crappie     100 79.62963  54.93827 50.000000
##   Bluegill          100 44.88417  25.67568  4.150579
##   Largemouth Bass   100 58.06452   0.00000  0.000000
##   Pumpkinseed       100 28.57143   0.00000  0.000000
##   Yellow Perch      100 20.00000  20.00000 20.000000</code></pre>
</figure>
<ul>
<li>Compute the row sums for the <code>freq</code> table to get the total number of fish (that are stock size or greater). These will be n in the SE calculations.</li>
</ul>
<figure>
<pre><code data-lang="r"><span>(</span><span> </span><span>sums</span><span> </span><span>&lt;-</span><span> </span><span>rowSums</span><span>(</span><span>freq</span><span>)</span><span> </span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">##   Black Crappie        Bluegill Largemouth Bass     Pumpkinseed 
##             162            1036              62               7 
##    Yellow Perch 
##               5</code></pre>
</figure>
<ul>
<li>Compute the SE for PSD-Q.</li>
</ul>
<figure>
<pre><code data-lang="r"><span>SEs</span><span> </span><span>&lt;-</span><span> </span><span>sqrt</span><span>(</span><span>PSDs</span><span>[,</span><span>&quot;quality&quot;</span><span>]</span><span>*</span><span>(</span><span>100</span><span>-</span><span>iPSDs</span><span>[,</span><span>&quot;quality&quot;</span><span>])</span><span>/</span><span>sums</span><span>)</span><span>
</span><span>round</span><span>(</span><span>SEs</span><span>,</span><span>1</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">##   Black Crappie        Bluegill Largemouth Bass     Pumpkinseed 
##             6.1             1.9             6.3            17.1 
##    Yellow Perch 
##            20.0</code></pre>
</figure>
<ul>
<li>Repeat for other size indices. For example, for PSD-P.</li>
</ul>
<figure>
<pre><code data-lang="r"><span>SEs</span><span> </span><span>&lt;-</span><span> </span><span>sqrt</span><span>(</span><span>PSDs</span><span>[,</span><span>&quot;preferred&quot;</span><span>]</span><span>*</span><span>(</span><span>100</span><span>-</span><span>iPSDs</span><span>[,</span><span>&quot;preferred&quot;</span><span>])</span><span>/</span><span>sums</span><span>)</span><span>
</span><span>round</span><span>(</span><span>SEs</span><span>,</span><span>1</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">##   Black Crappie        Bluegill Largemouth Bass     Pumpkinseed 
##             5.7             1.4             0.0             0.0 
##    Yellow Perch 
##            20.0</code></pre>
</figure>
<p>Note that the SE formula is usually used for proportions (and then multiplied by 100 to get a SE for percentages). However, it can also be shown algebraically to be useful for percentages.</p>
<hr>
<p>I would caution against using these standard errors to produce confidence intervals for the PSD-X values. If confidence intervals are the end-product for this analysis, then I suggest using the methods described in my book.</p>
<hr>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://derekogle.com/fishR/2016-12-09-PSD_SE"> fishR Blog</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/r3WAwLXjBFI"></a></div>