### Install interactions Package Source: https://github.com/jacob-long/interactions/blob/master/README.md Install the interactions package from CRAN. This is the first step before using any of its functions. ```r install.packages("interactions") ``` -------------------------------- ### Install and Load Interactions Package Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/00-START-HERE.md Installs the interactions package from CRAN and loads it into the R session. This is the first step before using any functions from the package. ```r install.packages("interactions") library(interactions) ``` -------------------------------- ### Install Interactions Package Development Version Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/README.md Install the latest development version of the interactions package directly from GitHub using the devtools package. ```r devtools::install_github("jacob-long/interactions") ``` -------------------------------- ### Get Help for Functions and Vignettes in R Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/INDEX.md Use the '?' operator to access help for specific functions like sim_slopes and interact_plot. To explore detailed examples and workflows, use vignette() and browseVignettes() for the 'interactions' package. ```r # Help for a function ?sim_slopes ?interact_plot # Vignettes vignette("interactions") # List vignettes browseVignettes("interactions") ``` -------------------------------- ### Probe Interaction with Survey Data Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/api-reference-probe_interaction.md This example demonstrates using probe_interaction with survey-weighted data using the 'survey' package. It shows how to set up the survey design and fit a survey-weighted generalized linear model. ```r library(survey) data(api) dstrat <- svydesign(id = ~1, strata = ~stype, weights = ~pw, data = apistrat, fpc = ~fpc) regmodel <- svyglm(api00 ~ ell * meals, design = dstrat) results <- probe_interaction(regmodel, pred = ell, modx = meals) results ``` -------------------------------- ### Package Documentation File Structure Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/INDEX.md This tree outlines the organization of documentation files within the package, indicating the purpose of each file from the README to specific API references and usage guides. ```bash output/ ├── README.md # Start here ├── INDEX.md # This file ├── api-reference-sim_slopes.md # Function docs ├── api-reference-interact_plot.md ├── api-reference-johnson_neyman.md ├── api-reference-sim_margins.md ├── api-reference-cat_plot.md ├── api-reference-probe_interaction.md ├── parameter-reference.md # Parameter guide ├── s3-methods.md # S3 method docs ├── types.md # Object structures └── usage-guide.md # Examples & workflows ``` -------------------------------- ### Linear Model Example Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/parameter-reference.md Example of fitting a linear model with an interaction term. ```r model <- lm(y ~ x * z, data = data) ``` -------------------------------- ### Install and Load Interactions Package Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/usage-guide.md Installs the interactions package from CRAN and loads it along with ggplot2 and dplyr for data manipulation and plot customization. ```r # Install from CRAN install.packages("interactions") # Load package library(interactions) library(ggplot2) # For plot customization library(dplyr) # For data manipulation ``` -------------------------------- ### Probe Interaction with Custom Moderator Values Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/api-reference-probe_interaction.md This example demonstrates how to specify exact values for the moderator variable (modx) when using probe_interaction. This allows for a more targeted analysis of the interaction effect at specific points. ```r # Specify exact moderator values results <- probe_interaction(fit, pred = hp, modx = wt, modx.values = c(2, 3, 4, 5)) results ``` -------------------------------- ### Survey Model Example Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/parameter-reference.md Example of fitting a survey-weighted generalized linear model with an interaction term. ```r model <- svyglm(y ~ x * z, design = survey_design) ``` -------------------------------- ### Probe Interaction with Factor Moderator Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/api-reference-probe_interaction.md This example demonstrates using probe_interaction when the moderator variable is a factor (categorical). It also includes plotting the points for clarity. ```r fit_cat <- lm(Petal.Length ~ Petal.Width * Species, data = iris) results <- probe_interaction(fit_cat, pred = Petal.Width, modx = Species, plot.points = TRUE) results ``` -------------------------------- ### Simple Slopes for Survey Design Models Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/api-reference-sim_slopes.md Analyze simple slopes for models fitted with survey data using the 'survey' package. This example demonstrates fitting a 'svyglm' model and then applying sim_slopes to analyze the interaction between 'ell' and 'meals'. ```r library(survey) data(api) dstrat <- svydesign(id = ~1, strata = ~stype, weights = ~pw, data = apistrat, fpc = ~fpc) regmodel <- svyglm(api00 ~ ell * meals, design = dstrat) results <- sim_slopes(regmodel, pred = ell, modx = meals) results ``` -------------------------------- ### Probe Interaction with Visualization Options Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/api-reference-probe_interaction.md This example shows how to enhance the probe_interaction plot by adding points, confidence intervals, and visualizing the Johnson-Neyman interval. It requires setting specific arguments like plot.points, interval, and jnplot. ```r # Add points and intervals to plot, plot J-N interval results <- probe_interaction(fit, pred = hp, modx = wt, plot.points = TRUE, interval = TRUE, jnplot = TRUE) results ``` -------------------------------- ### Three-Way Interaction Analysis Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/api-reference-sim_slopes.md Examine simple slopes in a three-way interaction model. This example fits a model predicting 'Income' from 'Frost', 'Murder', and 'Illiteracy' and then uses sim_slopes to analyze the effect of 'Murder' at specific values of 'Illiteracy' and 'Frost'. ```r states <- as.data.frame(state.x77) fit3 <- lm(Income ~ Frost * Murder * Illiteracy, data = states) # Analyze slopes at specific values results <- sim_slopes(fit3, pred = Murder, modx = Illiteracy, mod2 = Frost, modx.values = c(5, 10, 15), mod2.values = c(0, 100)) results ``` -------------------------------- ### Logistic Model Example Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/parameter-reference.md Example of fitting a logistic regression model with an interaction term. ```r model <- glm(y ~ x * z, data = data, family = binomial) ``` -------------------------------- ### Tidying Sim Slopes Results with dplyr Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/INDEX.md This example demonstrates how to convert the results of `sim_slopes` into a tidy data frame using the `tidy` S3 method and then filter and arrange the results using `dplyr`. ```r slopes <- sim_slopes(model, pred = x, modx = z) # Convert to tidy format tidy_slopes <- tidy(slopes) # Use with dplyr tidy_slopes %>% filter(p.value < 0.05) %>% arrange(estimate) ``` -------------------------------- ### Basic Interaction Analysis Workflow Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/usage-guide.md A template for fitting a linear model with an interaction term, analyzing the interaction using sim_slopes, and visualizing it with interact_plot or probe_interaction. ```r # 1. Fit a model with interaction model <- lm(outcome ~ predictor * moderator, data = your_data) # 2. Analyze the interaction analysis <- sim_slopes(model, pred = predictor, modx = moderator) print(analysis) # 3. Visualize the interaction plot <- interact_plot(model, pred = predictor, modx = moderator) print(plot) # Or combine both: combined <- probe_interaction(model, pred = predictor, modx = moderator) print(combined) ``` -------------------------------- ### Export sim_slopes results as a Word document Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/s3-methods.md Demonstrates converting `sim_slopes` results to a `huxtable` and exporting it as a .docx file. Requires `interactions` and `huxtable` packages. Specifies the number of digits for the output. ```r library(interactions) library(huxtable) fit <- lm(mpg ~ hp * wt, data = mtcars) slopes <- sim_slopes(fit, pred = hp, modx = wt, digits = 3) # Export as formatted table table <- as_huxtable(slopes) export_table(table, file = "slopes_table.docx") ``` -------------------------------- ### Basic Two-Way Interaction Analysis and Visualization Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/INDEX.md Demonstrates fitting a linear model and then analyzing and visualizing a basic two-way interaction using sim_slopes and interact_plot. ```R library(interactions) # Fit model model <- lm(mpg ~ hp * wt, data = mtcars) # Analyze sim_slopes(model, pred = hp, modx = wt) # Visualize interact_plot(model, pred = hp, modx = wt) # Or both probe_interaction(model, pred = hp, modx = wt) ``` -------------------------------- ### Extract Number of Observations from sim_slopes Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/s3-methods.md Use `nobs()` to extract the total number of observations used in the simple slopes analysis. This is a direct way to get the sample size for the model. ```r results <- sim_slopes(model, pred = x, modx = z) nobs(results) ``` -------------------------------- ### Probe interaction and visualize simultaneously Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/README.md Use the probe_interaction function to both analyze and visualize interaction effects in a single step. This is a convenient shortcut for common analysis tasks. ```r probe_interaction(fit, pred = x, modx = z) ``` -------------------------------- ### Standardized Predictors for Interpretation Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/api-reference-sim_slopes.md Use standardized predictors in the sim_slopes analysis for easier interpretation of regression coefficients. This example centers and standardizes all variables before analyzing simple slopes, setting the 'digits' argument to control output precision. ```r # Center and standardize for regression interpretation results <- sim_slopes(fiti, pred = hp, modx = wt, centered = "all", digits = 3) results ``` -------------------------------- ### Accessing probe_interaction Components Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/types.md Demonstrates how to access the simslopes table, the full simslopes object, and the interactplot object from a probe_interaction result. Also shows basic plot customization. ```r results <- probe_interaction(model, pred = x, modx = z) # Slopes table slopes_table <- results$simslopes$slopes # Full slopes object slopes_obj <- results$simslopes # Plot object plot_obj <- results$interactplot # Customize plot results$interactplot + theme_minimal() ``` -------------------------------- ### Plot Simple Margins Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/s3-methods.md Provides a basic plot method for sim_margins objects. The implementation is minimal. ```r results <- sim_margins(model, pred = x, modx = z) plot(results) ``` -------------------------------- ### Three-Way Interaction Analysis and Visualization Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/INDEX.md Shows how to fit a linear model for a three-way interaction and analyze/visualize it, including specifying moderator values and faceting the plot. ```R # Model states <- as.data.frame(state.x77) model <- lm(Income ~ Murder * Illiteracy * Frost, data = states) # Analyze at mod2 values slopes <- sim_slopes(model, pred = Murder, modx = Illiteracy, mod2 = Frost, mod2.values = "plus-minus") # Visualize with facetting plot <- interact_plot(model, pred = Murder, modx = Illiteracy, mod2 = Frost) ``` -------------------------------- ### Basic Probe Interaction Usage Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/api-reference-probe_interaction.md This snippet demonstrates the basic usage of probe_interaction to analyze and plot interaction effects from a linear model. It shows how to fit a model, call probe_interaction, and access the separate analysis and plot components. ```r library(interactions) # Fit interaction model fit <- lm(mpg ~ hp * wt, data = mtcars) # Analyze and plot in one call results <- probe_interaction(fit, pred = hp, modx = wt) # Print slopes and display plot results # Extract components separately slopes <- results$simslopes plot <- results$interactplot ``` -------------------------------- ### Combine tidy slopes and margins results Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/s3-methods.md Illustrates combining tidy output from both `sim_slopes` and `sim_margins` using `dplyr::bind_rows`. Requires `interactions`, `dplyr`, and `bind_rows`. ```r library(interactions) library(dplyr) library(bind_rows) fit <- lm(mpg ~ hp * wt, data = mtcars) # Compare slopes vs. margins slopes_tidy <- tidy(sim_slopes(fit, pred = hp, modx = wt)) %>% mutate(method = "slopes") margins_tidy <- tidy(sim_margins(fit, pred = hp, modx = wt)) %>% mutate(method = "margins") combined <- bind_rows(slopes_tidy, margins_tidy) ``` -------------------------------- ### Use Prediction Intervals Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/api-reference-interact_plot.md Illustrates how to display prediction intervals instead of confidence intervals by specifying int.type = "prediction" along with interval = TRUE. ```r # Use prediction intervals instead interact_plot(fit, pred = hp, modx = wt, interval = TRUE, int.type = "prediction") ``` -------------------------------- ### Sensitivity Analysis: Different Centering Options Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/usage-guide.md Examine how different centering strategies ('all' vs. 'none') for predictors affect the estimated conditional intercepts. This helps in understanding the impact of centering on model interpretation. ```r # All variables centered (default) slopes_centered <- sim_slopes(model, pred = x, modx = z, centered = "all") # No centering slopes_none <- sim_slopes(model, pred = x, modx = z, centered = "none") # Compare how centering affects conditional intercepts intercepts_comparison <- data.frame( centered = slopes_centered$ints$Est., none = slopes_none$ints$Est. ) print(intercepts_comparison) ``` -------------------------------- ### Tidy sim_slopes results Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/s3-methods.md Demonstrates tidying the results of `sim_slopes` into a data frame format for further analysis. Requires the `interactions` package. ```r library(interactions) fit <- lm(mpg ~ hp * wt, data = mtcars) slopes <- sim_slopes(fit, pred = hp, modx = wt) # Convert to tidy format for further analysis tidy_slopes <- tidy(slopes) head(tidy_slopes) ``` -------------------------------- ### Simulate Slopes with Original Data Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/README.md When using variable transformations in your formula (e.g., log(x)), provide the original data frame to the `data` argument in `sim_slopes` to ensure correct calculations. ```r sim_slopes(fit, pred = y, modx = x, data = original_data) ``` -------------------------------- ### Basic Margins Analysis with lm Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/api-reference-sim_margins.md Computes marginal effects for a linear model with an interaction term at default moderator values. Requires the 'interactions' library. ```r library(interactions) # Fit interaction model fit <- lm(mpg ~ hp * wt, data = mtcars) # Compute margins at default moderator values results <- sim_margins(fit, pred = hp, modx = wt) results ``` -------------------------------- ### Print Interaction Probe Results Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/s3-methods.md Use this method to display both the simple slopes analysis table and the corresponding interaction plot. It combines tabular and visual summaries of interaction effects. ```r results <- probe_interaction(model, pred = x, modx = z) print(results) ``` -------------------------------- ### Convert sim_margins to huxtable Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/s3-methods.md Converts simple margins results to a formatted huxtable object. Requires the `huxtable` package. The output is a huxtable object with a formatted table. ```r results <- sim_margins(model, pred = x, modx = z) table <- as_huxtable(results) ``` -------------------------------- ### Simulate Marginal Effects Analysis Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/INDEX.md Utilize sim_margins as an alternative to sim_slopes, employing a margins-based approach. It supports simulation and bootstrap standard errors and is beneficial for complex models. ```r margins <- sim_margins(model, pred = x, modx = z) ``` -------------------------------- ### Print Simple Margins Analysis Results Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/s3-methods.md Use this method to display simple margins analysis results in a tabular format. It includes conditional effects, standard errors, confidence intervals, and p-values. ```r results <- sim_margins(model, pred = x, modx = z) print(results) ``` -------------------------------- ### Basic Johnson-Neyman Interval with Plot Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/api-reference-johnson_neyman.md Computes the Johnson-Neyman interval and displays a plot of the results. Requires the 'interactions' package and a fitted linear model. ```r library(interactions) # Fit interaction model states <- as.data.frame(state.x77) states$HSGrad <- states$`HS Grad` fit <- lm(Income ~ HSGrad + Murder * Illiteracy, data = states) # Compute Johnson-Neyman interval jn <- johnson_neyman(fit, pred = Murder, modx = Illiteracy, plot = TRUE) jn ``` -------------------------------- ### Compare Different Models with sim_slopes Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/usage-guide.md Compare the results of different regression models by applying sim_slopes to each and binding the results. This is useful for understanding how model complexity or additional predictors affect estimated slopes. ```r # Model 1: Simple interaction model1 <- lm(y ~ x * z, data = data) slopes1 <- tidy(sim_slopes(model1, pred = x, modx = z)) %>% mutate(model = "Simple Interaction") # Model 2: With additional predictor model2 <- lm(y ~ x * z + w, data = data) slopes2 <- tidy(sim_slopes(model2, pred = x, modx = z)) %>% mutate(model = "With Covariate") # Compare comparison <- bind_rows(slopes1, slopes2) print(comparison) ``` -------------------------------- ### Print Simple Slopes Analysis Results Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/s3-methods.md Use this method to display formatted results from a simple slopes analysis. It shows conditional slopes, intercepts, and Johnson-Neyman intervals if requested. ```r results <- sim_slopes(model, pred = x, modx = z) print(results) # or just results ``` -------------------------------- ### Provide Original Data for Transformations Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/parameter-reference.md When your model formula includes transformations like poly(), sqrt(), or log(), you must provide the original data frame using the 'data' argument. This ensures correct calculations for transformed variables. ```r model <- lm(y ~ poly(x, 2) * z, data = mydata) interact_plot(model, pred = x, modx = z, data = mydata) ``` -------------------------------- ### Bar Chart with Points for Interaction Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/api-reference-cat_plot.md Generates a bar chart for an interaction, with points overlaid and uncertainty intervals shown. This is an alternative visualization despite common criticisms of bar charts for interactions. ```r # Some prefer this despite criticisms p <- cat_plot(fit, pred = color, modx = cut, geom = "bar", plot.points = TRUE, interval = TRUE) print(p) ``` -------------------------------- ### Probe Interaction for Three-Way Interaction Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/api-reference-probe_interaction.md This snippet illustrates how to use probe_interaction to analyze a three-way interaction effect. It requires specifying an additional moderator (mod2) and optionally its values. ```r # Three-way interaction states <- as.data.frame(state.x77) fit3 <- lm(Income ~ Frost * Murder * Illiteracy, data = states) results <- probe_interaction(fit3, pred = Murder, modx = Illiteracy, mod2 = Frost, mod2.values = "plus-minus") results ``` -------------------------------- ### Extracting Results from probe_interaction Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/api-reference-probe_interaction.md Demonstrates how to access the individual components of the results object returned by probe_interaction for further analysis. ```r results <- probe_interaction(fit, pred = x, modx = z) slopes_table <- results$simslopes$slopes plot_object <- results$interactplot ``` -------------------------------- ### Simulate Slopes with Confidence Intervals and Robust Standard Errors Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/parameter-reference.md Use this snippet to simulate simple slopes from a model, displaying confidence intervals instead of standard errors and calculating robust standard errors (HC3). ```r sim_slopes(model, pred = x, modx = z, confint = TRUE, # Show CIs not SEs ci.width = 0.95, # 95% CI robust = "HC3") # Robust SEs ``` -------------------------------- ### Survey Data Analysis with Interactions Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/usage-guide.md Perform interaction analysis on survey data using `svyglm` and the `survey` package. Demonstrates analysis with survey designs. ```r library(survey) data(api) # Create survey design dstrat <- svydesign(id = ~1, strata = ~stype, weights = ~pw, data = apistrat, fpc = ~fpc) # Fit svyglm model <- svyglm(api00 ~ ell * meals, design = dstrat) # Analysis works the same way slopes <- sim_slopes(model, pred = ell, modx = meals) print(slopes) plot <- interact_plot(model, pred = ell, modx = meals) print(plot) ``` -------------------------------- ### Survey Data Interaction Analysis Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/INDEX.md Shows how to analyze an interaction within survey data using svyglm and sim_slopes, accounting for survey design complexities. ```R library(survey) dstrat <- svydesign(id = ~1, strata = ~stype, weights = ~pw, data = apistrat, fpc = ~fpc) model <- svyglm(api00 ~ ell * meals, design = dstrat) sim_slopes(model, pred = ell, modx = meals) ``` -------------------------------- ### Simple Two-Way Interaction with Continuous Variables Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/api-reference-sim_slopes.md Analyze simple slopes for a two-way interaction between continuous predictors. This snippet demonstrates fitting a linear model and then using sim_slopes to examine the effect of 'hp' at different values of 'wt'. The default analysis uses values of 'wt' at mean ± 1 standard deviation. ```r library(interactions) # Fit a model with interaction fiti <- lm(mpg ~ hp * wt, data = mtcars) # Analyze simple slopes at default values (mean ± 1 SD of wt) results <- sim_slopes(fiti, pred = hp, modx = wt) results ``` -------------------------------- ### Analyze interaction effects with sim_slopes Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/README.md Analyze the fitted model to probe the simple slopes of an interaction. Specify the focal predictor and the moderator variable. ```r sim_slopes(fit, pred = x, modx = z) ``` -------------------------------- ### Basic Interaction Plot Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/api-reference-interact_plot.md Generates a basic interaction plot for a linear model. Requires the interactions package and a fitted model object. ```r library(interactions) # Fit model fit <- lm(mpg ~ hp * wt, data = mtcars) # Plot interaction p <- interact_plot(fit, pred = hp, modx = wt) print(p) ``` -------------------------------- ### Margins Analysis for Survey-Weighted Models Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/api-reference-sim_margins.md Computes marginal effects for survey-weighted generalized linear models using the 'survey' package. Requires setting up a survey design object. ```r library(survey) data(api) dstrat <- svydesign(id = ~1, strata = ~stype, weights = ~pw, data = apistrat, fpc = ~fpc) regmodel <- svyglm(api00 ~ ell * meals, design = dstrat) results <- sim_margins(regmodel, pred = ell, modx = meals) results ``` -------------------------------- ### Convert sim_slopes to huxtable Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/s3-methods.md Converts simple slopes results to a formatted huxtable object. Requires the `huxtable` package. The output is suitable for console printing or exporting to various file formats. ```r results <- sim_slopes(model, pred = x, modx = z) table <- as_huxtable(results) print(table) ``` -------------------------------- ### sim_slopes Function Signature Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/api-reference-sim_slopes.md This is the complete function signature for sim_slopes, detailing all available parameters and their default values. It is useful for understanding the full range of options when performing simple slopes analysis. ```r sim_slopes( model, pred, modx, mod2 = NULL, modx.values = NULL, mod2.values = NULL, centered = "all", at = NULL, data = NULL, cond.int = FALSE, johnson_neyman = TRUE, jnplot = FALSE, jnalpha = 0.05, robust = FALSE, digits = getOption("jtools-digits", default = 2), pvals = TRUE, confint = FALSE, ci.width = 0.95, cluster = NULL, modx.labels = NULL, mod2.labels = NULL, v.cov = NULL, v.cov.args = NULL, ... ) ``` -------------------------------- ### Interaction Plot for Survey Data Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/api-reference-interact_plot.md Demonstrates plotting interactions for survey data using the survey package. Requires a svydesign object and a svyglm model. ```r library(survey) data(api) dstrat <- svydesign(id = ~1, strata = ~stype, weights = ~pw, data = apistrat, fpc = ~fpc) regmodel <- svyglm(api00 ~ ell * meals, design = dstrat) p <- interact_plot(regmodel, pred = ell, modx = meals) print(p) ``` -------------------------------- ### Export Table to LaTeX Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/usage-guide.md Exports a summarized slopes table to a LaTeX file (.tex). Requires the huxtable package. ```r # Export to LaTeX export_table(table, file = "slopes_table.tex") ``` -------------------------------- ### Accessing Object Components Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/s3-methods.md Extract specific components like slopes tables, Johnson-Neyman intervals, or fitted models from simulation results objects. ```r results <- sim_slopes(fit, pred = x, modx = z) # Access slopes table slopes_table <- results$slopes # Access Johnson-Neyman interval jn_list <- results$jn # Access all fitted models models <- results$mods ``` -------------------------------- ### Optimizing Computation Speed for Large Models Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/usage-guide.md Improve the performance of `sim_slopes` for large models by using simpler centering options or disabling the Johnson-Neyman interval calculation. These adjustments can significantly reduce computation time. ```r # Use simpler centering to speed up slopes <- sim_slopes(model, pred = x, modx = z, centered = "none") # Faster # Or skip J-N interval slopes <- sim_slopes(model, pred = x, modx = z, johnson_neyman = FALSE) ``` -------------------------------- ### Export Table to Excel Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/usage-guide.md Exports a summarized slopes table to an Excel file (.xlsx). Requires the huxtable package. ```r # Export to Excel export_table(table, file = "slopes_table.xlsx") ``` -------------------------------- ### Logistic Regression Interaction Analysis and Visualization Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/INDEX.md Demonstrates analyzing and visualizing interactions in a logistic regression model, showing slopes in log-odds and plotting on the probability scale. ```R model <- glm(vs ~ hp * wt, data = mtcars, family = binomial) # Slopes in log-odds sim_slopes(model, pred = hp, modx = wt) # Plot on probability scale interact_plot(model, pred = hp, modx = wt, outcome.scale = "response") ``` -------------------------------- ### Categorical Interaction Plot Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/INDEX.md Illustrates how to use cat_plot to visualize an interaction between two categorical variables, including confidence intervals. ```R # Categorical × Categorical model <- lm(price ~ cut * color, data = diamonds) cat_plot(model, pred = cut, modx = color, interval = TRUE) ``` -------------------------------- ### Probe Interaction with Plotting Options and Precision Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/parameter-reference.md This snippet probes interactions, showing data points, uncertainty intervals, and the J-N interval with a specified number of digits for precision. ```r probe_interaction(model, pred = x, modx = z, plot.points = TRUE, # Show data interval = TRUE, # Show uncertainty jnplot = TRUE, # Show J-N interval digits = 3) # More precision ``` -------------------------------- ### Tidy Simple Slopes Results Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/s3-methods.md Converts the results of a sim_slopes analysis into a tidy data frame, compatible with the broom package. The output includes conditional slope estimates, standard errors, and p-values. ```r results <- sim_slopes(model, pred = x, modx = z) tidy_results <- tidy(results) ``` -------------------------------- ### Overlay Observed Data with Jitter Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/api-reference-interact_plot.md Enhances the visibility of observed data points by adding jitter, which helps to reduce overplotting when many points share similar values. ```r # With jitter to reduce overplotting interact_plot(fit, pred = hp, modx = wt, plot.points = TRUE, jitter = 0.1) ``` -------------------------------- ### Fit a model with an interaction Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/README.md Fit a linear model including an interaction term between predictors x and z. This is a common first step for interaction analysis. ```r fit <- lm(y ~ x * z, data = d) ``` -------------------------------- ### Customize ggplot Object Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/api-reference-cat_plot.md Demonstrates how to customize the ggplot object returned by cat_plot using standard ggplot2 functions like theme_minimal and scale_color_manual. ```r # Example customization p <- cat_plot(model, pred = x, modx = z) p + theme_minimal() + scale_color_manual(values = c("#E69F00", "#56B4E9")) ``` -------------------------------- ### Export Table to Word Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/usage-guide.md Exports a summarized slopes table to a Word document (.docx). Requires the huxtable package. ```r library(huxtable) slopes <- sim_slopes(model, pred = x, modx = z, digits = 3) # Convert to huxtable table <- as_huxtable(slopes) # Export to Word export_table(table, file = "slopes_table.docx") ``` -------------------------------- ### Display Confidence Intervals Instead of Standard Errors Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/parameter-reference.md Configure the output to display confidence interval bounds instead of standard errors and t-statistics. Set 'confint' to TRUE. ```r sim_slopes(model, pred = x, modx = z, confint = TRUE, ci.width = 0.95) ``` -------------------------------- ### Margins Analysis with Delta Method Standard Errors Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/api-reference-sim_margins.md Computes marginal effects using the delta method for standard error estimation, which is the default and fastest method. ```r # Delta method (default, fast) results <- sim_margins(fit, pred = hp, modx = wt, vce = "delta") ``` -------------------------------- ### Using Robust Standard Errors Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/00-START-HERE.md For analyses with heteroscedastic data, `sim_slopes` supports robust standard errors. Specify the type of robust SEs using the `robust` argument, such as `"HC3"` for heteroscedasticity-consistent standard errors. ```r # Use robust SEs with messy data sim_slopes(model, pred = x, modx = z, robust = "HC3") ``` -------------------------------- ### Tidy Simple Margins Results Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/s3-methods.md Converts the results of a sim_margins analysis into a tidy data frame, similar in structure to tidy.sim_slopes. ```r results <- sim_margins(model, pred = x, modx = z) tidy_results <- tidy(results) ``` -------------------------------- ### Combine Interaction Analysis and Plotting Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/INDEX.md The probe_interaction function conveniently combines the functionality of sim_slopes and interact_plot into a single call. It simplifies the process of performing comprehensive interaction analysis and visualization. ```r results <- probe_interaction(model, pred = x, modx = z) ``` -------------------------------- ### Plot Simple Slopes Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/s3-methods.md Generates a plot of simple slopes from a sim_slopes object. It may delegate to Johnson-Neyman plotting if available. For more control, consider using johnson_neyman() directly. ```r results <- sim_slopes(model, pred = x, modx = z, johnson_neyman = TRUE) plot(results) ``` -------------------------------- ### Margins Analysis with Custom Confidence Intervals Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/api-reference-sim_margins.md Displays confidence intervals instead of standard errors for marginal effects. Allows specifying the confidence interval width. ```r # Show CIs instead of standard errors results <- sim_margins(fit, pred = hp, modx = wt, confint = TRUE, ci.width = 0.99) results ``` -------------------------------- ### Plot Interaction Effects with Confidence Intervals Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/00-START-HERE.md Generates a plot to visualize interaction effects, including confidence intervals. This helps in understanding the nature and significance of the interaction. ```r interact_plot(model, pred = x, modx = z, interval = TRUE) ``` -------------------------------- ### sim_margins Function Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/api-reference-sim_margins.md Conducts a simple margins analysis for understanding interaction effects in regression. It is similar to sim_slopes but uses the margins package approach with different standard error calculations. This function is suitable for analyzing two- and three-way interactions. ```APIDOC ## sim_margins ### Description Conducts a simple margins analysis for understanding interaction effects in regression models. It is suitable for two- and three-way interactions and utilizes the `margins` package approach for standard error calculations. ### Function Signature ```r sim_margins( model, pred, modx, mod2 = NULL, modx.values = NULL, mod2.values = NULL, data = NULL, cond.int = FALSE, vce = c("delta", "simulation", "bootstrap", "none"), iterations = 1000, digits = getOption("jtools-digits", default = 2), pvals = TRUE, confint = FALSE, ci.width = 0.95, cluster = NULL, modx.labels = NULL, mod2.labels = NULL, ... ) ``` ### Parameters #### Model Parameters - **model** (regression object) - Required - Fitted model (lm, glm, svyglm, merMod, etc.). Must include the interaction. - **pred** (symbol/string) - Required - Focal predictor variable. Evaluated with rlang (allows !! syntax). Must be numeric. - **modx** (symbol/string) - Required - Moderator variable. Evaluated with rlang. - **mod2** (symbol/string) - Optional - Optional second moderator for three-way interactions. #### Value Specification - **modx.values** (vector/character) - Optional - Values at which to compute margins. NULL (or "mean-plus-minus") uses mean ± 1 SD for numeric. "plus-minus", "terciles" also available. Or numeric vector. - **mod2.values** (vector/character) - Optional - Values for second moderator. Same options as modx.values. #### Data and Options - **data** (data.frame) - Optional - Original data used to fit model. Improves centering. - **cond.int** (logical) - Optional - Include conditional intercepts in output? Default is FALSE. #### Standard Error and Output Control - **vce** (character) - Optional - Method for standard error calculation: "delta" (default, fast), "simulation", "bootstrap", or "none". - **iterations** (integer) - Optional - Number of iterations for "simulation" or "bootstrap" vce methods. Default is 1000. - **digits** (integer) - Optional - Decimal places in output. Default is 2. - **pvals** (logical) - Optional - Include p-values? Default is TRUE. - **confint** (logical) - Optional - Show confidence intervals instead of standard errors? Default is FALSE. - **ci.width** (numeric) - Optional - Confidence interval width (0-1). Ignored if confint=FALSE. Default is 0.95. - **cluster** (character/vector) - Optional - Column name of cluster variable or vector for clustered standard errors. #### Labels - **modx.labels** (character vector) - Optional - Labels for moderator values. If NULL, values themselves used. - **mod2.labels** (character vector) - Optional - Labels for second moderator values. #### Additional Arguments - **...** - Optional - Arguments passed to `margins::margins()`. ``` -------------------------------- ### Check Object Class Source: https://github.com/jacob-long/interactions/blob/master/_autodocs/types.md Check the class of an object using the `class()` function. Use `inherits()` to specifically check if an object belongs to the 'sim_slopes' class. ```r class(object) inherits(object, "sim_slopes") is_sim_slopes <- inherits(object, "sim_slopes") ```