11 September, 2023

Course

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

Prep

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

R

R studio

R studio

  • browser IDE
  • integrates with R, git, bash etc
  1. Rstudio is not R
  2. Avoid installing packages via RStudio
  3. Learn to use Keybindings: Cntl-Alt-K

Packages

  • extend functionality
  • installing
install.packages("tidyverse")
  • loading
library("dplyr")
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union

Packages

  • extend functionality
  • installing
  • loading
library("tidyverse")
## ── Attaching core tidyverse packages ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse 2.0.0 ──
## ✔ forcats   1.0.0     ✔ readr     2.1.4
## ✔ ggplot2   3.4.2     ✔ stringr   1.5.0
## ✔ lubridate 1.9.2     ✔ tibble    3.2.1
## ✔ purrr     1.0.1     ✔ tidyr     1.3.0
## ── Conflicts ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

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
mean
## function (x, ...) 
## UseMethod("mean")
## <bytecode: 0x5654501000d0>
## <environment: namespace:base>
base:::mean.default
## function (x, trim = 0, na.rm = FALSE, ...) 
## {
##     if (!is.numeric(x) && !is.complex(x) && !is.logical(x)) {
##         warning("argument is not numeric or logical: returning NA")
##         return(NA_real_)
##     }
##     if (isTRUE(na.rm)) 
##         x <- x[!is.na(x)]
##     if (!is.numeric(trim) || length(trim) != 1L) 
##         stop("'trim' must be numeric of length one")
##     n <- length(x)
##     if (trim > 0 && n) {
##         if (is.complex(x)) 
##             stop("trimmed means are not defined for complex data")
##         if (anyNA(x)) 
##             return(NA_real_)
##         if (trim >= 0.5) 
##             return(stats::median(x, na.rm = FALSE))
##         lo <- floor(n * trim) + 1
##         hi <- n + 1 - lo
##         x <- sort.int(x, partial = unique(c(lo, hi)))[lo:hi]
##     }
##     .Internal(mean(x))
## }
## <bytecode: 0x565451e348a0>
## <environment: namespace:base>

Reproducible research

Cheat sheets (ref cards)

R revision and updates

  • functions
  • pipes (|>)