simulate_gls_once: Simulate and Analyze Generalized Least Squares

View source: R/replext_gls.R

simulate_gls_onceR Documentation

Simulate and Analyze Generalized Least Squares

Description

This function simulates data and fits a Generalized Least Squares (GLS) model to the simulated data. It can handle multiple phases, multiple subjects (IDs), and various covariate specifications.

Usage

simulate_gls_once(
  n_timepoints_per_phase,
  rho,
  n_phases = 2,
  n_IDs = 1,
  betas,
  formula,
  covariate_specs = NULL
)

Arguments

n_timepoints_per_phase

Integer. The number of timepoints in each phase.

rho

Numeric. The autocorrelation parameter for the AR(1) error structure.

n_phases

Integer. The number of phases in the design. Default is 2.

n_IDs

Integer. The number of subjects (IDs) to simulate. Default is 1.

betas

Named numeric vector. The true coefficient values for the model.

formula

Formula object. The model formula to be fitted.

covariate_specs

List of lists. Specifications for generating covariates. Each inner list should contain elements 'vars' (variable names), 'dist' (distribution function name), args' (list of distribution parameters), and optionally 'expr' (a function to generate data).

Value

A list containing:

success

Logical. TRUE if the model converged, FALSE otherwise.

estimates

Named numeric vector. Estimated coefficients from the fitted model.

std_errors

Named numeric vector. Standard errors of the estimated coefficients.

p_values

Named numeric vector. P-values for the estimated coefficients.

bias

Named numeric vector. Bias of the estimated coefficients (estimate - true value).

rmse

Numeric. Root Mean Square Error of the estimated coefficients.

Examples

# Simple example with 2 phases, 10 timepoints per phase, and no covariates
result <- simulate_gls_once(
  n_timepoints_per_phase = 10,
  rho = 0.2,
  betas = c("(Intercept)" = 0, "phase1" = 1),
  formula = y ~ phase
)


scdtb documentation built on Sept. 30, 2024, 9:35 a.m.