This helper function creates a valid formulas
object. The
formulas
object is an argument to the mice
function.
It is a list of formula's that specifies the target variables and
the predictors by means of the standard ~
operator.
Usage
make.formulas(data, blocks = make.blocks(data), predictorMatrix = NULL)
Examples
f1 <- make.formulas(nhanes)
f1
#> $age
#> age ~ bmi + hyp + chl
#> <environment: 0x55b285ceadb8>
#>
#> $bmi
#> bmi ~ age + hyp + chl
#> <environment: 0x55b285ceadb8>
#>
#> $hyp
#> hyp ~ age + bmi + chl
#> <environment: 0x55b285ceadb8>
#>
#> $chl
#> chl ~ age + bmi + hyp
#> <environment: 0x55b285ceadb8>
#>
f2 <- make.formulas(nhanes, blocks = make.blocks(nhanes, "collect"))
f2
#> $collect
#> age + bmi + hyp + chl ~ `0`
#> <environment: 0x55b285b7c380>
#>
# for editing, it may be easier to work with the character vector
c1 <- as.character(f1)
c1
#> [1] "age ~ bmi + hyp + chl" "bmi ~ age + hyp + chl" "hyp ~ age + bmi + chl"
#> [4] "chl ~ age + bmi + hyp"
# fold it back into a formula list
f3 <- name.formulas(lapply(c1, as.formula))
f3
#> $age
#> age ~ bmi + hyp + chl
#> <environment: 0x55b2852019e8>
#>
#> $bmi
#> bmi ~ age + hyp + chl
#> <environment: 0x55b2852019e8>
#>
#> $hyp
#> hyp ~ age + bmi + chl
#> <environment: 0x55b2852019e8>
#>
#> $chl
#> chl ~ age + bmi + hyp
#> <environment: 0x55b2852019e8>
#>