| f_glm | R Documentation |
glm() functions with diagnostics, assumption checking, and post hoc analysisPerforms Generalized Linear Model (GLM) analysis on a given dataset with options for diagnostics, assumption checking, and post hoc analysis. Several response parameters can be analyzed in sequence and the generated output can be in various formats ('Word', 'pdf', 'Excel').
f_glm(
formula,
family = gaussian(),
data = NULL,
diagnostic_plots = TRUE,
effect_plot = TRUE,
contrast_plots = FALSE,
alpha = 0.05,
adjust = "sidak",
type = "response",
intro_text = TRUE,
dispersion_test = TRUE,
output_type = "default",
save_as = NULL,
save_in_wdir = FALSE,
close_generated_files = FALSE,
open_generated_files = interactive(),
influence_threshold = 2,
...
)
formula |
A formula specifying the model to be fitted. More response variables can be
added using |
family |
The error distribution and link function to be used in the model (default: gaussian()).
This can be a character string naming a family function, a family function or
the result of a call to a family function. (See |
data |
A data frame containing the variables in the model. |
diagnostic_plots |
Logical. If |
effect_plot |
Logical. If |
contrast_plots |
Logical. If |
alpha |
Numeric. Significance level for tests. Default is |
adjust |
Character string specifying the method used to adjust p-values for multiple comparisons. Available methods include:
Default is |
type |
Character string specifying the scale of emmeans post hoc results:
|
intro_text |
Logical. If |
dispersion_test |
Logical. If |
output_type |
Character string specifying the output format. Default is
|
save_as |
Character string specifying the output file path (without extension).
If a full path is provided, output is saved to that location.
If only a filename is given, the file is saved in |
save_in_wdir |
Logical. If |
close_generated_files |
Logical. Closes open Excel or Word (NOT pdf) files before writing, depending on the output format. Works on Windows (taskkill), macOS (pkill) and Linux (pkill/soffice). Default |
open_generated_files |
Logical. Whether to open the generated output
files after creation. Defaults to |
influence_threshold |
Numeric multiplier for the leverage threshold. Observations
with hat values exceeding |
... |
Additional arguments passed to |
The function first checks if all specified variables are present in the data and ensures that the response variable is numeric.
It fits a Generalized Linear Model (GLM) using the specified formula, family, and data. Model diagnostics are performed with DHARMa (simulation-based residual checks including a KS test, dispersion test, and outlier test). High-leverage observations are flagged using hat values.
Significance of each predictor is assessed via Type II Analysis of Deviance (stats::drop1()). If significant effects are found, post hoc pairwise comparisons are performed using estimated marginal means from emmeans() with the chosen p-value adjustment method (default: Sidak). When complete separation is detected, the function falls back to likelihood ratio test (LRT) based pairwise comparisons, which are robust to separation.
Effect and interaction plots. When effect_plot = TRUE, an
estimated marginal means plot (estimate \pm 95% CI on the response
scale, with jittered raw data and compact-letter-display labels) is added
after the post hoc table for each categorical predictor. For a significant
categorical interaction, interaction plots are drawn instead: a two-way
interaction uses the x-axis plus colour (both orientations), while three-
and four-way interactions add facet panels for the remaining factor(s),
with one plot per choice of x-axis factor. Interactions involving five or
more categorical factors are not plotted (a warning is issued); consult the
post hoc cell-means table instead. Estimates follow the type
argument, so for non-gaussian families they are back-transformed to the
response scale. The plots themselves are kept clean for publication (data,
axes, and legend only); the descriptive label and explanatory caption are
emitted as text above and below each figure in the report. All effect and
interaction plots are ggplot2 objects and are stored in the returned
object (e.g. out$y1$effect_plot_treatment,
out$y1$interaction_plot_a_b_1) so they can be retrieved and
customised afterwards. Matches f_aov.
More response variables can be added using + (e.g., response1 + response2 ~ predictor) to fit a sequential GLM for each response variable, captured in one output file.
Outputs can be generated in multiple formats ("pdf", "word", "excel" and "rmd") as specified by output_type. The function also closes any open 'Word' files to avoid conflicts when generating 'Word' documents. If output_type = "rmd" is used it is advised to use it in a chunk with {r, echo=FALSE, results='asis'}
This function requires [Pandoc](https://github.com/jgm/pandoc/releases/tag) (version 1.12.3 or higher), a universal document converter.
Windows: Install Pandoc and ensure the installation folder
(e.g., "C:/Users/your_username/AppData/Local/Pandoc") is added to your system PATH.
macOS: If using Homebrew, Pandoc is typically installed in "/usr/local/bin". Alternatively, download the .pkg installer and verify that the binary's location is in your PATH.
Linux: Install Pandoc through your distribution's package manager (commonly installed in "/usr/bin" or "/usr/local/bin") or manually, and ensure the directory containing Pandoc is in your PATH.
If Pandoc is not found, this function may not work as intended.
An object of class 'f_glm' (a named list, one entry per response variable) containing:
The fitted glm object.
Output of summary(glm_fit).
Type II Analysis of Deviance table from stats::drop1().
DHARMa residual checks and hat-value based leverage diagnostics.
Estimated marginal means, pairwise comparisons, CLD letters, and summary table.
Logical indicating whether complete separation was detected.
McFadden's Pseudo-R^2.
Using the option output_type, it can also generate output in the form of: R Markdown code, 'Word', 'pdf', or 'Excel' files. Includes print and plot methods for 'f_glm' objects.
Sander H. van Delden plantmind@proton.me
# GLM Binomial example with output to console
mtcars_mod <- mtcars
mtcars_mod$cyl <- as.factor(mtcars_mod$cyl)
glm_bin <- f_glm(vs ~ cyl,
family = binomial,
data = mtcars_mod,
output_type = "default")
print(glm_bin)
# GLM Binomial example with output to MS Word file
glm_bin_word <- f_glm(vs ~ cyl,
family = binomial,
data = mtcars_mod,
output_type = "word"
)
# GLM Poisson example with output to rmd text
data(warpbreaks)
glm_pos <- f_glm(breaks ~ wool + tension,
data = warpbreaks,
family = poisson(link = "log"),
intro_text = FALSE,
output_type = "rmd")
cat(glm_pos$rmd)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.