R/Stats course

Murray Logan

15 September 2024

Course

https://pcinereus.github.io/SUYRs_documents/

http://www.flutterbys.com.au/stats

http://r4ds.had.co.nz/

Topics

Day Topic
1 Intro to Version control (git), reproducible research (quarto) and setting up R environment
2 Introduction to Bayesian analyses and (generalized) linear models (GLM)
3 Day 2 continued - Bayesian GLM continued
4 Day 3 continued - Bayesian GLM continued
5 Bayesian generalized linear mixed effects models (GLMM)
6 Day 5 continued - Bayesian GLMM
7 Day 6 continued - Bayesian GLMM
8 Bayesian generalized additive models (GAM + GAMM)
9 Regression trees
10 Multivariate analyses

Preparations

  • Show-us-your-Rs
  • R editors
    • Rstudio
  • Version control / Collaboration
    • git/github
  • Scripts and reproducible research
    • knitr
    • lintr/styler
    • Quarto

R

https://github.com/rstudio/cheatsheets/raw/master/base-r.pdf

R studio

https://github.com/rstudio/cheatsheets/raw/master/rstudio-ide.pdf

R studio

  • browser IDE
  • integrates with R, git, bash etc



Important considerations

  1. Rstudio is not R
  2. Avoid installing packages via RStudio
  3. Learn to use Keybindings: Cntl-Shift-K

Packages

  • extend functionality
  • installing from CRAN
install.packages("tidyverse")
  • installing from github
removes::install_github("jmgirard/standist")
  • loading
library("tidyverse")

Packages - Option 1

install.packages("car")         # for regression diagnostics
install.packages("ggfortify")   # for model diagnostics
install.packages("DHARMa")      # for model diagnostics
install.packages("see")         # for model diagnostics
install.packages("lindia")      # for diagnostics of lm and glm
install.packages("broom")       # for consistent, tidy outputs
install.packages("knitr")       # for knitting documents and code
install.packages("glmmTMB")     # for model fitting
install.packages("effects")     # for partial effects plots
install.packages("ggeffects")   # for partial effects plots
install.packages("emmeans")     # for estimating marginal means
install.packages("modelr")      # for auxillary modelling functions
install.packages("performance") # for model diagnostics
install.packages("datawizard")  # for data properties
install.packages("insight")     # for model information
install.packages("sjPlot")      # for outputs

Packages - Option 1

install.packages("report")      # for reporting methods/results
install.packages("easystats")   # framework for stats, modelling and visualisation
install.packages("MuMIn")       # for AIC and model selection 
install.packages("MASS")        # for old modelling routines 
install.packages("patchwork")   # for combining multiple plots together 
install.packages("gam")         # for GAM(M)s 
install.packages("gratia")      # for GAM(M) plots 
install.packages("modelbased")  # for model info
install.packages("broom.mixed") # for tidy outputs from mixed models

Packages - Option 2

install.packages("car")         # for regression diagnostics
install.packages("ggfortify")   # for model diagnostics
install.packages("DHARMa")      # for model diagnostics
install.packages("see")         # for model diagnostics
install.packages("broom")       # for consistent, tidy outputs
install.packages("knitr")       # for knitting documents and code
install.packages("glmmTMB")     # for model fitting
install.packages("effects")     # for partial effects plots
install.packages("ggeffects")   # for partial effects plots
install.packages("emmeans")     # for estimating marginal means
install.packages("modelr")      # for auxillary modelling functions
install.packages("performance") # for model diagnostics
install.packages("datawizard")  # for data properties
install.packages("insight")     # for model information
install.packages("sjPlot")      # for outputs

Packages - Option 2

install.packages("report")      # for reporting methods/results
install.packages("easystats")   # framework for stats, modelling and visualisation
install.packages("patchwork")   # for combining multiple plots together 
install.packages("modelbased")  # for model info
install.packages("broom.mixed") # for tidy outputs from mixed models
install.packages("tidybayes")   # for tidy outputs from mixed models

and then there is cmdstan or rstan and brms….

Packages

  • namespaces
stats::filter()
dplyr::filter()
  • polymorphism (functional overloading)
mean
function (x, ...) 
UseMethod("mean")
<bytecode: 0x55a3716664e0>
<environment: namespace:base>

base:::mean.default

Reproducible research

  • install knitr and quarto packages
  • Quarto - modification of R markdown

https://github.com/rstudio/cheatsheets/raw/master/rmarkdown-2.0.pdf

Cheat sheets (ref cards)

https://www.rstudio.com/resources/cheatsheets/

R revision and updates

  • functions
  • pipes (|>)