<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/freedmans-paradox/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.alexejgossmann.com/Freedmans_paradox/"> Alexej's blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Recently I came across the classical 1983 paper <a target="_blank" rel="nofollow" href="http://www.public.asu.edu/~gasweete/crj604/readings/1983-Freedman%20(Screening%20Regression%20Equations).pdf"><em>A note on screening regression equations</em></a> by <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/David_A._Freedman">David Freedman</a>. Freedman shows in an impressive way the dangers of data reuse in statistical analyses. The potentially dangerous scenarios include those where the results of one statistical procedure performed on the data are fed into another procedure performed on the same data. As a concrete example Freedman considers the practice of performing variable selection first, and then fitting another model using only the identified variables on the same data that was used to identify them in the first place. Because of the unexpectedly high severity of the problem this phenomenon became known as <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Freedman%27s_paradox">“Freedman’s paradox”</a>. Moreover, in his paper Freedman derives asymptotic estimates for the resulting errors.</p>
<p>The 1983 paper presents a simulation with only 10 repetitions. But in the present day it is very easy (both in terms of computational time and implementation difficulty) to reproduce the simulation with many more repetitions (even my phone’s computational power is probably higher than that of the high performance computer that Freedman used in the 80’s). We also have more convenient ways to visualize the results than in the 80’s. So let’s do it.</p>
<p>I am going to use a few R packages (most notably the package <code>broom</code> to fit and analyze many many linear models in a single step).</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>dplyr</span><span>)</span><span>
</span><span>library</span><span>(</span><span>broom</span><span>)</span><span>
</span><span>library</span><span>(</span><span>ggplot2</span><span>)</span><span>
</span><span>library</span><span>(</span><span>tidyr</span><span>)</span><span>
</span><span>set.seed</span><span>(</span><span>20170605</span><span>)</span></code></pre>
</figure>
<p>The considered data structure is the following:</p>
<ul>
<li>A matrix of predictors with 100 rows and 50 columns is generated with independent standard normal entries.</li>
<li>The response variable is generated independently of the model matrix (also from the standard normal distribution), i.e., the true answer is that there is no relationship between predictors and response.</li>
</ul>
<p>Instead of Freedman’s 10 repetitions we perform 1000. So let’s generate all 1000 datasets at once as stacked in a large data frame:</p>
<figure>
<pre><code data-lang="r"><span>n_row</span><span> </span><span>&lt;-</span><span> </span><span>100</span><span>
</span><span># n_col is set to 51 because the 51st column will serve as y
</span><span>n_col</span><span> </span><span>&lt;-</span><span> </span><span>51</span><span>
</span><span>n_rep</span><span> </span><span>&lt;-</span><span> </span><span>1000</span><span>
</span><span># a stack of matrices for all n_rep repetitions is generated...
</span><span>X</span><span> </span><span>&lt;-</span><span> </span><span>matrix</span><span>(</span><span>rnorm</span><span>(</span><span>n_rep</span><span> </span><span>*</span><span> </span><span>n_row</span><span> </span><span>*</span><span> </span><span>n_col</span><span>),</span><span> </span><span>n_rep</span><span> </span><span>*</span><span> </span><span>n_row</span><span>,</span><span> </span><span>n_col</span><span>)</span><span>
</span><span>colnames</span><span>(</span><span>X</span><span>)</span><span> </span><span>&lt;-</span><span> </span><span>paste0</span><span>(</span><span>&quot;X&quot;</span><span>,</span><span> </span><span>1</span><span>:</span><span>n_col</span><span>)</span><span>
</span><span># ...and then transformed to a data frame with a repetition number column
</span><span>X_df</span><span> </span><span>&lt;-</span><span> </span><span>as_data_frame</span><span>(</span><span>X</span><span>)</span><span> </span><span>%>%</span><span> </span><span>mutate</span><span>(</span><span>repetition</span><span> </span><span>=</span><span> </span><span>rep</span><span>(</span><span>1</span><span>:</span><span>n_rep</span><span>,</span><span> </span><span>each</span><span> </span><span>=</span><span> </span><span>n_row</span><span>))</span></code></pre>
</figure>
<p><strong>The data are analyzed in two successive linear models. The second (illegally) reusing the results of the first.</strong></p>
<p><em>The first model fit.</em><br>
After the 1000 ordinary linear models are fit to the data, we record for each of them the R squared, the F test statistic with corresponding p-value, and the t test statistics with p-values for the individual regression coefficients.</p>
<p>Using functions from the <code>broom</code> package we can fit and extract information from all 1000 models at once.</p>
<figure>
<pre><code data-lang="r"><span># all models can be fit at once...
</span><span>models_df</span><span> </span><span>=</span><span> </span><span>X_df</span><span> </span><span>%>%</span><span> </span><span>group_by</span><span>(</span><span>repetition</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>do</span><span>(</span><span>full_model</span><span> </span><span>=</span><span> </span><span>lm</span><span>(</span><span>X</span><span>51</span><span> </span><span>~</span><span> </span><span>.</span><span> </span><span>+</span><span> </span><span>0</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>select</span><span>(</span><span>.</span><span>,</span><span> </span><span>-</span><span>repetition</span><span>)))</span><span>
</span><span># ...then the results are extracted
</span><span>model_coefs</span><span> </span><span>&lt;-</span><span> </span><span>tidy</span><span>(</span><span>models_df</span><span>,</span><span> </span><span>full_model</span><span>)</span><span>
</span><span>model_statistics</span><span> </span><span>&lt;-</span><span> </span><span>glance</span><span>(</span><span>models_df</span><span>,</span><span> </span><span>full_model</span><span>)</span><span>
</span><span>model_statistics</span><span>$</span><span>data_reuse</span><span> </span><span>&lt;-</span><span> </span><span>rep</span><span>(</span><span>FALSE</span><span>,</span><span> </span><span>nrow</span><span>(</span><span>model_statistics</span><span>))</span></code></pre>
</figure>
<p><em>The second model fit.</em><br>
For each one of the first 1000 models, the corresponding second linear model is fit using only those variables which have p-values significant at the 25% level in the first model.<br>
That is, the second model uses the first model for variable selection.</p>
<p>This gives us 1000 reduced re-fitted linear models. We record the same model statistics (R squared, F, and t tests) as for the first group of models.</p>
<figure>
<pre><code data-lang="r"><span>reduced_models</span><span> </span><span>&lt;-</span><span> </span><span>list</span><span>()</span><span>
</span><span>for</span><span> </span><span>(</span><span>i</span><span> </span><span>in</span><span> </span><span>1</span><span>:</span><span>n_rep</span><span>)</span><span> </span><span>{</span><span>
</span><span>full_data</span><span> </span><span>&lt;-</span><span> </span><span>X_df</span><span> </span><span>%>%</span><span> </span><span>filter</span><span>(</span><span>repetition</span><span> </span><span>==</span><span> </span><span>i</span><span>)</span><span>
</span><span>significant_coefs</span><span> </span><span>&lt;-</span><span> </span><span>model_coefs</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>repetition</span><span> </span><span>==</span><span> </span><span>i</span><span> </span><span>&amp;</span><span> </span><span>p.value</span><span> </span><span></span><span> </span><span>0.25</span><span>)</span><span>
</span><span>reduced_data</span><span> </span><span>&lt;-</span><span> </span><span>select</span><span>(</span><span>full_data</span><span>,</span><span>
</span><span>one_of</span><span>(</span><span>unlist</span><span>(</span><span>significant_coefs</span><span>[</span><span> </span><span>,</span><span> </span><span>&quot;term&quot;</span><span>])),</span><span> </span><span>X</span><span>51</span><span>)</span><span>
</span><span>reduced_models</span><span>[[</span><span>i</span><span>]]</span><span> </span><span>&lt;-</span><span> </span><span>lm</span><span>(</span><span>X</span><span>51</span><span> </span><span>~</span><span> </span><span>.</span><span> </span><span>+</span><span> </span><span>0</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>reduced_data</span><span>)</span><span>
</span><span>tmp_df</span><span> </span><span>&lt;-</span><span> </span><span>glance</span><span>(</span><span>reduced_models</span><span>[[</span><span>i</span><span>]])</span><span>
</span><span>tmp_df</span><span>$</span><span>repetition</span><span> </span><span>&lt;-</span><span> </span><span>i</span><span>
</span><span>tmp_df</span><span>$</span><span>data_reuse</span><span> </span><span>&lt;-</span><span> </span><span>TRUE</span><span>
</span><span>model_statistics</span><span> </span><span>&lt;-</span><span> </span><span>bind_rows</span><span>(</span><span>model_statistics</span><span>,</span><span> </span><span>tmp_df</span><span>)</span><span>
</span><span>}</span></code></pre>
</figure>
<p>Finally let’s look at the results. The figure shows the distributions of the considered model statistics across the 1000 repetitions for model fits with and without data reuse (the code producing this figure is given at the bottom of this post):</p>
<p><img alt="plot of chunk unnamed-chunk-5" src="https://i2.wp.com/www.alexejgossmann.com/images/2017-6-5-Freedmans_paradox/unnamed-chunk-5-1.png?w=456" data-recalc-dims="1"></p>
<p>Well, the R squared statistic shows a moderate change between models with or without data reuse (average of 0.3093018 vs. 0.5001641). The F test statistic however grows immensely to an average of 3.2806118 (from 1.0480097), and the p-values fall after data reuse to an average of 0.0112216 (from 0.5017696), below the widely used (but arbitrary) 5% significance level.</p>
<p>Obviously the model with data reuse is highly misleading here, because in fact there are absolutely no relationships between the predictor variables and the response (as per the data generation procedure).</p>
<p>In fact, Freedman derived asymptotic estimates for the magnitudes of change in the considered model statistics, and they indeed match the above observations. However I’m too lazy to summarize them here. So I refer to <a target="_blank" rel="nofollow" href="http://www.public.asu.edu/~gasweete/crj604/readings/1983-Freedman%20(Screening%20Regression%20Equations).pdf">the primary source</a>.</p>
<hr>
<p>This code generates the above figure:</p>
<figure>
<pre><code data-lang="r"><span>model_statistics</span><span> </span><span>%>%</span><span>
</span><span>select</span><span>(</span><span>r.squared</span><span>,</span><span> </span><span>p.value</span><span>,</span><span> </span><span>statistic</span><span>,</span><span> </span><span>repetition</span><span>,</span><span> </span><span>data_reuse</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>data_reuse</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>data_reuse</span><span>,</span><span> </span><span>&quot;With Data Reuse&quot;</span><span>,</span><span> </span><span>&quot;Without Data Reuse&quot;</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>data_reuse</span><span> </span><span>=</span><span> </span><span>factor</span><span>(</span><span>data_reuse</span><span>,</span><span> </span><span>levels</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;Without Data Reuse&quot;</span><span>,</span><span> </span><span>&quot;With Data Reuse&quot;</span><span>),</span><span>
</span><span>ordered</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>rename</span><span>(</span><span>&quot;F-statistic&quot;</span><span> </span><span>=</span><span> </span><span>statistic</span><span>,</span><span> </span><span>&quot;p-value&quot;</span><span> </span><span>=</span><span> </span><span>p.value</span><span>,</span><span> </span><span>&quot;R squared&quot;</span><span> </span><span>=</span><span> </span><span>r.squared</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>gather</span><span>(</span><span>stat</span><span>,</span><span> </span><span>value</span><span>,</span><span> </span><span>-</span><span>repetition</span><span>,</span><span> </span><span>-</span><span>data_reuse</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>stat</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>value</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_violin</span><span>(</span><span>aes</span><span>(</span><span>fill</span><span> </span><span>=</span><span> </span><span>stat</span><span>),</span><span> </span><span>scale</span><span> </span><span>=</span><span> </span><span>&quot;width&quot;</span><span>,</span><span> </span><span>draw_quantiles</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>0.25</span><span>,</span><span> </span><span>0.5</span><span>,</span><span> </span><span>0.75</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_hline</span><span>(</span><span>yintercept</span><span> </span><span>=</span><span> </span><span>0.05</span><span>,</span><span> </span><span>linetype</span><span> </span><span>=</span><span> </span><span>2</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>0.3</span><span>)</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span>data_reuse</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme_linedraw</span><span>()</span><span> </span><span>+</span><span>
</span><span>scale_y_continuous</span><span>(</span><span>breaks</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>0.05</span><span>,</span><span> </span><span>2</span><span>,</span><span> </span><span>4</span><span>,</span><span> </span><span>6</span><span>))</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>paste</span><span>(</span><span>n_rep</span><span>,</span><span> </span><span>&quot;repetitions of an LM fit with&quot;</span><span>,</span><span> </span><span>n_row</span><span>,</span><span> </span><span>&quot;rows,&quot;</span><span>,</span><span> </span><span>n_col</span><span>,</span><span> </span><span>&quot;columns&quot;</span><span>))</span></code></pre>
</figure>
	<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://www.alexejgossmann.com/Freedmans_paradox/"> Alexej's 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/5zEiZ1KScJU"></a></div>