Formulas for additive linear models can be generated with specified coefficient values and variable names.

genFormula(coefs, vars)

Arguments

coefs

A vector that contains the values of the coefficients. Coefficients can also be defined as character for use with double dot notation. If length(coefs) == length(vars), then no intercept is assumed. Otherwise, an intercept is assumed.

vars

A vector of strings that specify the names of the explanatory variables in the equation.

Value

A string that represents the desired formula

Examples


genFormula(c(.5, 2, 4), c("A", "B", "C"))
#> [1] "0.5 * A + 2 * B + 4 * C"
genFormula(c(.5, 2, 4), c("A", "B"))
#> [1] "0.5 + 2 * A + 4 * B"

genFormula(c(.5, "..x", 4), c("A", "B", "C"))
#> [1] "0.5 * A + ..x * B + 4 * C"
genFormula(c(.5, 2, "..z"), c("A", "B"))
#> [1] "0.5 + 2 * A + ..z * B"

changeX <- c(7, 10)
genFormula(c(.5, 2, changeX[1]), c("A", "B"))
#> [1] "0.5 + 2 * A + 7 * B"
genFormula(c(.5, 2, changeX[2]), c("A", "B"))
#> [1] "0.5 + 2 * A + 10 * B"
genFormula(c(.5, 2, changeX[2]), c("A", "B", "C"))
#> [1] "0.5 * A + 2 * B + 10 * C"

newForm <- genFormula(c(-2, 1), c("A"))

def1 <- defData(varname = "A", formula = 0, variance = 3, dist = "normal")
def1 <- defData(def1, varname = "B", formula = newForm, dist = "binary", link = "logit")

set.seed(2001)
dt <- genData(500, def1)
summary(glm(B ~ A, data = dt, family = binomial))
#> 
#> Call:
#> glm(formula = B ~ A, family = binomial, data = dt)
#> 
#> Coefficients:
#>             Estimate Std. Error z value Pr(>|z|)    
#> (Intercept)  -2.3700     0.2046 -11.586   <2e-16 ***
#> A             1.1457     0.1186   9.662   <2e-16 ***
#> ---
#> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#> 
#> (Dispersion parameter for binomial family taken to be 1)
#> 
#>     Null deviance: 483.31  on 499  degrees of freedom
#> Residual deviance: 317.32  on 498  degrees of freedom
#> AIC: 321.32
#> 
#> Number of Fisher Scoring iterations: 6
#>