Skip to contents

Get data for independent two-sample tests when defined by a formula of form y ~ x.

Usage

get_ind_data(data, formula)

Arguments

data

(data.frame)
The data frame containing the formula variables.

formula

(formula)
The formula to be parsed and evaluated on the data.

Value

A named list with components:

  1. data

    A length-2 named list of numeric response vectors, the focal group first and the reference group second, each named by its factor level.

  2. focal_group

    A scalar character giving the focal group name, the second level with observations in the grouping factor.

  3. ref_group

    A scalar character giving the reference group name, the first level with observations in the grouping factor.

  4. parse_formula

    The list returned by parse_formula() applied to formula.

Details

The formula with form y ~ x is defined by

  • y is a numeric vector for the outcome.

  • x is a factor with exactly two observed levels representing the binary group variable.

For factor x, the first observed level will be the reference group. For example, data$x <- factor(data$x, levels = c("pre", "post")) makes pre the reference group and post the focal group. A downstream comparison then takes the form mean(post) - mean(pre).

A non-factor x is converted with as.factor() and a warning is emitted. The reference group is then the smallest value of x. as.factor() orders the levels alphabetically or numerically. Pass x as a factor to control which group is the reference.

A row with a missing value of x is dropped from both groups. Missing values of x are ignored when checking that it has exactly two observed levels. A missing value of y is kept in its group.

Either side of the formula may wrap a term in I(). Operators such as + and * carry special meaning inside a formula. Any other expression, such as log(y) or x > 0, must be wrapped in I() so it is evaluated as ordinary R code against data. On the left hand side, I() transforms the outcome and must yield a numeric result. For example, I(log(y)) ~ x uses the log-transformed outcome. On the right hand side, I() derives the grouping variable. For example, y ~ I(x > 0) groups the outcome by whether x is positive.

Examples

#----------------------------------------------------------------------------
# get_ind_data() example
#----------------------------------------------------------------------------
library(bkmodel)

# A two-arm study comparing an outcome under control and treatment
# control is the first factor level, so it is the reference group
data <- data.frame(
  score = c(5.1, 6.3, 5.8, 7.2, 8.1, 7.6, 8.4, 7.9),
  group = factor(
    rep(c("control", "treatment"), each = 4),
    levels = c("control", "treatment")
  )
)

# Split the outcome into the focal and reference groups
res <- get_ind_data(data = data, formula = score ~ group)
res
#> $data
#> $data$treatment
#> [1] 8.1 7.6 8.4 7.9
#> 
#> $data$control
#> [1] 5.1 6.3 5.8 7.2
#> 
#> 
#> $focal_group
#> [1] "treatment"
#> 
#> $ref_group
#> [1] "control"
#> 
#> $parse_formula
#> $parse_formula$formula
#> score ~ group
#> <environment: 0x5ed28dec1810>
#> 
#> $parse_formula$lhs
#> ~score
#> <environment: 0x5ed28dec1810>
#> 
#> $parse_formula$rhs
#> ~group
#> <environment: 0x5ed28dec1810>
#> 
#> $parse_formula$rhs_group
#> ~group
#> <environment: 0x5ed28dec1810>
#> 
#> $parse_formula$rhs_block
#> NULL
#> 
#> 

# Extract each group with the returned group names
focal <- res$data[[res$focal_group]]
reference <- res$data[[res$ref_group]]

# Estimate the treatment effect as the focal minus reference mean
mean(focal) - mean(reference)
#> [1] 1.9

# The same split feeds an independent two-sample t-test
t.test(focal, reference)
#> 
#> 	Welch Two Sample t-test
#> 
#> data:  focal and reference
#> t = 4.0205, df = 3.8538, p-value = 0.01707
#> alternative hypothesis: true difference in means is not equal to 0
#> 95 percent confidence interval:
#>  0.5680331 3.2319669
#> sample estimates:
#> mean of x mean of y 
#>       8.0       6.1 
#> 

# Wrap the response in I() to compare on the log scale
get_ind_data(data = data, formula = I(log(score)) ~ group)
#> $data
#> $data$treatment
#> [1] 2.091864 2.028148 2.128232 2.066863
#> 
#> $data$control
#> [1] 1.629241 1.840550 1.757858 1.974081
#> 
#> 
#> $focal_group
#> [1] "treatment"
#> 
#> $ref_group
#> [1] "control"
#> 
#> $parse_formula
#> $parse_formula$formula
#> I(log(score)) ~ group
#> <environment: 0x5ed28dec1810>
#> 
#> $parse_formula$lhs
#> ~I(log(score))
#> <environment: 0x5ed28dec1810>
#> 
#> $parse_formula$rhs
#> ~group
#> <environment: 0x5ed28dec1810>
#> 
#> $parse_formula$rhs_group
#> ~group
#> <environment: 0x5ed28dec1810>
#> 
#> $parse_formula$rhs_block
#> NULL
#> 
#>