<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/changing-names-in-the-tidyverse-an-example-for-many-regressions/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://statbandit.wordpress.com/2017/03/09/changing-names-in-the-tidyverse-an-example-for-many-regressions/"> R – Stat Bandit</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>A collaborator posed an interesting R question to me today. She wanted to do<br>
several regressions using different outcomes, with models being computed on<br>
different strata defined by a combination of experimental design variables. She then just wanted to extract the p-values for the slopes for each of the models, and then<br>
filter the strata based on p-value levels.</p>
<p>This seems straighforward, right? Let’s set up a toy example:</p>
<pre>
library(tidyverse)

dat &lt;- as_tibble(expand.grid(letters[1:4], 1:5))
d &lt;- vector('list', nrow(dat))
set.seed(102)
for(i in 1:nrow(dat)){
x &lt;- rnorm(100)
d[[i]] &lt;- tibble(x = x, y1 = 3 - 2*x + rnorm(100), y2 = -4+5*x+rnorm(100))
}
dat &lt;- as_tibble(bind_cols(dat, tibble(dat=d))) %>% unnest()
knitr::kable(head(dat), format='html')
</pre>
<table>
<thead>
<tr>
<th> Var1 </th>
<th> Var2 </th>
<th> x </th>
<th> y1 </th>
<th> y2 </th>
</tr>
</thead>
<tbody>
<tr>
<td> a </td>
<td> 1 </td>
<td> 0.1805229 </td>
<td> 4.2598245 </td>
<td> -3.004535 </td>
</tr>
<tr>
<td> a </td>
<td> 1 </td>
<td> 0.7847340 </td>
<td> 0.0023338 </td>
<td> -2.104949 </td>
</tr>
<tr>
<td> a </td>
<td> 1 </td>
<td> -1.3531646 </td>
<td> 3.1711898 </td>
<td> -9.156758 </td>
</tr>
<tr>
<td> a </td>
<td> 1 </td>
<td> 1.9832982 </td>
<td> -0.7140910 </td>
<td> 5.966377 </td>
</tr>
<tr>
<td> a </td>
<td> 1 </td>
<td> 1.2384717 </td>
<td> 0.3523034 </td>
<td> 2.131004 </td>
</tr>
<tr>
<td> a </td>
<td> 1 </td>
<td> 1.2006174 </td>
<td> 0.6267716 </td>
<td> 1.752106 </td>
</tr>
</tbody>
</table>
<p>Now we’re going to perform two regressions, one using <code>y1</code> and one using <code>y2</code> as the dependent variables, for each stratum defined by <code>Var1</code> and <code>Var2</code>.</p>
<pre>
out %
nest(-Var1, -Var2) %>%
mutate(model1 = map(data, ~lm(y1~x, data=.)),
model2 = map(data, ~lm(y2~x, data=.)))
</pre>
<p>Now conceptually, all we do is tidy up the output for the models using the <code>broom</code> package, filter on the rows containg the slope information, and extract the p-values, right? Not quite….</p>
<pre>
library(broom)
out_problem % mutate(output1 = map(model1, ~tidy(.)),
output2 = map(model2, ~tidy(.))) %>%
select(-data, -model1, -model2) %>%
unnest()
names(out_problem)
</pre>
<p>[1] &quot;Var1&quot; &quot;Var2&quot; &quot;term&quot; &quot;estimate&quot; &quot;std.error&quot;<br>
[6] &quot;statistic&quot; &quot;p.value&quot; &quot;term&quot; &quot;estimate&quot; &quot;std.error&quot;<br>
[11] &quot;statistic&quot; &quot;p.value&quot;</p>
<p>We've got two sets of output, but with the same column names!!! This is a problem! An easy solution would be to preface the column names with the name of the response variable. I struggled with this today until I discovered the <em>secret function</em>.</p>
<pre>
out_nice % mutate(output1 = map(model1, ~tidy(.)),
output2 = map(model2, ~tidy(.)),
output1 = map(output1, ~setNames(., paste('y1', names(.), sep='_'))),
output2 = map(output2, ~setNames(., paste('y2', names(.), sep='_')))) %>%
select(-data, -model1, -model2) %>%
unnest()
</pre>
<p>This is a compact representation of the results of both regressions by strata, and we can extract the information we would like very easily. For example, to extract the stratum-specific slope estimates:</p>
<pre>
out_nice %>% filter(y1_term=='x') %>%
select(Var1, Var2, ends_with('estimate')) %>%
knitr::kable(digits=3, format='html')
</pre>
<table>
<thead>
<tr>
<th> Var1 </th>
<th> Var2 </th>
<th> y1_estimate </th>
<th> y2_estimate </th>
</tr>
</thead>
<tbody>
<tr>
<td> a </td>
<td> 1 </td>
<td> -1.897 </td>
<td> 5.036 </td>
</tr>
<tr>
<td> b </td>
<td> 1 </td>
<td> -2.000 </td>
<td> 5.022 </td>
</tr>
<tr>
<td> c </td>
<td> 1 </td>
<td> -1.988 </td>
<td> 4.888 </td>
</tr>
<tr>
<td> d </td>
<td> 1 </td>
<td> -2.089 </td>
<td> 5.089 </td>
</tr>
<tr>
<td> a </td>
<td> 2 </td>
<td> -2.052 </td>
<td> 5.015 </td>
</tr>
<tr>
<td> b </td>
<td> 2 </td>
<td> -1.922 </td>
<td> 5.004 </td>
</tr>
<tr>
<td> c </td>
<td> 2 </td>
<td> -1.936 </td>
<td> 4.969 </td>
</tr>
<tr>
<td> d </td>
<td> 2 </td>
<td> -1.961 </td>
<td> 4.959 </td>
</tr>
<tr>
<td> a </td>
<td> 3 </td>
<td> -2.043 </td>
<td> 5.017 </td>
</tr>
<tr>
<td> b </td>
<td> 3 </td>
<td> -2.045 </td>
<td> 4.860 </td>
</tr>
<tr>
<td> c </td>
<td> 3 </td>
<td> -1.996 </td>
<td> 5.009 </td>
</tr>
<tr>
<td> d </td>
<td> 3 </td>
<td> -1.922 </td>
<td> 4.894 </td>
</tr>
<tr>
<td> a </td>
<td> 4 </td>
<td> -2.000 </td>
<td> 4.942 </td>
</tr>
<tr>
<td> b </td>
<td> 4 </td>
<td> -2.000 </td>
<td> 4.932 </td>
</tr>
<tr>
<td> c </td>
<td> 4 </td>
<td> -2.033 </td>
<td> 5.042 </td>
</tr>
<tr>
<td> d </td>
<td> 4 </td>
<td> -2.165 </td>
<td> 5.049 </td>
</tr>
<tr>
<td> a </td>
<td> 5 </td>
<td> -2.094 </td>
<td> 5.010 </td>
</tr>
<tr>
<td> b </td>
<td> 5 </td>
<td> -1.961 </td>
<td> 5.122 </td>
</tr>
<tr>
<td> c </td>
<td> 5 </td>
<td> -2.106 </td>
<td> 5.153 </td>
</tr>
<tr>
<td> d </td>
<td> 5 </td>
<td> -1.974 </td>
<td> 5.009 </td>
</tr>
</tbody>
</table>
<p> </p>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/statbandit.wordpress.com/620/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/statbandit.wordpress.com/620/" 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>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://statbandit.wordpress.com/2017/03/09/changing-names-in-the-tidyverse-an-example-for-many-regressions/"> R – Stat Bandit</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/E2th5vMIuAA"></a></div>