<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/very-non-standard-calling-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.win-vector.com/blog/2018/12/very-non-standard-calling-in-r/"> R – Win-Vector Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Our group has done a <em>lot</em> of work with non-standard calling conventions in <a rel="nofollow" href="https://www.r-project.org/" target="_blank"><code>R</code></a>.</p>
<p>Our tools work hard to <em>eliminate</em> non-standard calling (as is the purpose of <a rel="nofollow" href="https://cran.r-project.org/web/packages/wrapr/vignettes/let.html" target="_blank"><code>wrapr::let()</code></a>), or at least make it cleaner and more controllable (as is done in the <a rel="nofollow" href="https://journal.r-project.org/archive/2018/RJ-2018-042/index.html" target="_blank">wrapr dot pipe</a>). And even so, we <em>still</em> get surprised by some of the side-effects and mal-consequences of the over-use of non-standard calling conventions in <code>R</code>.</p>
<p>Please read on for a recent example.</p>
<p><span></span></p>
<p>
<p>Consider the following calls to <code>stats::lm()</code>. And notice the third example fails (throws an error).</p>
<pre>
# works
lm(&quot;y ~ x&quot;, 
   data = data.frame(
     x=1:5, 
     y = c(1, 1, 2, 2, 2)), 
   weights = numeric(5)+1)
#> 
#> Call:
#> lm(formula = &quot;y ~ x&quot;, data = data.frame(x = 1:5, y = c(1, 1, 
#>     2, 2, 2)), weights = numeric(5) + 1)
#> 
#> Coefficients:
#> (Intercept)            x  
#>         0.7          0.3


# works
f1 &lt;- function(w = NULL) {
  lm(as.formula(&quot;y ~ x&quot;), 
     data = data.frame(
       x=1:5, 
       y = c(1, 1, 2, 2, 2)), 
     weights = w)
}
f1(numeric(5)+1)
#> 
#> Call:
#> lm(formula = as.formula(&quot;y ~ x&quot;), data = data.frame(x = 1:5, 
#>     y = c(1, 1, 2, 2, 2)), weights = w)
#> 
#> Coefficients:
#> (Intercept)            x  
#>         0.7          0.3


# fails
f2 &lt;- function(w = NULL) {
  lm(&quot;y ~ x&quot;, 
     data = data.frame(
       x=1:5, 
       y = c(1, 1, 2, 2, 2)), 
     weights = w)
}
f2(numeric(5)+1)
#> Error in eval(extras, data, env): object 'w' not found
</pre>
<p>According the <code>stats::lm()</code> documentation (<code>help(lm)</code>) the first argument must be:</p>
<blockquote>
<p>
an object of class “formula” (or one that can be coerced to that class): a symbolic description of the model to be fitted. The details of model specification are given under ‘Details’.
</p>
</blockquote>
<p>A string appears to be coerce-able into a formula, so all three examples should work. However, typing “<code>print(lm)</code>” reveals the issue: <code>stats::lm()</code> doesn’t take the “<code>weights</code>” argument in a standard way (as the value of a function parameter). It instead grabs it through a sequence of <code>match.call()</code> and <code>eval()</code> steps. It is a complicated way to get the value, which works until it does not work. Somehow passing in the formula as a string interferes with how the value of <code>weights</code> is found. I think we can now see the benefits of isolation and independence of concerns in code.</p>
<p>This over-use of direct environment copying and manipulation is what leads to a great many data-leaks in <code>stats::lm()</code> and <code>stats::glm()</code>. This is in addition to their weird habit of keeping a copy of all of the training data (which loses quite a few of the merits of these methods). <a rel="nofollow" href="http://www.win-vector.com/blog/2014/05/trimming-the-fat-from-glm-models-in-r/" target="_blank">Our group dealt with these issues a long time ago</a>, so we are somewhat familiar with <code>stats::lm()</code> and <code>stats::glm()</code>.</p>
<p>Of course, one could (as the <code>stats::lm()</code> documentation mentions) call <code>stats::lm.fit()</code>. However, <code>stats::lm.fit()</code> does not seem to accept weights and its own documentation (<code>help(lm.fit)</code>) starts ominously:</p>
<blockquote>
<p>
These are the basic computing engines called by lm used to fit linear models. These should usually not be used directly unless by experienced users.
</p>
</blockquote>
<p>Having just finished teaching a four day intensive course covering data science in Python, I can’t help but remark that users of <code>sklearn.linear_model.LinearRegression()</code> don’t need to worry about issues such as the above. Some of the notational flair of <code>R</code> comes at the cost of significant opportunities for user confusion.</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="http://www.win-vector.com/blog/2018/12/very-non-standard-calling-in-r/"> R – Win-Vector 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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/y6QmB_8bFu8" height="1"></a></div>