automatically-create-formulas-for-all-possible-models

# I have managed to get the list of all possible models but I haven't found a way of adding the offset term in the models, as shown by the last line that gives an error.


#https://stackoverflow.com/questions/5300595/automatically-create-formulas-for-all-possible-linear-models
regressors <- c("scale_prop_adults_mean", "scale_m_f_adult_ratio_1",
                "scale_clinic_distance_1km", "scale_perc_hiv_mean")

regMat <- expand.grid(c(TRUE,FALSE), c(TRUE,FALSE),
                      c(TRUE,FALSE), c(TRUE,FALSE))
regMat
#>     Var1  Var2  Var3  Var4
#> 1   TRUE  TRUE  TRUE  TRUE
#> 2  FALSE  TRUE  TRUE  TRUE
#> 3   TRUE FALSE  TRUE  TRUE
#> 4  FALSE FALSE  TRUE  TRUE
#> 5   TRUE  TRUE FALSE  TRUE
#> 6  FALSE  TRUE FALSE  TRUE
#> 7   TRUE FALSE FALSE  TRUE
#> 8  FALSE FALSE FALSE  TRUE
#> 9   TRUE  TRUE  TRUE FALSE
#> 10 FALSE  TRUE  TRUE FALSE
#> 11  TRUE FALSE  TRUE FALSE
#> 12 FALSE FALSE  TRUE FALSE
#> 13  TRUE  TRUE FALSE FALSE
#> 14 FALSE  TRUE FALSE FALSE
#> 15  TRUE FALSE FALSE FALSE
#> 16 FALSE FALSE FALSE FALSE

#regMat <- regMat[-(dim(regMat)[1]),]

# let's name the columns
names(regMat) <- regressors


allModelsList <- apply(regMat, 1, function(x) as.formula(
  paste(c("n_prev_tbcases ~ 1", regressors[x]),
        collapse=" + ")) )

#How can I add an offset offset(log(tent_cxr_total)) term without getting an error
#i.e n_prev_tbcases ~ 1 + offset(log(tent_cxr_total))
allModelsList <- apply(regMat, 1, function(x) as.formula(
  paste(c("n_prev_tbcases ~ 1", regressors[x]),"offset(log(tent_cxr_total))",
        collapse=" + ")) )
#> Error in str2lang(x): <text>:1:20: unexpected symbol
#> 1: n_prev_tbcases ~ 1 offset
#>                        ^

Created on 2021-02-23 by the reprex package (v0.3.0)

olsrr::ols_step_all_possible

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.