<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/pipe-friendly-bootstrapping-with-list-variables-in-rstats/"></div></div><div><a data-url="https://www.r-bloggers.com/pipe-friendly-bootstrapping-with-list-variables-in-rstats/" data-counturl="https://www.r-bloggers.com/pipe-friendly-bootstrapping-with-list-variables-in-rstats/" data-count="horizontal" data-text="Pipe-friendly bootstrapping with list-variables in #rstats" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://strengejacke.wordpress.com/2016/08/01/pipe-friendly-bootstrapping-with-list-variables-in-rstats/"> R – Strenge Jacke!</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>A few days ago, my package <strong>sjstats</strong> was <a target="_blank" href="https://cran.r-project.org/package=sjstats">updated on CRAN</a>. Most functions of this package are convenient functions for common statistical computations, especially for (mixed) regression models. This latest update introduces some <a target="_blank" href="https://cran.r-project.org/web/packages/magrittr/vignettes/magrittr.html">pipe-friendly</a> bootstrapping-methods, namely <code>bootstrap()</code>, <code>boot_ci()</code>, <code>boot_se()</code> and <code>boot_p()</code>. In this post, I just wanted to give a quick example of these functions, used within a pipeline-workflow.</p>
<p>First, load the required libraries:</p>
<pre>library(dplyr)
library(sjstats)</pre>
<p>Now, init the sample data and fit a regular model. The model estimates how the dependency (<em>e42dep</em>) of an older person is related to the burden of care (<em>neg_c_7</em>) of a person who provides care to the frail older people:</p>
<pre>data(efc)
fit &lt;- lm(neg_c_7 ~ e42dep + c161sex, data = efc)</pre>
<p>I demonstrate the <code>boot_ci()</code>-function, so the confidence intervals are of interest here:</p>
<pre>confint(fit)

>                  2.5 %    97.5 %
> (Intercept)  5.3374378 7.7794162
> e42dep       1.2929451 1.7964296
> c161sex     -0.1193198 0.9871336</pre>
<p>Now let’s see to obtain bootstrapped confidence intervals for this model. First, the <code>bootstrap()</code>-function generates bootstrap replicates and returns a data-frame with just one column, <code>$strap</code>, which is a <em>list-variable</em> with bootstrap samples:</p>
<pre>bootstrap(efc, 1000)</pre>
<p>This is how the list-variable looks like:</p>
<pre># A tibble: 1,000 x 1
                     strap
                    &lt;list>
1  &lt;data.frame [908 x 26]>
2  &lt;data.frame [908 x 26]>
3  &lt;data.frame [908 x 26]>
4  &lt;data.frame [908 x 26]>
5  &lt;data.frame [908 x 26]>
6  &lt;data.frame [908 x 26]>
7  &lt;data.frame [908 x 26]>
8  &lt;data.frame [908 x 26]>
9  &lt;data.frame [908 x 26]>
10 &lt;data.frame [908 x 26]>
# ... with 990 more rows</pre>
<p>Since all data frames are saved in a list, you can use <code>lapply()</code> to easily run the same linear model (used above) over all bootstrap samples and save these fitted model objects in another list-variable (named <em>models</em> in the example below). Then, using <code>lapply()</code> again, we can extract the coefficient of interest (here, the second coefficient, which is the estimated <em>e42dep</em>) for each „bootstrap“ model and save these coefficients in another variable (named <em>dependency</em> in the example below). Finally, we use the <code>boot_ci()</code>-function to calculate confidence intervals of the bootstrapped coefficients.</p>
<p>The complete code looks like this:</p>
<pre>efc %>% 
  # generate bootstrape replicates, saved in
  # the list-variable 'strap'
  bootstrap(1000) %>% 
  # run linear model on all bootstrap samples
  mutate(models = lapply(.$strap, function(x) {
    lm(neg_c_7 ~ e42dep + c161sex, data = x)
  })) %>%
  # extract coefficient for &quot;e42dep&quot; (dependency) variable
  mutate(dependency = unlist(lapply(.$models, function(x) coef(x)[2]))) %>%
  # compute boostrapped confidence intervals
  boot_ci(dependency)</pre>
<p>And the result (depending on your <code>seed()</code>) is:</p>
<pre>conf.low conf.high 
1.303847  1.790724</pre>
<p>The complete code, put together:</p>
<pre>library(dplyr)
library(sjstats)
data(efc)

fit &lt;- lm(neg_c_7 ~ e42dep + c161sex, data = efc)
confint(fit)

efc %>% 
  bootstrap(1000) %>% 
  mutate(models = lapply(.$strap, function(x) {
    lm(neg_c_7 ~ e42dep + c161sex, data = x)
  })) %>%
  mutate(dependency = unlist(lapply(.$models, function(x) coef(x)[2]))) %>%
  boot_ci(dependency)</pre>
<p> Tagged: <a target="_blank" href="https://strengejacke.wordpress.com/tag/bootstrap/">bootstrap</a>, <a target="_blank" href="https://strengejacke.wordpress.com/tag/pipe/">pipe</a>, <a target="_blank" href="https://strengejacke.wordpress.com/tag/r/">R</a>, <a target="_blank" href="https://strengejacke.wordpress.com/tag/rstats/">rstats</a> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/strengejacke.wordpress.com/1123/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/strengejacke.wordpress.com/1123/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/pipe-friendly-bootstrapping-with-list-variables-in-rstats/"></div></div><div><a data-url="https://www.r-bloggers.com/pipe-friendly-bootstrapping-with-list-variables-in-rstats/" data-counturl="https://www.r-bloggers.com/pipe-friendly-bootstrapping-with-list-variables-in-rstats/" data-count="vertical" data-text="Pipe-friendly bootstrapping with list-variables in #rstats" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://strengejacke.wordpress.com/2016/08/01/pipe-friendly-bootstrapping-with-list-variables-in-rstats/"> R – Strenge Jacke!</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/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=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/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/3tr0gKgU2vk"></a></div>