Useful functions and other code snippets (help yourself)…


Alpha transparency

This function accepts any R colour, or vector of R colours and allows you to set the alpha level by converting them to RGBA HEX codes.

#' @title Change the alpha value of colours
#' @desc Reduce the alpha value (level of transparency) of colours to identify patterns of over-plotting when displaying lots of data points.
#' @source https://magesblog.com/post/2013-04-30-how-to-change-alpha-value-of-colours-in/
#' @param col a vector of colours
#' @param alpha alpha valuses range from 0-1, 0 being fully transparent and 1 being fully opaque.
#' @return a vector of alpha colours (notice longer codes)
rgba <- function(col, alpha = 1){
  # handle input errors
  if(missing(col)){ stop("Please provide a vector of colours.") }
  # create output
  apply(sapply(col, col2rgb)/255, 2, function(x){
    rgb(x[1], x[2], x[3], alpha = alpha)
  })
}

Example usage

# A named colour with alpha transparancy 0.4
rgba("forestgreen",.4)
## forestgreen 
## "#228B2266"
# A vector of numbered colours with alpha transparency 0.9
rgba(c(1:3), .9)
## [1] "#000000E6" "#FF0000E6" "#00CD00E6"
# A vector of mixed type colours with alpha transparency 0.2
rgba(c(1, "steelblue", "#FFBB00", rgb(0.4, 0.2, 0.3)), .2)
##           1   steelblue     #FFBB00     #66334D 
## "#00000033" "#4682B433" "#FFBB0033" "#66334D33"

Pretty F-test tables

We’ve been learning model simplification and this usually means we have a whole bunch of sequential F-test output which needs to be cited. This is a method to merge outputs into one well-formatted table. The process is condensed here so it’s easier to see how the table is made (I’ll add clearer steps for the whole process to the stats section later).

Dependencies

You’ll need {tidyverse} to filter the merged table and {knitr} to output nice tables using kable(). Make sure these packages are installed and their libraries are loaded.

# fit the model (I've got some data already loaded)
fit_all <- lm(TRANSMISSION ~ ELEVATION + SOIL.TEMP + SOIL.MOISTURE + BARE.GRND + RICHNESS, data = transmission) 
# order of removal determined via coefficient estimates
fit_removal1 <- update(fit_all, ~. -BARE.GRND)
fit_removal2 <- update(fit_removal1, ~. -SOIL.TEMP)
fit_removal3 <- update(fit_removal2, ~. -RICHNESS)
fit_removal4 <- update(fit_removal3, ~. -SOIL.MOISTURE)
# sequential F-tests stored as data frames
removal1 <- as.data.frame(anova(fit_all, fit_removal1))
removal2 <- as.data.frame(anova(fit_removal1, fit_removal2))
removal3 <- as.data.frame(anova(fit_removal2, fit_removal3))
removal4 <- as.data.frame(anova(fit_removal3, fit_removal4))
# merge data frames
ftests_table <- rbind(removal1, removal2, removal3, removal4)
# Take out the lines we don't need (check Res.Df for fit_all to keep in the original model at the top despite its Df != -1)
ftests_table <- ftests_table %>% filter(Res.Df == 48 | Df == -1)
# Add descriptive model names as a new column
ftests_table <- add_column(
  ftests_table,
  Model = c(
    "Original Model", 
    "Without BARE.GRND",
    "Without SOIL.TEMP",
    "Without RICHNESS",
    "Without SOIL.MOISTURE"
  ),
  .before = 1 # at the front of the table
)
# finished table
ftests_table
Model Res.Df RSS Df Sum of Sq F Pr(>F)
Original Model 48 2.991032 NA NA NA NA
Without BARE.GRND 49 2.999066 -1 -0.0080341 0.1289316 0.7211171
Without SOIL.TEMP 50 3.006920 -1 -0.0078534 0.1283118 0.7217275
Without RICHNESS 51 3.037438 -1 -0.0305188 0.5074761 0.4795447
Without SOIL.MOISTURE 52 3.351841 -1 -0.3144022 5.2789593 0.0257214