<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/options-for-teaching-r-to-beginners-a-false-dichotomy/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://feedproxy.google.com/~r/SASandR/~3/CtuQkfO_En4/options-for-teaching-r-to-beginners.html"> SAS and R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I’ve been reading David Robinson’s excellent blog entry “Teach the tidyverse to beginners” (<a rel="nofollow" href="http://varianceexplained.org/r/teach-tidyverse" target="_blank">http://varianceexplained.org/r/teach-tidyverse</a>), which argues that a tidyverse approach is the best way to teach beginners.  He summarizes two competing curricula:</p>
<p>1) “Base R first”: teach syntax such as $ and [[]], built in functions like <span>ave()</span> and <span>tapply(),</span> and use base graphics</p>
<p>2) “Tidyverse first”: start from scratch with pipes (<span>%>%)</span> and leverage <span>dplyr</span> and use <span>ggplot2</span> for graphics</p>
<p>If I had to choose one of these approaches, I’d also go with 2) (“Tidyverse first”), since it helps to move us closer to helping our students “think with data” using more powerful tools (see <a rel="nofollow" href="http://amstat.tandfonline.com/doi/full/10.1080/00031305.2015.1094283" target="_blank">here</a> for my sermon on this topic). </p>
<p>
<h3>A third way</h3>
<p>Of course, there’s a third option that addresses David’s imperative to “get students doing powerful things quickly”.  The <a rel="nofollow" href="https://cran.r-project.org/web/packages/mosaic" target="_blank">mosaic package</a> was written to make R easier to use in introductory statistics courses.  The package is part of Project MOSAIC (http://mosaic-web.org), an NSF-funded initiative to integrate statistics, modeling, and computing. A paper outlining the mosaic package’s “Less Volume, More Creativity” approach was recently published in the R Journal (<a rel="nofollow" href="https://journal.r-project.org/archive/2017/RJ-2017-024" target="_blank">https://journal.r-project.org/archive/2017/RJ-2017-024</a>). To his credit, David mentions the mosaic package in a response to one of the comments on his blog.</p>
<h3>Less Volume, More Creativity</h3>
<p>One of the big ideas in the mosaic package is that students build on the existing formula interface in R as a mechanism to calculate summary statistics, generate graphical displays, and fit regression models. Randy Pruim has dubbed this approach “Less Volume, More Creativity”.</p>
<p>While teaching this formula interface involves adding a new learning outcome (what is “<span>Y ~ X</span>“?), the mosaic approach simplifies calculation of summary statistics by groups and the generation of two or three dimensional displays on day one of an introductory statistics course (see for example Wang et al., <a rel="nofollow" href="http://escholarship.org/uc/item/84v3774z" target="_blank">“Data Viz on Day One: bringing big ideas into intro stats early and often”</a> (2017), TISE). </p>
<p>The formula interface also prepares students for more complicated models in R (e.g., logistic regression, classification).</p>
<p>Here’s a simple example using the <span>diamonds</span> data from the <span>ggplot2</span> package.  We model the relationships between two colors (D and J), number of carats, and price. </p>
<p>I’ll begin with a bit of data wrangling to generate an analytic dataset with just those two colors. (Early in a course I would either hide the next code chunk or make the <span>recoded</span> dataframe accessible to the students to avoid cognitive overload.)  Note that an R Markdown file with the following commands is available for download at <a rel="nofollow" href="https://nhorton.people.amherst.edu/mosaic-blog.Rmd" target="_blank">https://nhorton.people.amherst.edu/mosaic-blog.Rmd</a>.</p>
<p><span>recoded &lt;-diamonds </span><span>%>%</span><br><span>  filter(color==”D” | color==”J”) %>%</span><br><span>  mutate(col = as.character(color))</span></p>
<p>We first calculate the mean price (in US$) for each of the two colors.</p>
<p><span>mean(price ~ col, data = recoded)</span>
<pre data-ordinal="1">   D    J <br>3170 5324 </pre>
<p>This call is an example of how the formula interface facilitates calculation of a variable’s mean for each of the levels of another variable. We see that D color diamonds tend to cost less than J color diamonds.</p>
<p>A useful function in <span>mosaic</span> is <span>favstats()</span> which provides a useful set of summary statistics (including sample size and missing values) by group.</p>
<p><span>favstats(price ~ col, data = recoded)</span><br>
<table cellspacing="0">
<thead>
<tr>
<th align="left">
<div>col</div>
<div></div>
</th>
<th align="right">
<div>min</div>
<div></div>
</th>
<th align="right">
<div>Q1</div>
<div></div>
</th>
<th align="right">
<div>median</div>
<div></div>
</th>
<th align="right">
<div>Q3</div>
<div></div>
</th>
<th align="right">
<div>max</div>
<div></div>
</th>
<th align="right">
<div>mean</div>
<div></div>
</th>
<th align="right">
<div>sd</div>
<div></div>
</th>
<th align="right">
<div>n</div>
<div></div>
</th>
<th align="right">
<div>missing</div>
<div></div>
</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">D</td>
<td align="right">357</td>
<td align="right">911</td>
<td align="right">1838</td>
<td align="right">4214</td>
<td align="right">18693</td>
<td align="right">3170</td>
<td align="right">3357</td>
<td align="right">6775</td>
<td align="right">0</td>
</tr>
<tr>
<td align="left">J</td>
<td align="right">335</td>
<td align="right">1860</td>
<td align="right">4234</td>
<td align="right">7695</td>
<td align="right">18710</td>
<td align="right">5324</td>
<td align="right">4438</td>
<td align="right">2808</td>
<td align="right">0</td>
</tr>
</tbody>
</table>
<p>A similar command can be used to generate side by side boxplots. Here we illustrate the use of lattice graphics. (An alternative formula based graphics system (<a rel="nofollow" href="https://cran.r-project.org/web/packages/ggformula/index.html" target="_blank">ggformula</a>) will be the focus of a future post.)</p>
<p><span>bwplot(col ~ price, data = recoded)</span>
<div><a rel="nofollow" href="https://i1.wp.com/1.bp.blogspot.com/-EPlm8SN5OD8/WXTpX5e6f_I/AAAAAAAACIU/wzIy8FRuYQsGyPQ7nE_CQGW7f_EbWkIqACLcBGAs/s1600/Rplot05.jpeg?ssl=1" target="_blank"><img border="0" data-original-height="450" src="https://i1.wp.com/1.bp.blogspot.com/-EPlm8SN5OD8/WXTpX5e6f_I/AAAAAAAACIU/wzIy8FRuYQsGyPQ7nE_CQGW7f_EbWkIqACLcBGAs/s320/Rplot05.jpeg?resize=450%2C450&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<div></div>
<p>The distributions are skewed to the right (not surprisingly since they are prices). If we wanted to formally compare these sample means we could do so with a two-sample t-test (or in a similar fashion, by fitting a linear model).</p>
<p><span>t.test(price ~ col, data = recoded)</span><br><span><span>	</span>Welch Two Sample t-test</span><br><span><br></span><span>data:  price by col</span><br><span>t = -20, df = 4000, p-value &lt;2e-16</span><br><span>alternative hypothesis: true difference in means is not equal to 0</span><br><span>95 percent confidence interval:</span><br><span> -2336 -1971</span><br><span>sample estimates:</span><br><span>mean in group D mean in group J </span><br><span>           3170            5324 </span><br><span><br></span><span></span><br><span>msummary(lm(price ~ col, data = recoded))</span><br><span>            Estimate Std. Error t value Pr(>|t|)    </span><br><span>(Intercept)   3170.0       45.0    70.4   &lt;2e-16 ***</span><br><span>colJ          2153.9       83.2    25.9   &lt;2e-16 ***</span><br><span><br></span><span>Residual standard error: 3710 on 9581 degrees of freedom</span><br><span>Multiple R-squared:  0.0654,<span>	</span>Adjusted R-squared:  0.0653 </span><br><span></span><br><span>F-statistic:  670 on 1 and 9581 DF,  p-value: &lt;2e-16</span><br><span><br></span><span>The results from the two approaches are consistent: the group differences are highly statistically significant.  We could conclude that J diamonds tend to cost more than D diamonds, back in the population of all diamonds.</span><br><span><br></span><span>Let’s do a quick review of the mosaic modeling syntax to date:</span><br><span>mean(price ~ col)</span><br><span>bwplot(price ~ col)</span><span></span><span>t.test(price ~ col)</span><span></span><span>lm(price ~ col)</span><span> </span><span>See the pattern? On a statistical note, it’s important to remember that the diamonds were <b>not</b> randomized into colors: this is a found (observational dataset) so there may be other factors at play.  The revised<a rel="nofollow" href="http://www.amstat.org/asa/education/Guidelines-for-Assessment-and-Instruction-in-Statistics-Education-Reports.aspx" target="_blank"> GAISE College report</a> reiterates the importance of multivariate thinking in intro stats.</span><span>Moving to three dimensions</span><span>Let’s continue with the “Less Volume, More Creativity” approach to bring in a third variable: the number of carats in each diamond. </span><span></span><span>xyplot(price ~ carat, groups=col, auto.key=TRUE, type=c(“p”, “r”), data = recoded)</span><span> </span>
<div><a rel="nofollow" href="https://i2.wp.com/4.bp.blogspot.com/-Sc30DmCnZiQ/WXTp6XzHO4I/AAAAAAAACIY/2O9uIfWfWAIQcH_WlRAXbv6_8jIu8Y9fACLcBGAs/s1600/Rplot06.jpeg?ssl=1" target="_blank"><img border="0" data-original-height="450" src="https://i0.wp.com/4.bp.blogspot.com/-Sc30DmCnZiQ/WXTp6XzHO4I/AAAAAAAACIY/2O9uIfWfWAIQcH_WlRAXbv6_8jIu8Y9fACLcBGAs/s320/Rplot06.jpeg?resize=450%2C450&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<p><span>We see that controlling for the number of carats, the D color diamonds tend to sell for more than the J color diamonds.  We can confirm this by fitting a regression model that controls for both variables (and then display the resulting predicted values from this <a rel="nofollow" href="http://mdsr-book.github.io/excerpts/mdsr-regression.pdf" target="_blank">parallel slopes model </a>using </span><span>plotModel()</span><span>).</span><span> </span>
<div><a rel="nofollow" href="https://i1.wp.com/1.bp.blogspot.com/-aTkk7ofDG0s/WXo0DB3OHvI/AAAAAAAACJQ/rdiXvFErxN479bvBETGBPEaLHNxDGaDawCLcBGAs/s1600/Rplot09.jpeg?ssl=1" target="_blank"><img border="0" data-original-height="450" src="https://i1.wp.com/1.bp.blogspot.com/-aTkk7ofDG0s/WXo0DB3OHvI/AAAAAAAACJQ/rdiXvFErxN479bvBETGBPEaLHNxDGaDawCLcBGAs/s320/Rplot09.jpeg?resize=450%2C450&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<div></div>
<div></div>
<p><span><span></span><span>This is a great example of <a rel="nofollow" href="https://en.wikipedia.org/wiki/Simpson%27s_paradox" target="_blank">Simpson’s paradox</a>: accounting for the number of carats has yielded opposite results from a model that didn’t include carats.</span>If we were to move forward with such an analysis we’d need to be sure to undertake an assessment of our model and verify conditions and assumptions (but for the purpose of the blog entry I’ll defer that).</span><br>
<h3><span><b></b></span></h3>
<h3><span><b>Moving beyond mosaic</b></span></h3>
<p><span>The revised GAISE College report enunciated the importance of technology when teaching statistics. Many courses still use calculators or web-based applets to incorporate technology into their classes. </span><span></span><span>R is an excellent environment for teaching statistics, but many instructors feel uncomfortable using it (particularly if they feel compelled to teach the </span><span>$</span><span> and </span><span>[[]]</span><span> syntax, which many find offputting).  The mosaic approach helps make the use of R feasible for many audiences by keeping things simple. It’s unfortunately true that many introductory statistics courses don’t move beyond bivariate relationships (so students may feel paralyzed about what to do about other factors). The mosaic approach has the advantage that it can bring multivariate thinking, modeling, and exploratory data tools together with a single interface (and modest degree of difficulty in terms of syntax). I’ve been teaching multiple regression as a descriptive method early in an intro stat course for the past ten years (and it helps to get students excited about material that they haven’t seen before). The mosaic approach also scales well: it’s straightforward to teach students dplyr/tidyverse data wrangling by adding in the pipe operator and some key data idioms. (So perhaps the third option should be labeled “mosaic and tidyverse”.)  </span><br><span><br></span><span>See the following for an example of how </span><span>favstats()</span><span> can be replaced by </span><span>dplyr</span><span> idioms. </span><br><span><br></span><span></span><span>recoded %>%</span><br><span> </span><span>  group_by(col) %>%</span><br><span> </span><span>  summarize(meanval = mean(price, na.rm = TRUE))</span><span> </span><br>
<table cellspacing="0">
<thead>
<tr>
<th align="left">
<div>col</div>
<div></div>
</th>
<th align="right">
<div>meanval</div>
<div></div>
</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">D</td>
<td align="right">3170</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td align="left">J</td>
<td align="right">5324</td>
</tr>
</tbody>
</table>
<p><span></span><span>That being said, I suspect that many students (and instructors) will still use </span><span>favstats()</span><span> for simple tasks (e.g., to check sample sizes, check for missing data, etc).  I know that I do.  But the important thing is that unlike training wheels, mosaic doesn’t hold them back when they want to learn new things. I’m a big fan of </span><span>ggplot2</span><span>, but even Hadley </span><a rel="nofollow" href="http://coleoguy.blogspot.com/2015/08/hadley-and-winston-talk-about-future-of.html" target="_blank">agrees</a><span> that the existing syntax is not what he wants it to be.  While it’s not hard to learn to use </span><span>+</span><span> to glue together multiple graphics commands and to get your head around aesthetics, teaching </span><span>ggplot2</span><span> adds several additional learning outcomes to a course that’s already overly pregnant with them.</span><br><span><br></span><span></span><br>
<h3><span>Side note</span></h3>
<p><span>I would argue that a lot of what is in mosaic </span><b>should</b><span> have been in base R (e.g., formula interface to </span><span>mean()</span><span>, </span><span>data=</span><span> option for </span><span>mean()</span><span>).  Other parts are more focused on teaching (e.g., </span><span>plotModel()</span><span>, </span><span>xpnorm()</span><span>, and resampling with the </span><span>do()</span><span> function).</span></p>
<h3><span>Closing thoughts</span></h3>
<p><span>In summary, I argue that the mosaic approach is consistent with the tidyverse. It dovetails nicely with David’s “Teach tidyverse” as an intermediate step that may be more accessible for undergraduate audiences without a strong computing background.  I’d encourage people to check it out (and let Randy, Danny, and I know if there are ways to improve the package).</span><br><span><span><br></span><span>W</span><span>ant to learn more about mosaic?  In addition to the R Journal paper referenced above, you can see how we get students using R quickly in the package’s “Less Volume, More Creativity” and “Minimal R” vignettes.  We also provide curated examples from commonly used textbooks in the “mosaic resources” vignette and a series of <a rel="nofollow" href="https://github.com/ProjectMOSAIC/LittleBooks" target="_blank">freely downloadable and remixable monographs</a> including <i>The Student’s Guide to R</i> and <i>Start Teaching with R.</i></span></span>
<div>
<a rel="nofollow" href="http://feeds.feedburner.com/~ff/SASandR?a=CtuQkfO_En4:UrDPYqO1HFk:yIl2AUoC8zA" target="_blank"><img border="0" src="http://feeds.feedburner.com/~ff/SASandR?d=yIl2AUoC8zA"></a> <a rel="nofollow" href="http://feeds.feedburner.com/~ff/SASandR?a=CtuQkfO_En4:UrDPYqO1HFk:J3aVl1i_38o" target="_blank"><img border="0" src="http://feeds.feedburner.com/~ff/SASandR?d=J3aVl1i_38o"></a> <a rel="nofollow" href="http://feeds.feedburner.com/~ff/SASandR?a=CtuQkfO_En4:UrDPYqO1HFk:qj6IDK7rITs" target="_blank"><img border="0" src="http://feeds.feedburner.com/~ff/SASandR?d=qj6IDK7rITs"></a> <a rel="nofollow" href="http://feeds.feedburner.com/~ff/SASandR?a=CtuQkfO_En4:UrDPYqO1HFk:63t7Ie-LG7Y" target="_blank"><img border="0" src="http://feeds.feedburner.com/~ff/SASandR?d=63t7Ie-LG7Y"></a> <a rel="nofollow" href="http://feeds.feedburner.com/~ff/SASandR?a=CtuQkfO_En4:UrDPYqO1HFk:F7zBnMyn0Lo" target="_blank"><img border="0" src="http://feeds.feedburner.com/~ff/SASandR?i=CtuQkfO_En4:UrDPYqO1HFk:F7zBnMyn0Lo"></a> <a rel="nofollow" href="http://feeds.feedburner.com/~ff/SASandR?a=CtuQkfO_En4:UrDPYqO1HFk:gIN9vFwOqvQ" target="_blank"><img border="0" src="http://feeds.feedburner.com/~ff/SASandR?i=CtuQkfO_En4:UrDPYqO1HFk:gIN9vFwOqvQ"></a>
</div>
<p><img alt="" width="1" src="http://feeds.feedburner.com/~r/SASandR/~4/CtuQkfO_En4" height="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="http://feedproxy.google.com/~r/SASandR/~3/CtuQkfO_En4/options-for-teaching-r-to-beginners.html"> SAS and R</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 alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/1iEvbzUCv3E" height="1"></a></div>