Provide a formula, variables and a family to generate a linear predictor using the formula and provided variables before using the inverse link of the family to generate the GLM modelled mean, mu, which is then used to simulate the response with this mean from the generating function according to the chosen family.
Usage
glm_data(formula, ..., family = gaussian(), family_args = NULL)Arguments
- formula
an object of class "formula" (or one that can be coerced to that class): a symbolic description of the model to be fitted. The details of model specification are given under ‘Details’ in the glm documentation.
- ...
a
data.framewith columns corresponding to variables used informula, a namedlistof those variables, or individually provided named arguments of variables- family
the
familyof the response. this can be acharacterstring naming a family function, a familyfunctionor the result of acallto a family function- family_args
a named
listwith values of arguments passed to family relevantr<family_name>function for simulating the data
Examples
# Generate a gaussian response from a single covariate
glm_data(Y ~ 1+2*x1,
x1 = rnorm(10))
#> Y x1
#> 1 2.8659599 0.90426912
#> 2 -1.6487121 0.07964921
#> 3 -2.4299142 -1.25882722
#> 4 2.2689910 1.02568511
#> 5 -1.1256621 -0.73077860
#> 6 1.2460188 -0.19014551
#> 7 1.5504812 0.52886469
#> 8 2.3707824 0.55021053
#> 9 2.5668455 0.54968434
#> 10 0.4049102 -0.65954237
# Generate a gaussian response from a single covariate with non-linear
# effects. Specify that the response should have standard deviation sqrt(3)
glm_data(Y ~ 1+2*log(x1),
x1 = runif(10, min = 1, max = 10),
family_args = list(sd = sqrt(3)))
#> Y x1
#> 1 4.904000 7.573046
#> 2 6.511720 4.813340
#> 3 2.369593 3.414977
#> 4 4.763931 7.921432
#> 5 3.492461 6.286120
#> 6 4.643466 9.087110
#> 7 6.407443 8.832921
#> 8 5.130513 4.593756
#> 9 4.336856 9.686072
#> 10 3.039030 5.463919
# Generate a negative binomial response
glm_data(Y ~ 1+2*x1-x2,
x1 = rnorm(10),
x2 = rgamma(10, shape = 2),
family = MASS::negative.binomial(2))
#> Y x1 x2
#> 1 0 -0.9531239 1.3214499
#> 2 4 1.7927561 3.7469426
#> 3 0 0.3489767 1.8461783
#> 4 0 0.2591038 2.0354566
#> 5 0 -0.8059519 0.6438993
#> 6 1 0.1056647 0.7646888
#> 7 0 -0.3335997 0.9691220
#> 8 57 1.6418480 0.6479179
#> 9 1 -0.6439059 0.5980742
#> 10 3 0.5870206 0.8236377
# Provide variables as a list/data.frame and pass a link to the negative.binomial
# function
glm_data(resp ~ 1+2*x1-x2,
data.frame(
x1 = rnorm(10),
x2 = rgamma(10, shape = 2)
),
family = MASS::negative.binomial(2),
family_args = list(link = "identity"))
#> resp x1 x2
#> 1 0 -0.05367151 2.0555178
#> 2 0 -0.56352463 4.8979183
#> 3 0 -0.74390896 0.9440099
#> 4 0 -0.10904165 0.3404743
#> 5 0 -0.56082923 2.1408569
#> 6 0 0.18800155 0.6841673
#> 7 1 0.74885094 1.8483538
#> 8 0 -1.91653832 0.6336780
#> 9 2 0.23609585 0.2305099
#> 10 2 0.62895342 1.4685068