e_lm_power: Multiple regression power analysis

View source: R/e_lm_power.R

e_lm_powerR Documentation

Multiple regression power analysis

Description

Multiple regression power analysis

Usage

e_lm_power(
  dat = NULL,
  formula_full = NULL,
  formula_red = NULL,
  fit_model_type = c("lm", "lmer")[1],
  n_total,
  n_param_full = NULL,
  n_param_red = NULL,
  sig_level = 0.05,
  weights = NULL,
  sw_print = TRUE,
  sw_plots = TRUE,
  n_plot_ref = NULL
)

Arguments

dat

observed effect size data set

formula_full

observed effect size full model formula, used with dat

formula_red

observed effect size reduced model formula, used with dat

fit_model_type

"lm" or "lmer", used with dat to specify how formulas should be fit

n_total

a total sample size value or list of values, used for power curve

n_param_full

number of parameters in full model, only used if dat is not specified

n_param_red

number of parameters in reduced model, only used if dat is not specified; must be fewer than n_param_full

sig_level

Type-I error rate

weights

observed effect size model fit, if it should be weighted regression

sw_print

print results

sw_plots

create histogram and power curve plots

n_plot_ref

a sample size reference line for the plot; if null, then uses size of data, otherwise uses median of n_total. Histogram is created for first reference value in the list.

Value

list with tables and plots of power analysis results

Examples


# without data, single n
out <-
  e_lm_power(
    dat           = NULL
  , formula_full  = NULL
  , formula_red   = NULL
  , n_total       = 100
  , n_param_full  = 10
  , n_param_red   = 5
  , sig_level     = 0.05
  , weights       = NULL
  , sw_print      = TRUE
  , sw_plots      = TRUE
  , n_plot_ref    = NULL
  )

# without data, sequence of n for power curve
out <-
  e_lm_power(
    dat           = NULL
  , formula_full  = NULL
  , formula_red   = NULL
  , n_total       = seq(20, 300, by = 5)
  , n_param_full  = 10
  , n_param_red   = 5
  , sig_level     = 0.05
  , weights       = NULL
  , sw_print      = TRUE
  , sw_plots      = TRUE
  , n_plot_ref    = NULL
  )


# with data
str(dat_mtcars_e)

yvar      <- "mpg"
xvar_full <- c("cyl", "disp", "hp", "drat", "wt", "qsec")
xvar_red  <- c(               "hp", "drat", "wt", "qsec")

formula_full <-
  stats::as.formula(
    paste0(
      yvar
    , " ~ "
    , paste(
        xvar_full
      , collapse= "+"
      )
    )
  )

formula_red <-
  stats::as.formula(
    paste0(
      yvar
    , " ~ "
    , paste(
        xvar_red
      , collapse= "+"
      )
    )
  )


# with data, single n
out <-
  e_lm_power(
    dat           = dat_mtcars_e
  , formula_full  = formula_full
  , formula_red   = formula_red
  , n_total       = 100
  , n_param_full  = NULL
  , n_param_red   = NULL
  , sig_level     = 0.05
  , weights       = NULL
  , sw_print      = TRUE
  , sw_plots      = TRUE
  , n_plot_ref    = NULL
  )

# with data, sequence of n for power curve, multiple reference sample sizes
out <-
  e_lm_power(
    dat           = dat_mtcars_e
  , formula_full  = formula_full
  , formula_red   = formula_red
  , n_total       = seq(10, 300, by = 5)
  , n_param_full  = NULL
  , n_param_red   = NULL
  , sig_level     = 0.05
  , weights       = NULL
  , sw_print      = TRUE
  , sw_plots      = TRUE
  , n_plot_ref    = c(100, 120, 150)
  )
out$tab_power_ref |> print(width = Inf)

### RMarkdown results reporting
# The results above indicate the following.
#
# 1. With the number of observations
#      $n = `r out[["tab_power"]] |> dplyr::filter(n_total == 100) |> dplyr::pull(n_total)`$
#    and the number of groups
#      $k = `r out[["tab_power"]] |> dplyr::filter(n_total == 100) |> dplyr::pull(n_groups)`$
# 2. Observed (preliminary) power:
#     * `r out[["tab_power"]] |> dplyr::filter(n_total == 100) |>
#            dplyr::pull(obs_power         ) |> signif(digits = 2)`.
# 3. Cohen small, medium, and large power:
#     * `r out[["tab_power"]] |> dplyr::filter(n_total == 100) |>
#            dplyr::pull(Cohen_small_power ) |> signif(digits = 2)`,
#     * `r out[["tab_power"]] |> dplyr::filter(n_total == 100) |>
#            dplyr::pull(Cohen_medium_power) |> signif(digits = 2)`, and
#     * `r out[["tab_power"]] |> dplyr::filter(n_total == 100) |>
#            dplyr::pull(Cohen_large_power ) |> signif(digits = 2)`.

erikerhardt/erikmisc documentation built on April 17, 2025, 10:48 a.m.