# Handling errors using purrr's possibly() and safely()

Errors can lead to problems when using loops for repetitive tasks like fitting many models or simulating data. In this post I show how to use purrr::possibly() and purrr:safely() to handle errors as well as purrr:quietly() for capturing warnings and messages.

# Simulate! Simulate! - Part 4: A binomial generalized linear mixed model

I walk through an example of simulating data from a binomial generalized linear mixed model with a logit link and then exploring estimates of over/underdispersion.

# Controlling legend appearance in ggplot2 with override.aes

Changing the legend appearance without changing the plot appearance can be done using the override.aes argument in guide_legend(). I go through four plotting examples to demonstrate how it can be used.

# Analysis essentials: Using the help page for a function in R

Learning how to get help in R is an essential part of learning R. In this short post I walk through how I use some of the sections of an R help page. I end the post by talking about the argument order of a function and why we can (sometimes!) leave off the argument labels.

# An example of base::split() for looping through groups

The base::split() function allows you to "split" your data into defined groups, returning a list with one element per group. This post goes through an example to show the utility of split() for looping through groups.

# Making a background color gradient in ggplot2

This post demonstrates one way to add a background color gradient in ggplot2 based on a continuous variable and geom_segment().

# Expanding binomial counts to binary 0/1 with purrr::pmap()

In this post I show how binomial count data can be expanded to long form binary 0/1 data. I've used this approach for simulations to explore methods for diagnosing lack of fit due to non-independence of trials in a binomial vs binary analysis.

# More exploratory plots with ggplot2 and purrr: Adding conditional elements

Following up on a previous post, I show how I add conditional elements to automated ggplot2 plots through the use of if() statements within my plotting function.

# Many similar models - Part 2: Automate model fitting with purrr::map() loops

The task of fitting many similar models can be automated by looping through variables. I show an example of fitting the same model for multiple different response variables and then making residual plots for all models prior to extracting model results.

# Many similar models - Part 1: How to make a function for model fitting

In this post I discuss how to construct the formula that can be passed to model fitting functions like lm(). I then demonstrate how to use this within a user-created function in order to streamline the process of fitting many similar models.

# The small multiples plot: how to combine ggplot2 plots with one shared axis

There are a variety of ways to combine ggplot2 plots with a single shared axis, but things can get tricky if you want a lot of control over all plot elements. I show four approaches to make such a plot: using facets and with packages cowplot, egg and patchwork.

# Embedding subplots in ggplot2 graphics

I first learned about embedding many small subplots into a larger plot as a way to visualize large datasets with package ggsubplot. Embedding subplots is still possible in ggplot2 today with the annotation_custom() function. I demonstrate one approach to do this, making many subplots in a loop and then adding them to the larger plot.

# Custom contrasts in emmeans

One of the nice things about emmeans is that you can build custom comparisons for any groups or combinations of groups. I give an example showing how to set these up.

# Getting started with emmeans

Post hoc comparisons are made easy in package emmeans. This post goes through some of the basics for those just getting started with the package.

# Lots of zeros or too many zeros?: Thinking about zero inflation in count data

When working with counts, having many zeros does not necessarily indicate zero inflation. I demonstrate this by simulating data from the negative binomial and generalized Poisson distributions. I then show one way to check if the data has excess zeros compared to the number of zeros expected based on the model.

# How to plot fitted lines with ggplot2

I show a general approach for plotting fitted lines with ggplot2 that works across many different model types.

# Analysis essentials: An example directory structure for an analysis using R

I go through an example of the directory structure I used to organize my data, analysis scripts, and outputs for a recent collaborative analysis I did using R.

# The log-0 problem: analysis strategies and options for choosing c in log(y + c)

Analyzing positive data with 0 values can be challenging, since a direct log transformation isn't possible. I discuss some of the things to consider when deciding on an analysis strategy for such data and then explore the effect of the value of the constant, c, when using log(y + c) as the response variable.

# Getting started simulating data in R: some helpful functions and how to use them

Here is the written version of a talk I gave at the Eugene R Users Group.

# Automating exploratory plots with ggplot2 and purrr

In this post I show an example of how to automate the process of making many exploratory plots in ggplot2 with multiple continuous response and explanatory variables. To loop through both x and y variables involves nested looping. In the latter section of the post I go over options for saving the resulting plots, either together in a single document, separately, or by creating combined plots prior to saving. #### Ariel Muldoon

I currently work as an applied statistician in aviation and aeronautics. In a previous role as a consulting statistician in academia I created and taught R workshops for applied science graduate students who are just getting started in R, where my goal was to make their transition to a programming language as smooth as possible. See these workshop materials at my website.