Skip to content

Instantly share code, notes, and snippets.

View skgrange's full-sized avatar

Stuart Grange skgrange

View GitHub Profile
@skgrange
skgrange / linking_pmf_sources_to_op_with_multiple_linear_regression_models.R
Created November 22, 2021 10:12
Example of training multiple linear regression (MLR) models to explain/predict oxidative potential (OP) by particulate matter (PM) sources as identified by positive matrix factorisation (PMF) using simulated observations
# Define the helper functions ---------------------------
# https://stats.stackexchange.com/questions/83826/is-a-weighted-r2-in-robust-linear-model-meaningful-for-goodness-of-fit-analys/87085
r2w <- function(model, observed){
SSe <- sum(model$w * (model$resid) ^ 2)
observed <- model$resid+model$fitted
SSt <- sum((model$w * observed - mean(model$w * observed)) ^ 2)
value <- 1 - SSe / SSt;
return(value)
}
@skgrange
skgrange / multiple_linear_regression_modelling_of_op.R
Last active November 19, 2021 09:24
Example of training multiple linear regression (MLR) models to predict oxidative potential (OP) with other particulate matter (PM) constituents with simulated observations
# Define the helper functions ---------------------------
# https://stats.stackexchange.com/questions/83826/is-a-weighted-r2-in-robust-linear-model-meaningful-for-goodness-of-fit-analys/87085
r2w <- function(model, observed){
SSe <- sum(model$w * (model$resid) ^ 2)
observed <- model$resid+model$fitted
SSt <- sum((model$w * observed - mean(model$w * observed)) ^ 2)
value <- 1 - SSe / SSt;
return(value)
}
@skgrange
skgrange / calculate_ebc_components.R
Last active November 4, 2020 12:28
R function to calculate equivalent black carbon (EBC) components with the aethalometer model.
#' Function to calculate equivalent black carbon (EBC) components with the
#' aethalometer model.
#'
#' @param df Data frame/tibble containing aethalometer absorption observations.
#' See examples for details of format required.
#'
#' @param uv Name of variable/column in \code{df} to be used for the UV
#' absorption.
#'
#' @param ir Name of variable/column in \code{df} to be used for the IR