This function generates a model formula using a vector of predictors and a predictor link, and returns a list of models obtained from calling a specified analysis function with the generated formula. Additional arguments can also be passed to the analysis function.
Arguments
- df
data.frame
- dv
(character) vector of outcome variable names
- preds
(character) vector of predictor variable names
- preds_link
link between predictors in model formula (default
+)- ana_fn
analysis function (for example: lm, lavaan::sem)
- ...
additional arguments passed to
ana_fn
Details
The formula is constructed by surrounding each variable with backticks to allow for special characters in variable names. The analysis function receives the formula and the data.frame as first and second (positional) arguments, respectively.
Examples
df |>
get_fmodel(dv="y3", preds=c("x1","x2","x3"), ana_fn=lm) |>
lapply(summary)
#> $y3
#>
#> Call:
#> ana_fn(formula = f, data = df)
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -1.4684 -0.2799 0.0112 0.2821 1.4765
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) -0.03519 0.01858 -1.895 0.0587 .
#> x1 -0.01095 0.04624 -0.237 0.8129
#> x2 0.36611 0.04456 8.215 1.85e-15 ***
#> x3 0.69605 0.03034 22.938 < 2e-16 ***
#> ---
#> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#>
#> Residual standard error: 0.4153 on 496 degrees of freedom
#> Multiple R-squared: 0.8401, Adjusted R-squared: 0.8391
#> F-statistic: 868.7 on 3 and 496 DF, p-value: < 2.2e-16
#>
#>