
Generate Data from a Gamma Regression Model
node_gamma.RdData from the parents is used to generate the node using gamma regression by applying the betas to the design matrix and sampling from the rgamma function.
Arguments
- data
A
data.table(or something that can be coerced to adata.table) containing all columns specified byparents.- parents
A character vector specifying the names of the parents that this particular child node has. If non-linear combinations or interaction effects should be included, the user may specify the
formulaargument instead.- formula
An optional
formulaobject to describe how the node should be generated orNULL(default). If supplied it should start with~, having nothing else on the left hand side. The right hand side may contain any valid formula syntax, such asA + BorA + B + I(A^2), allowing non-linear effects. If this argument is defined, there is no need to define theparentsargument. For example, usingparents=c("A", "B")is equal to usingformula= ~ A + B. Random effects and random slopes are currently not supported here.- betas
A numeric vector with length equal to
parents, specifying the causal beta coefficients used to generate the node.- intercept
A single number specifying the intercept that should be used when generating the node.
- shape
A single positive number specifying the shape parameter (
shapeargument inrgamma). The shape parameter determines the conditional variance of the generated values relative to their conditional mean.- link
The link function used to transform the linear predictor to the conditional mean used in
rgamma. For a standard gamma regression model, this should be set to"log"(which is the default). Other allowed values are"identity"and"inverse".
Details
This function uses the linear predictor defined by the betas and the input design matrix to sample from a subject-specific gamma distribution. It does so by calculating the linear predictor using the data, betas and intercept, applying the inverse of the specified link function to obtain the conditional mean, and passing it to the rgamma function with shape=shape and scale=mu_i/shape of the stats package.
For an observation with conditional mean \(\mu_i\) and shape parameter \(\alpha\), the generated outcome follows a gamma distribution with shape \(\alpha\) and scale \(\mu_i/\alpha\). Consequently, the conditional mean is \(E(Y_i\mid X_i)=\mu_i\) and the conditional variance is \(\mathrm{Var}(Y_i\mid X_i)=\mu_i^2/\alpha\).
Every non-missing conditional mean must be finite and strictly positive. This node type currently does not support inclusion of random effects or random slopes in the formula.
Examples
library(simDAG)
set.seed(124554)
dag <- empty_dag() +
node("age", type="rnorm", mean=50, sd=4) +
node("sex", type="rbernoulli", p=0.5) +
node("cost", type="gamma", shape=3,
formula= ~ 1.5 + age*0.01 + sexTRUE*0.2)
sim_dat <- sim_from_dag(dag=dag, n_sim=100, sort_dag=FALSE)
head(sim_dat)
#> age sex cost
#> <num> <lgcl> <num>
#> 1: 52.81824 FALSE 2.010315
#> 2: 50.86212 FALSE 6.142049
#> 3: 52.92595 TRUE 9.755461
#> 4: 51.01464 FALSE 8.386133
#> 5: 43.14107 FALSE 7.033998
#> 6: 54.15130 TRUE 16.006808