Chapter 10: Where to Go from Here and Common Pitfalls
“The journey of a thousand miles begins with one step.” — Lao Tzu
There are many resources that can aid in developing your R
skills from here. We have introduced the basics of R
, helping you take a few steps on your journey of understanding R
. We have focused on the ones that are most important for researchers in the health, behavioral, and social sciences.
Since this has been a primer, we hope that you will continue your learning of R
via the various sources available at little to no cost. Just like this book, many R
books are available online as well as in print. This allows you to explore and learn online at your own pace without having to buy a bunch of books or other resources.
Below, we list a few R
books that we have found to be useful. Most are available free in some form.
- R for Data Science by Hadley Wickham and Garrett Grolemund
- Efficient R Programming by Colin gillespie and Robin Lovelace
- The R Cookbook
- An Introduction to Statistical Learning
- R Markdown: The Definitive Guide
There are many, many books that talk about R
in various forms so by no means is this a complete list.
Common Pitfalls
To end, we wanted to highlight some pitfalls that can plague any beginner to R
. We list a few that we’ve encountered, although others surely exist.
- Document your work.
- Avoid overriding objects unless it is on purpose. Changing objects can be hard to keep track of in bigger projects.
- Ask questions.
R
is very flexible; this can make it overwhelming to learn since there are many ways to perform the same task. However, there are people who have figured out easy ways to do complex stuff and most are willing to answer an email. - Plan out the steps of your data manipulation and analyses. A few minutes of planning can help you not get lost in the technology and lose sight of the goal.
- Understand the statistics before throwing data in a model. This can lead to major problems in science. At the very least, understand the assumptions of the modeling type and when it can and should be used.
- Do exploratory data analysis (EDA) to understand your data.
R
is made for this–so use it. Otherwise, your model may be completely wrong and have many violated assumptions. - Be transparent in your writing. If you use the
R
scripts correctly, you can provide your code as part of any publication. This will greatly increase replicability of our important research findings.
Quiz
As a final note, we thought we would give you a quiz to test your memory of the topics we’ve covered. Don’t worry; no pressure to get them all. We’ve included some tougher ones. Regardless of how well you do, we hope you’ll continue improving in your R
programming skills.
Question 1
What kind of vector is this?
x <- c(10.1, 2.1, 4.6, 2.3, 8.9)
Question 2
What does this line of code do?
df[c(1,5), c("B", "C")]
Question 3
In the tidyverse
there are four join functions. What are they?
Question 4
What functions are used in the “three step summary” as described in Chapter 2?
Question 5
What does the following code do?
ggplot(df, aes(x=C, y=D)) +
geom_boxplot(aes(color = C)) +
theme_bw() +
scale_color_manual(values = c("dodgerblue4", "coral2"))
Question 6
Name three functions you can use to summarize your data in an informative way.
Question 7
What type of model does aov()
perform?
Question 8
What are the differences between aov()
and lm()
?
Question 9
What assumptions of normality and heteroskedasticity fail, what function can be used to fit logistic and poisson regressions?
Question 10
If you were trying to perform logistic regression, what arguments are necessary?
Question 11
In multilevel modeling, which functions can be used to fit a Generalized Estimating Equations model?
Question 12
When comparing mixed effects models, what does anova()
do?
Question 13
Can R
do structural equation modeling? If so, what package(s) are useful?
Question 14
What types of models can glmnet()
perform? How can you do a cross-validated “glmnet” model?
Question 15
Is the following data in wide or long form? How do you know?
## ID measures values
## 1 1 Var_Time1 -1.0858
## 2 2 Var_Time1 1.3165
## 3 3 Var_Time1 -1.7264
## 4 4 Var_Time1 0.9188
## 5 5 Var_Time1 0.3040
## 6 6 Var_Time1 0.5283
## 7 7 Var_Time1 -1.2762
## 8 8 Var_Time1 -0.3861
## 9 9 Var_Time1 -0.8158
## 10 10 Var_Time1 0.9094
## 11 1 Var_Time2 0.6207
## 12 2 Var_Time2 0.8186
## 13 3 Var_Time2 0.3182
## 14 4 Var_Time2 0.3264
## 15 5 Var_Time2 0.1462
## 16 6 Var_Time2 0.8154
## 17 7 Var_Time2 0.5898
## 18 8 Var_Time2 0.1544
## 19 9 Var_Time2 0.3458
## 20 10 Var_Time2 0.9749
To make your data long form but it is currently in wide form, what function(s) can you use?
Question 16
What form of looping is the fastest? What does apply()
do? Can you do for loops in R
?
Question 17
What is the following code doing?
sandwhich <- function(pb, jam){
s <- pb + jam
return(s)
}
Question 18
What is wrong with this chunk of code?
df <- df +
mutate(newvar = ifelse(oldvar == 1, 1, 0))
Question 19
What is your favorite built in theme or how would you make your favorite custom theme?
Question 20
What kind of plot does the following make?
pos = position_dodge(width = .1)
ggplot(summed_data, aes(x = dep2, y = sed, group = asthma, color = asthma)) +
geom_line(position = pos) +
geom_errorbar(aes(ymin = sed - s_se, ymax = sed + s_se),
width = .1,
position = pos)
Goodbye and Good Luck
I hope this has been a useful primer to get you into R
. If you still feel rusty, feel free to go through the book again or look at other online resources. R
is very flexible and can ease the data and analysis burden of research. Implement good practices and your work will become easier to track, easier to document, and easier to communicate. Good luck on your journey using R
in your research!
Step1 <- of_a_journey(you) %>%
has(begun)
You <- now_have_seen(aspects, of, R) %>%
that_can(increase) %>%
productivity(your)
GoodLuck <- journey(on, your)
Begley, C. Glenn, and John P A Ioannidis. 2015. “Reproducibility in science: Improving the standard for basic and preclinical research.” Circulation Research 116 (1): 116–26. https://doi.org/10.1161/CIRCRESAHA.114.303819.
Cumming. 2014. “The new statistics: Why and how.” Psychological Science 25 (1): 7–29. https://doi.org/10.1177/0956797613504966.
Goodman, Steven N, Daniele Fanelli, and John P A Ioannidis. 2016. “What does research reproducibility mean?” Science Translational Medicine 8 (341): 1–6. https://doi.org/10.1126/scitranslmed.aaf5027.
Ioannidis, John P A. 2005. “Why most published research findings are false.” PLoS Medicine 2 (8): 0696–701. https://doi.org/10.1371/journal.pmed.0020124.
Munafò, Marcus R, Brian A Nosek, Dorothy V M Bishop, Katherine S Button, Christopher D Chambers, Nathalie Percie du Sert, Uri Simonsohn, et al. 2017. “A manifesto for reproducible science.” Nature Human Behaviour 1 (January): 1–9. https://doi.org/10.1038/s41562-016-0021.
Taylor, Jonathan, and Robert J Tibshirani. 2015. “Statistical learning and selective inference.” Proceedings of the National Academy of Sciences of the United States of America 112 (25). https://doi.org/10.1073/pnas.1507583112.