Skip to content

Commit 366e689

Browse files
committed
A start to better documentation
1 parent 5456878 commit 366e689

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+833
-39
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ Imports: compositions,
1313
robCompositions,
1414
testthat,
1515
ggplot2
16-
RoxygenNote: 7.1.0
16+
RoxygenNote: 7.1.1

R/append_ilr_coords.R

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,29 @@
1+
#' Add ILR coordinates to a data.frame containing composition variables
2+
#'
3+
#' @description Add ILR coordinates to a data.frame containing composition variables
4+
#' @param dataf data.frame containing composition variables
5+
#' @param comps character vector of composition variable names in dataf
6+
#' @param psi ilrBase passed to \code{compositions::ilr()}
7+
#' @export
8+
#'
9+
#' @examples
10+
#' library(deltacomp)
11+
#' data(fat_data)
12+
#' head(fat_data)
13+
#' comp_vars <- c("sl", "sb", "lpa", "mvpa")
14+
#' # create sequential binary partition
15+
#' sbp <- create_seq_bin_part(length(comp_vars))
16+
#' # The orthonormal transformation matrix
17+
#' psi <- compositions::gsi.buildilrBase(sbp)
18+
#' head(append_ilr_coords(
19+
#' fat_data,
20+
#' comp_vars,
21+
#' psi
22+
#' ))
23+
#'
24+
#'
25+
#'
26+
127

228
append_ilr_coords <- function(dataf, comps, psi) {
329

R/check_input_args.R

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,30 @@
11

2+
#' Sanity checks for arguments passed to predict_delta_comps()
3+
#'
4+
#' @description Sanity checks for arguments passed to predict_delta_comps()
5+
#'
6+
#' @param dataf A \code{data.frame} containing data
7+
#' @param y Name (as string/character vector of length 1) of outcome variable in \code{dataf}
8+
#' @param comps Character vector of names of compositions in \code{dataf}. See details for more information.
9+
#' @param covars Character vector of covariates names (non-comp variables) in \code{dataf} or NULL for none (default).
10+
#' @param deltas A vector of time-component changes (as proportions of compositions , i.e., values between -1 and 1). Optional.
11+
#' @export
12+
#' @details
13+
#' Throws errors for any problematic input. Returns \code{TRUE} invisibly if no issues found.
14+
#' @examples
15+
#' check_input_args(
16+
#' dataf = fat_data,
17+
#' y = "fat",
18+
#' comps = c("sl", "sb", "lpa", "mvpa"),
19+
#' covars = NULL,
20+
#' deltas = seq(-60, 60, by = 5) / (24 * 60)
21+
#' )
22+
#'
23+
#'
24+
25+
26+
27+
228
check_input_args <- function(dataf, y, comps, covars, deltas) {
329

430

R/check_strictly_positive_vals.R

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
#' Check if compositional variable are strictly greater than 0
2+
#'
3+
#' @param dataf data.frame containing composition variables
4+
#' @param comps character vector of composition variable names in dataf
5+
#' @param tol a numeric value that compositional values are expected to be greater or equal than. 1e-6 is deafult
6+
#'
7+
#' @return
8+
#' If any compositional values are found to be strictly less than \code{tol} and erro is thrown.
9+
#' Returns \code{TRUE} invisibly otherwise.
10+
#' @export
11+
#'
12+
#' @examples
13+
#' data(fat_data)
14+
#' check_strictly_positive_vals(fat_data, c("lpa", "sl"), tol = 1e-6)
15+
#' # not run (throws error):
16+
#' # check_strictly_positive_vals(data.frame(a = rnorm(10)), "a", tol = 1e-6)
117
check_strictly_positive_vals <- function(dataf, comps, tol = 1e-6) {
218

319
vals <- dataf[, comps]

R/cols_exist.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
#' Check whether columns exist in a data.frame
2+
#'
3+
#' @param dataf a data.frame
4+
#' @param cols character vector of columns to be checked in \code{dataf}
5+
#'
6+
#' @return
7+
#' An error if all \code{cols} not present in \code{dataf}.
8+
#' Returns \code{TRUE} invisibly otherwise.
9+
#' @export
10+
#'
11+
#' @examples
12+
#' data(fat_data)
13+
#' cols_exist(fat_data, c("lpa", "sl"))
14+
#' # not run (throws error):
15+
#' # cols_exist(fat_data, "a")
116
cols_exist <- function(dataf, cols) {
217

318
if (is_null_or_na(cols)) {

R/create_comparison_matrix.R

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,45 @@
11

22

3-
# comparisons = "one-v-all"
4-
# comparisons = "prop-realloc"
5-
# comparisons = "one-v-one"
63

4+
#' Creates row-wise perturbations of compositions from the mean composition
5+
#'
6+
#' @param comparisons currently two choices: \code{"one-v-one"} or \code{"prop-realloc"} (default).
7+
#' @param comps the names (character vector) of the compositional variables
8+
#' @param mean_comps the mean composition of \code{comps}
9+
#' @details
10+
#' \code{comparisons = "one-v-one"} creates a matrix with \code{length(comps)} columns and \code{length(comps) * (length(comps) - 1)} rows.
11+
#' The rows contain all pairs of variables with 1 and -1 values.
12+
#'
13+
#' \code{comparisons = "prop-realloc"} creates a matrix with \code{length(comps)} columns and \code{length(comps)} rows.
14+
#' Each rows contains a 1 value for a compositional variable and the remaining values sum to -1 proportional to the \code{mean_comps} value for those variables.
15+
#'
16+
#' Note that for both \code{comparisons} options the net change is 0 (each row sums to 0).
17+
#' @export
18+
#'
19+
#' @examples
20+
#' create_comparison_matrix("one-v-one", LETTERS[1:3], c(0.5, 0.3, 0.2))
21+
#' create_comparison_matrix("prop-realloc", LETTERS[1:3], c(0.5, 0.3, 0.2))
722
create_comparison_matrix <- function(comparisons, comps, mean_comps) {
823

924
K <- poss_comps0 <- NULL
1025
n_comp <- length(comps)
1126

12-
if (comparisons == "one-v-all") {
27+
### depreciated
28+
# if (comparisons == "one-v-all") {
29+
#
30+
# # number of combinations is:
31+
# # K = n_comps as only one combination per composition
32+
# K <- n_comp
33+
# poss_comps0 <- matrix(0, nrow = K, ncol = n_comp, dimnames = list(NULL, comps))
34+
#
35+
# for (k in 1:K) {
36+
# poss_comps0[k, k] <- 1
37+
# poss_comps0[k, -k] <- -1 / (K - 1) # equal distribution of allocation to other comps
38+
# }
39+
#
40+
# } else
1341

14-
# number of combinations is:
15-
# K = n_comps as only one combination per composition
16-
K <- n_comp
17-
poss_comps0 <- matrix(0, nrow = K, ncol = n_comp, dimnames = list(NULL, comps))
18-
19-
for (k in 1:K) {
20-
poss_comps0[k, k] <- 1
21-
poss_comps0[k, -k] <- -1 / (K - 1) # equal distribution of allocation to other comps
22-
}
23-
24-
} else if (comparisons == "prop-realloc") {
42+
if (comparisons == "prop-realloc") {
2543

2644
### same as "one-vs-all" except the "1 / (K - 1)"s replaced by weighted means to sum to 1
2745
# number of combinations is:
@@ -64,4 +82,4 @@ create_comparison_matrix <- function(comparisons, comps, mean_comps) {
6482

6583
return(poss_comps0)
6684

67-
}
85+
}

R/create_seq_bin_part.R

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
11

22

3+
#' Create a sequential binary partition indication matrix
4+
#'
5+
#' @param n_comp the number of compositional variables to partition
6+
#'
7+
#' @return
8+
#' A \code{n_comp} by \code{n_comp - 1} matrix where each column relates to one partition.
9+
#'
10+
#' The partitions are made so that the numerator (\code{1} values) for the \code{i}th column is in the \code{i}th row.
11+
#' All values below the \code{1} in the column are set to \code{-1} (the denominator).
12+
#'
13+
#' The generated sequential binary partition for 3 comp vars is \code{(1, -1, -1), (0, 1, -1)}.
14+
#'
15+
#' The generated sequential binary partition for 4 comp vars is \code{(1, -1, -1, -1), (0, 1, -1, -1), (0, 0, 1, -1)}.
16+
#'
17+
#' etc
18+
#' @export
19+
#'
20+
#' @examples
21+
#' create_seq_bin_part(3)
22+
#' create_seq_bin_part(4)
23+
#' create_seq_bin_part(10)
324
create_seq_bin_part <- function(n_comp) {
425

5-
# sequential binary partition for 3 comp vars can be
6-
# (1, -1, -1), (0, 1, -1)
7-
# sequential binary partition for 4 comp vars can be
8-
# (1, -1, -1, -1), (0, 1, -1, -1), (0, 0, 1, -1)
9-
# etc
26+
1027
base_zeros <- rep(0, n_comp)
1128

1229
sbp <- matrix(0, nrow = n_comp, ncol = n_comp - 1)
@@ -16,7 +33,7 @@ create_seq_bin_part <- function(n_comp) {
1633
this_col[(j + 1):n_comp] <- -1
1734
sbp[, j] <- this_col
1835
}
19-
# can also do ilrBase(D = n_comp) etc
36+
# alternative ilr bases can be made via ilrBase(D = n_comp) etc
2037

2138
return(sbp)
2239

R/extract_lm_quantities.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
#' Extract critical quantities from a lm object (for confidence interval calculations)
2+
#'
3+
#' @param lm_X a lm object
4+
#' @param alpha level of significance. Defaults to 0.05.
5+
#'
6+
#' @return
7+
#' A list containing the \code{lm}'s model matrix (\code{dmX}),
8+
#' the inverse of \code{t(dmX) %*% dmX} (\code{XtX_inv}),
9+
#' the standard error (\code{s_e}),
10+
#' the estimated single column beta matrix (\code{beta_hat}), and
11+
#' the critical value of the relevant degrees of freedom t-dist (\code{crit_val}).
12+
#' @export
13+
#'
14+
#' @examples
15+
#' data(fat_data)
16+
#' lm_fat <- lm(fat ~ sl, data = fat_data)
17+
#' extract_lm_quantities(lm_fat)
118
extract_lm_quantities <- function(lm_X, alpha = 0.05) {
219

320
# get the design matrix from the LM

R/fairclough.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#' Data from Fairclough (2017). Fitness, fatness and the reallocation of time between childrens daily movement behaviours: an analysis of compositional data
1+
#' Data from Fairclough (2017). Fitness, fatness and the reallocation of time between children's daily movement behaviours: an analysis of compositional data
22
#'
33
#' A dataset containing z_bmi (outcome), time-use compositions (sl,sb,lpa,mvpa), and covariates
44
#' from the Fairclough (2017) paper. The data can be found in supp file 7 of the paper

R/fat_data.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
#' Randomly generated data to simulate child fat percentage regressed on time-use composiitonal data
1+
#' Randomly generated data to simulate child fat percentage regressed on time-use compositional data
22
#'
33
#' A dataset containing fat percentage (outcome), time-use compositions (sl,sb,lpa,mvpa), and covariates
4-
#' (sibs,parents,ed). Note sl+sb+lpa+mvpa=1440minutes for each subject. The variables are as follows:
4+
#' (sibs,parents,ed). Note sl+sb+lpa+mvpa=1440 minutes for each subject. The variables are as follows:
55
#'
66
#' \itemize{
77
#' \item fat. child fat percentage (11.29--29.99)
88
#' \item sl. daily sleep in minutes (283--765)
99
#' \item sb. sedentary behaviour in minutes (354--789)
1010
#' \item lpa. low-intensity physical activity in minutes (157--507)
11-
#' \item mvpa. moderate- to vigourous-intensity physical activity in minutes (35--155)
11+
#' \item mvpa. moderate- to vigorous-intensity physical activity in minutes (35--155)
1212
#' \item sibs. number of siblings (0,1,2,3,4)
1313
#' \item parents. number of parents/caregivers at home (1,2)
1414
#' \item ed. education level of parent(s) (0=high school, 1=diploma, 2=degree)

0 commit comments

Comments
 (0)