Title: | Integrated Toolkit for Psychological Analysis and Modeling in R |
---|---|
Description: | A beginner-friendly R package for modeling in psychology or related field. It allows fitting models, plotting, checking goodness of fit, and model assumption violations all in one place. It also produces beautiful and easy-to-read output. |
Authors: | Jason Moy [aut, cre] |
Maintainer: | Jason Moy <[email protected]> |
License: | GPL (>= 3) |
Version: | 0.5.0.9000 |
Built: | 2025-03-04 05:20:47 UTC |
Source: | https://github.com/jasonmoy28/psycmodel |
Acitelli, L. K. (1997). Sampling couples to understand them: Mixing the theoretical with the practical. Journal of Social and Personal Relationships, 14, 243-261.
acitelli
acitelli
A data frame with 296 rows and 16 variables:
each couple has 1 couple id
each couple has 1 value for years married (centered; 11.214 subtracted)
A variable to distinguish the two members (1 and 2)
-1 Female; 1 Male
Mean of positive self variables (uncentered)
Mean of positive partner variables (uncentered)(both variables the average of Cooperative, Mature, Friendly, Hard Working and Care about Others)
Mean of four variables (theoretical range from 1 to 4)
Mean of two disagreement and tension (theoretical range from 1 to 4)
Perception of having similar hobbies (1 = very true; 0 = somewhat true; -1 = not at all true)
-1 Female; 1 Male
Mean of positive self variables (uncentered)
Mean of positive partner variables (uncentered) (both variables the average of Cooperative, Mature, Friendly, Hard Working and Care about Others)
Mean of four variables (theoretical range from 1 to 4)
Mean of two disagreement and tension (theoretical range from 1 to 4)
Perception of having similar hobbies (1 = very true; 0 = somewhat true; -1 = not at all true)
String variable with Husband and Wife
https://journals.sagepub.com/doi/10.1177/0265407597142006
Plot categorical variable with barplot. Continuous moderator are plotted at ± 1 SD from the mean.
anova_plot(model, predictor = NULL, graph_label_name = NULL, y_lim = NULL)
anova_plot(model, predictor = NULL, graph_label_name = NULL, y_lim = NULL)
model |
fitted model (usually |
predictor |
predictor variable. Must specified for non-interaction plot and must not specify for interaction plot. |
graph_label_name |
vector or function. Vector should be passed in the form of |
y_lim |
the plot's upper and lower limit for the y-axis. Length of 2. Example: |
a ggplot
object
# Main effect plot with 1 categorical variable fit_1 = lavaan::HolzingerSwineford1939 %>% dplyr::mutate(school = as.factor(school)) %>% lm(data = ., grade ~ school) anova_plot(fit_1,predictor = school) # Interaction effect plot with 2 categorical variables fit_2 = lavaan::HolzingerSwineford1939 %>% dplyr::mutate(dplyr::across(c(school,sex),as.factor)) %>% lm(data = ., grade ~ school*sex) anova_plot(fit_2) # Interaction effect plot with 1 categorical variable and 1 continuous variable fit_3 = lavaan::HolzingerSwineford1939 %>% dplyr::mutate(school = as.factor(school)) %>% dplyr::mutate(ageyr = as.numeric(ageyr)) %>% lm(data = ., grade ~ ageyr*school) anova_plot(fit_3)
# Main effect plot with 1 categorical variable fit_1 = lavaan::HolzingerSwineford1939 %>% dplyr::mutate(school = as.factor(school)) %>% lm(data = ., grade ~ school) anova_plot(fit_1,predictor = school) # Interaction effect plot with 2 categorical variables fit_2 = lavaan::HolzingerSwineford1939 %>% dplyr::mutate(dplyr::across(c(school,sex),as.factor)) %>% lm(data = ., grade ~ school*sex) anova_plot(fit_2) # Interaction effect plot with 1 categorical variable and 1 continuous variable fit_3 = lavaan::HolzingerSwineford1939 %>% dplyr::mutate(school = as.factor(school)) %>% dplyr::mutate(ageyr = as.numeric(ageyr)) %>% lm(data = ., grade ~ ageyr*school) anova_plot(fit_3)
APIM_sem( data, mod_type, predictor_a, predictor_p, outcome_a, outcome_p, med_a = NULL, med_p = NULL, mod_a = NULL, mod_p = NULL, bootstrap = NULL, standardized = FALSE, return_result = FALSE, quite = FALSE )
APIM_sem( data, mod_type, predictor_a, predictor_p, outcome_a, outcome_p, med_a = NULL, med_p = NULL, mod_a = NULL, mod_p = NULL, bootstrap = NULL, standardized = FALSE, return_result = FALSE, quite = FALSE )
data |
data frame object |
mod_type |
options are "simple" (main effect), "med" (mediation), and "mod" (moderation) |
predictor_a |
predictor variable name for actor |
predictor_p |
predictor variable name for partner |
outcome_a |
dependent variable name for actor |
outcome_p |
dependent variable name for partner |
med_a |
mediation variable name for actor |
med_p |
mediation variable name for partner |
mod_a |
moderation variable name for actor |
mod_p |
moderation variable name for partner |
bootstrap |
number of bootstrapping (e.g., 5000). Default is not using bootstrap |
standardized |
standardized coefficient |
return_result |
return |
quite |
suppress printing output. Default is |
Actor-partner interdependence model using SEM approach (with lavaan). Indistinguishable dyads only. Results should be the same as those from Kenny (2015a, 2015b).
data.frame from lavaan::parameterestimates()
Kenny, D. A. (2015, October). An interactive tool for the estimation and testing mediation in the Actor-Partner Interdependence Model using structural equation modeling. Computer software. Available from https://davidakenny.shinyapps.io/APIMeM/. Kenny, D. A. (2015, October). An interactive tool for the estimation and testing moderation in the Actor-Partner Interdependence Model using structural equation modeling. Computer software. Available from https://davidakenny.shinyapps.io/APIMoM/. Stas, L, Kenny, D. A., Mayer, A., & Loeys, T. (2018). Giving Dyadic Data Analysis Away: A User-Friendly App for Actor-Partner Interdependence Models. Personal Relationships, 25 (1), 103-119. https://doi.org/10.1111/pere.12230.
APIM_sem(data = acitelli, predictor_a = 'Tension_A', predictor_p = 'Tension_P', outcome_a = 'Satisfaction_A', outcome_p = 'Satisfaction_P', mod_type = 'simple')
APIM_sem(data = acitelli, predictor_a = 'Tension_A', predictor_p = 'Tension_P', outcome_a = 'Satisfaction_A', outcome_p = 'Satisfaction_P', mod_type = 'simple')
Actor-partner interdependence model that test multiple moderators simultaneously.
APIM_table( data, predictor_a, predictor_p, outcome_a, outcome_p, mod_a = NULL, mod_p = NULL, mod_type = "mod", return_result = FALSE )
APIM_table( data, predictor_a, predictor_p, outcome_a, outcome_p, mod_a = NULL, mod_p = NULL, mod_type = "mod", return_result = FALSE )
data |
data frame object |
predictor_a |
predictor variable name for actor |
predictor_p |
predictor variable name for partner |
outcome_a |
dependent variable name for actor |
outcome_p |
dependent variable name for partner |
mod_a |
moderation variable name for actor. Support |
mod_p |
moderation variable name for partner. Support |
mod_type |
only 'mod' is supported for now |
return_result |
return |
data.frame of the APIM table
APIM_table(data = acitelli, predictor_a = 'Tension_A', predictor_p = 'Tension_P', outcome_a = 'Satisfaction_A', outcome_p = 'Satisfaction_P', mod_a = c('SelfPos_A','OtherPos_A','SimHob_A'), mod_p = c('SelfPos_P','OtherPos_P','SimHob_P'))
APIM_table(data = acitelli, predictor_a = 'Tension_A', predictor_p = 'Tension_P', outcome_a = 'Satisfaction_A', outcome_p = 'Satisfaction_P', mod_a = c('SelfPos_A','OtherPos_A','SimHob_A'), mod_p = c('SelfPos_P','OtherPos_P','SimHob_P'))
This function will run N number of CFA where N = length(group)
, and report the fit measures of CFA in each group.
The function is intended to help you get a better understanding of which group has abnormal fit indicator
cfa_groupwise(data, ..., group, model = NULL, ordered = FALSE)
cfa_groupwise(data, ..., group, model = NULL, ordered = FALSE)
All argument must be explicitly specified. If not, all arguments will be treated as CFA items
a data.frame
with group-wise CFA result
# The example is used as the illustration of the function output only. # It does not imply the data is appropriate for the analysis. cfa_groupwise( data = lavaan::HolzingerSwineford1939, group = "school", x1:x3, x4:x6, x7:x9 )
# The example is used as the illustration of the function output only. # It does not imply the data is appropriate for the analysis. cfa_groupwise( data = lavaan::HolzingerSwineford1939, group = "school", x1:x3, x4:x6, x7:x9 )
The function fits a CFA model using the lavaan::cfa()
. Users can fit single and multiple factors CFA, and it also supports multilevel CFA (by specifying the group).
Users can fit the model by passing the items using dplyr::select()
syntax or an explicit lavaan model for more versatile usage.
All arguments (except the CFA items) must be explicitly named (e.g., model = your-model; see example for inappropriate behavior).
cfa_summary( data, ..., model = NULL, group = NULL, ordered = FALSE, digits = 3, estimator = "ML", model_covariance = TRUE, model_variance = TRUE, plot = TRUE, group_partial = NULL, streamline = FALSE, quite = FALSE, return_result = FALSE )
cfa_summary( data, ..., model = NULL, group = NULL, ordered = FALSE, digits = 3, estimator = "ML", model_covariance = TRUE, model_variance = TRUE, plot = TRUE, group_partial = NULL, streamline = FALSE, quite = FALSE, return_result = FALSE )
First, just like researchers have argued against p value of 0.05 is not a good cut-of, researchers have also argue against that fit indicies (more importantly, the cut-off criteria) are not completely representative of the goodness of fit. Nonetheless, you are required to report them if you are publishing an article anyway. I will summarize the general recommended cut-off criteria for CFA model below. Researchers consider models with CFI (Bentler, 1990) that is > 0.95 to be excellent fit (Hu & Bentler, 1999), and > 0.9 to be acceptable fit. Researchers considered a model is excellent fit if CFI > 0.95 (Hu & Bentler, 1999), RMSEA < 0.06 (Hu & Bentler, 1999), TLI > 0.95, SRMR < 0.08. The model is considered an acceptable fit if CFI > 0.9 and RMSEA < 0.08. I need some time to find all the relevant references, but this should be the general consensus.
a lavaan
object if return_result is TRUE
Hu, L., & Bentler, P. M. (1999). Cutoff criteria for fit indexes in covariance structure analysis: Conventional criteria versus new alternatives. Structural Equation Modeling, 6, 1–55. https://doi.org/10.1080/10705519909540118
# REMEMBER, YOU MUST NAMED ALL ARGUMENT EXCEPT THE CFA ITEMS ARGUMENT # Fitting a multilevel single factor CFA model fit <- cfa_summary( data = lavaan::HolzingerSwineford1939, x1:x3, x4:x6, x7:x9, group = "sex", model_variance = FALSE, # do not print the model_variance model_covariance = FALSE # do not print the model_covariance ) # Fitting a CFA model by passing explicit lavaan model (equivalent to the above model) # Note in the below function how I added `model = ` in front of the lavaan model. # Similarly, the same rule apply for all arguments (e.g., `ordered = FALSE` instead of just `FALSE`) fit <- cfa_summary( model = "visual =~ x1 + x2 + x3", data = lavaan::HolzingerSwineford1939, quite = TRUE # silence all output ) ## Not run: # This will fail because I did not add `model = ` in front of the lavaan model. # Therefore,you must add the tag in front of all arguments # For example, `return_result = 'model'` instaed of `model` cfa_summary("visual =~ x1 + x2 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 ", data = lavaan::HolzingerSwineford1939 ) ## End(Not run)
# REMEMBER, YOU MUST NAMED ALL ARGUMENT EXCEPT THE CFA ITEMS ARGUMENT # Fitting a multilevel single factor CFA model fit <- cfa_summary( data = lavaan::HolzingerSwineford1939, x1:x3, x4:x6, x7:x9, group = "sex", model_variance = FALSE, # do not print the model_variance model_covariance = FALSE # do not print the model_covariance ) # Fitting a CFA model by passing explicit lavaan model (equivalent to the above model) # Note in the below function how I added `model = ` in front of the lavaan model. # Similarly, the same rule apply for all arguments (e.g., `ordered = FALSE` instead of just `FALSE`) fit <- cfa_summary( model = "visual =~ x1 + x2 + x3", data = lavaan::HolzingerSwineford1939, quite = TRUE # silence all output ) ## Not run: # This will fail because I did not add `model = ` in front of the lavaan model. # Therefore,you must add the tag in front of all arguments # For example, `return_result = 'model'` instaed of `model` cfa_summary("visual =~ x1 + x2 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 ", data = lavaan::HolzingerSwineford1939 ) ## End(Not run)
Compare the fit indices of models (see below for model support)
compare_fit( ..., digits = 3, quite = FALSE, streamline = FALSE, return_result = FALSE )
compare_fit( ..., digits = 3, quite = FALSE, streamline = FALSE, return_result = FALSE )
... |
model. If it is a |
digits |
number of digits to round to |
quite |
suppress printing output |
streamline |
print streamlined output |
return_result |
If it is set to |
a dataframe
with fit indices and change in fit indices
# lme model fit1 <- lm_model( data = popular, response_variable = popular, predictor_var = c(sex, extrav) ) fit2 <- lm_model( data = popular, response_variable = popular, predictor_var = c(sex, extrav), two_way_interaction_factor = c(sex, extrav) ) compare_fit(fit1, fit2) # see ?measurement_invariance for measurement invariance example
# lme model fit1 <- lm_model( data = popular, response_variable = popular, predictor_var = c(sex, extrav) ) fit2 <- lm_model( data = popular, response_variable = popular, predictor_var = c(sex, extrav), two_way_interaction_factor = c(sex, extrav) ) compare_fit(fit1, fit2) # see ?measurement_invariance for measurement invariance example
This function uses the correlation::correlation()
to generate the correlation table.
cor_test( data, cols, ..., digits = 3, show_p = FALSE, method = "pearson", p_adjust = "none", streamline = FALSE, verbose = TRUE, return_result = FALSE )
cor_test( data, cols, ..., digits = 3, show_p = FALSE, method = "pearson", p_adjust = "none", streamline = FALSE, verbose = TRUE, return_result = FALSE )
data |
data frame |
cols |
correlation items. Support |
... |
additional arguments passed to correlation::correlation(). See ?correlation::correlation. Note that the return data.frame from correlation::correlation() must contains |
digits |
number of digits to round to |
show_p |
Default is |
method |
Default is "pearson". Options are "kendall", "spearman","biserial", "polychoric", "tetrachoric", "biweight", "distance", "percentage", "blomqvist", "hoeffding", "gamma", "gaussian","shepherd", or "auto". See ?correlation::correlation for detail |
p_adjust |
Default is "holm". Options are "hochberg", "hommel", "bonferroni", "BH", "BY", "fdr", "somers" or "none". See ?stats::p.adjust for more detail |
streamline |
print streamlined output. |
verbose |
deafult is |
return_result |
If it is set to |
a data.frame
of the correlation table
cor_test(iris, where(is.numeric))
cor_test(iris, where(is.numeric))
Computing the Cronbach alphas for multiple factors.
cronbach_alpha( ..., data, var_name, group = NULL, quite = FALSE, return_result = FALSE )
cronbach_alpha( ..., data, var_name, group = NULL, quite = FALSE, return_result = FALSE )
... |
Items. Group each latent factors using c() with when computing Cronbach alpha for 2+ factors (see example below) |
data |
|
var_name |
character or a vector of characters. The order of |
group |
optional character. Specify this argument for computing Cronbach alpha for group separetely |
quite |
suppress printing output |
return_result |
If it is set to |
a data.frame
object if return_result is TRUE
cronbach_alpha( data = lavaan::HolzingerSwineford1939, var_name = c('Visual','Textual','Speed'), c(x1,x2,x3), # one way to pass the items of a factor is by wrapping it with c() x4:x6, # another way to pass the items is use tidyselect syntax x7:x9)
cronbach_alpha( data = lavaan::HolzingerSwineford1939, var_name = c('Visual','Textual','Speed'), c(x1,x2,x3), # one way to pass the items of a factor is by wrapping it with c() x4:x6, # another way to pass the items is use tidyselect syntax x7:x9)
This function generates a table of descriptive statistics (mainly using psych::describe()
) and or a correlation table. User can export this to a csv file (optionally, using the file_path argument). Users can open the csv file with MS Excel then copy and paste the table into MS Word table.
descriptive_table( data, cols, ..., digits = 3, descriptive_indicator = c("mean", "sd", "cor"), file_path = NULL, streamline = FALSE, quite = FALSE, show_p = FALSE, return_result = FALSE )
descriptive_table( data, cols, ..., digits = 3, descriptive_indicator = c("mean", "sd", "cor"), file_path = NULL, streamline = FALSE, quite = FALSE, show_p = FALSE, return_result = FALSE )
data |
|
cols |
column(s) need to be included in the table. Support |
... |
additional arguments passed to cor_test. See ?cor_test. |
digits |
number of digit for the descriptive table |
descriptive_indicator |
Default is mean, sd, cor. Options are missing (missing value count), non_missing (non-missing value count), cor (correlation table), n, mean, sd, median, trimmed (trimmed mean), median, mad (median absolute deviation from the median), min, max, range, skew, kurtosis, se (standard error) |
file_path |
file path for export. The function will implicitly pass this argument to the write.csv(file = file_path) |
streamline |
print streamlined output |
quite |
suppress printing output |
show_p |
Default is |
return_result |
If it is set to |
a data.frame
of the descriptive table
descriptive_table(iris, cols = where(is.numeric)) # all numeric columns descriptive_table(iris, cols = where(is.numeric), # get missing count, non-missing count, and mean & sd & correlation table descriptive_indicator = c("missing", "non_missing", "mean", "sd", "cor") )
descriptive_table(iris, cols = where(is.numeric)) # all numeric columns descriptive_table(iris, cols = where(is.numeric), # get missing count, non-missing count, and mean & sd & correlation table descriptive_indicator = c("missing", "non_missing", "mean", "sd", "cor") )
The function is used to fit a exploratory factor analysis model. It will first find the optimal number of factors using parameters::n_factors. Once the optimal number of factor is determined, the function will fit the model using
psych::fa()
. Optionally, you can request a post-hoc CFA model based on the EFA model which gives you more fit indexes (e.g., CFI, RMSEA, TLI)
efa_summary( data, cols, rotation = "varimax", optimal_factor_method = FALSE, efa_plot = TRUE, digits = 3, n_factor = NULL, post_hoc_cfa = FALSE, quite = FALSE, streamline = FALSE, return_result = FALSE )
efa_summary( data, cols, rotation = "varimax", optimal_factor_method = FALSE, efa_plot = TRUE, digits = 3, n_factor = NULL, post_hoc_cfa = FALSE, quite = FALSE, streamline = FALSE, return_result = FALSE )
data |
|
cols |
columns. Support |
rotation |
the rotation to use in estimation. Default is 'oblimin'. Options are 'none', 'varimax', 'quartimax', 'promax', 'oblimin', or 'simplimax' |
optimal_factor_method |
Show a summary of the number of factors by optimization method (e.g., BIC, VSS complexity, Velicer's MAP) |
efa_plot |
show explained variance by number of factor plot. default is |
digits |
number of digits to round to |
n_factor |
number of factors for EFA. It will bypass the initial optimization algorithm, and fit the EFA model using this specified number of factor |
post_hoc_cfa |
a CFA model based on the extracted factor |
quite |
suppress printing output |
streamline |
print streamlined output |
return_result |
If it is set to |
a fa
object from psych
efa_summary(lavaan::HolzingerSwineford1939, starts_with("x"), post_hoc_cfa = TRUE)
efa_summary(lavaan::HolzingerSwineford1939, starts_with("x"), post_hoc_cfa = TRUE)
get interaction term
get_interaction_term(model)
get_interaction_term(model)
model |
model |
a list with predict vars names
get factor df to combine with mean_df
get_predict_df(data)
get_predict_df(data)
data |
data |
factor_df
Fit a generalized linear regression using glm()
. This function is still in early development stage.
glm_model( data, response_variable, predictor_variable, two_way_interaction_factor = NULL, three_way_interaction_factor = NULL, family, quite = FALSE )
glm_model( data, response_variable, predictor_variable, two_way_interaction_factor = NULL, three_way_interaction_factor = NULL, family, quite = FALSE )
data |
|
response_variable |
response variable. Support |
predictor_variable |
predictor variable. Support |
two_way_interaction_factor |
two-way interaction factors. You need to pass 2+ factor. Support |
three_way_interaction_factor |
three-way interaction factor. You need to pass exactly 3 factors. Specifying three-way interaction factors automatically included all two-way interactions, so please do not specify the two_way_interaction_factor argument. Support |
family |
a GLM family. It will passed to the family argument in glm See |
quite |
suppress printing output |
an object class of glm
representing the linear regression fit
fit <- glm_model( response_variable = incidence, predictor_variable = period, family = "poisson", # or you can enter as poisson(link = 'log'), data = lme4::cbpp )
fit <- glm_model( response_variable = incidence, predictor_variable = period, family = "poisson", # or you can enter as poisson(link = 'log'), data = lme4::cbpp )
Exploratory analyses for linear regression models with multiple response, predictor, and two-way interaction variables. (lm
models).
At the moment, multi-categorical variables are not supported as predictors or interactions (but control is fine). Binary variable should be numeric
instead of factor
glm_model_explore( data, response_variable, predictor_variable, family, two_way_interaction_variable = NULL, three_way_interaction_variable = NULL, control_variable = NULL, marginal_alpha = 0.1, verbose = TRUE, show_p = TRUE, return_result = FALSE, print_control = FALSE, plot_interaction = FALSE, file_dir = NULL, device = "jpeg", width = 8.5, height = 5, units = "in", y_lim = c(0, 1) )
glm_model_explore( data, response_variable, predictor_variable, family, two_way_interaction_variable = NULL, three_way_interaction_variable = NULL, control_variable = NULL, marginal_alpha = 0.1, verbose = TRUE, show_p = TRUE, return_result = FALSE, print_control = FALSE, plot_interaction = FALSE, file_dir = NULL, device = "jpeg", width = 8.5, height = 5, units = "in", y_lim = c(0, 1) )
data |
|
response_variable |
Response variable. Support |
predictor_variable |
Pred variable. Support |
family |
a GLM family. It will passed to the family argument in glm See |
two_way_interaction_variable |
Two-way interaction variable. Each two-way interaction variable will interact with each pred variable. Support |
three_way_interaction_variable |
Three-way interaction variable. Each three-way interaction variable will interact with each pred and two-way interaction variables. Support |
control_variable |
Control variables. Support |
marginal_alpha |
Set marginal_alpha level for marginally significant (denoted by |
verbose |
Default is |
show_p |
Default is |
return_result |
Default is |
print_control |
Default is |
plot_interaction |
Default is |
file_dir |
Path of the directory to save plot to |
device |
Default is "jpeg". See ggplot2::ggsave() for all options. |
width |
Default is 8.5 (i.e., letter size width). |
height |
Default is 5. |
units |
Default is inches. Options are "in", "cm", "mm" or "px". |
y_lim |
the plot's upper and lower limit for the y-axis. Length of 2. Example: |
data.frame
test = data.frame(y1 = sample(c(0, 1), size = 1000, replace = TRUE), y2 = sample(c(0, 1), size = 1000, replace = TRUE), y3 = sample(c(0, 1), size = 1000, replace = TRUE), x1 = rnorm(1000,100,10), x2 = rnorm(1000,10,1), x3 = rnorm(1000,6,2), m1 = rnorm(1000,3,1), m2 = rnorm(1000,2,0.5), m3 = rnorm(1000,9,0.1), c1 = rnorm(1000,5,0.4), c2 = rnorm(1000,2,0.2), c3 = rnorm(1000,7,0.9) ) glm_model_explore(data = test, response_variable = c(y1,y2,y3), predictor_variable = c(x1,x2,x3), two_way_interaction_variable = c(m1,m2,m3), family = binomial(link = 'logit'), control_variable = c(c1,c2,c3))
test = data.frame(y1 = sample(c(0, 1), size = 1000, replace = TRUE), y2 = sample(c(0, 1), size = 1000, replace = TRUE), y3 = sample(c(0, 1), size = 1000, replace = TRUE), x1 = rnorm(1000,100,10), x2 = rnorm(1000,10,1), x3 = rnorm(1000,6,2), m1 = rnorm(1000,3,1), m2 = rnorm(1000,2,0.5), m3 = rnorm(1000,9,0.1), c1 = rnorm(1000,5,0.4), c2 = rnorm(1000,2,0.2), c3 = rnorm(1000,7,0.9) ) glm_model_explore(data = test, response_variable = c(y1,y2,y3), predictor_variable = c(x1,x2,x3), two_way_interaction_variable = c(m1,m2,m3), family = binomial(link = 'logit'), control_variable = c(c1,c2,c3))
This is a helper function for knitting Rmd. Due to technological limitation, the output cannot knit to PDF in Rmd directly (the problem is with the latex engine printing unicode character).
Therefore, to bypass this problem, you will first need to knit to html file first, then use this function to convert it to a PDF file.
html_to_pdf(file_path = NULL, dir = NULL, scale = 1, render_exist = FALSE)
html_to_pdf(file_path = NULL, dir = NULL, scale = 1, render_exist = FALSE)
file_path |
file path to the HTML file (can be relative if you are in a R project) |
dir |
file path to the directory of all HTML files (can be relative if you are in a R project) |
scale |
the scale of the PDF |
render_exist |
overwrite exist PDF. Default is |
no return value
## Not run: html_to_pdf(file_path = "html_name.html") # all HTML files in the my_html_folder will be converted html_to_pdf(dir = "Users/Desktop/my_html_folder") ## End(Not run)
## Not run: html_to_pdf(file_path = "html_name.html") # all HTML files in the my_html_folder will be converted html_to_pdf(dir = "Users/Desktop/my_html_folder") ## End(Not run)
The function creates interaction plot. By default, it will create an interaction plot using -1 SD and +1 SD of continuous variables, or the two levels of binary variables.
interaction_plot( model, interaction_term = NULL, response_var_name = NULL, predict_var1_name = NULL, predict_var2_name = NULL, predict_var3_name = NULL, predict_var1_level = NULL, predict_var2_level = NULL, predict_var3_level = NULL, predict_var1_level_name = NULL, predict_var2_level_name = NULL, predict_var3_level_name = NULL, y_lim = NULL, plot_color = FALSE, return_plot_data = FALSE, return_plot = FALSE, verbose = TRUE, print_plot = TRUE, data = NULL )
interaction_plot( model, interaction_term = NULL, response_var_name = NULL, predict_var1_name = NULL, predict_var2_name = NULL, predict_var3_name = NULL, predict_var1_level = NULL, predict_var2_level = NULL, predict_var3_level = NULL, predict_var1_level_name = NULL, predict_var2_level_name = NULL, predict_var3_level_name = NULL, y_lim = NULL, plot_color = FALSE, return_plot_data = FALSE, return_plot = FALSE, verbose = TRUE, print_plot = TRUE, data = NULL )
model |
a regression model object from effect. |
interaction_term |
default is the first highest order interaction term in the model. The term should be given explicitly if you want to plot other interaction terms. |
response_var_name |
The name of the response variable can be changed using this setting. |
predict_var1_name |
The name of the first predictor can be changed using this setting. |
predict_var2_name |
The name of the second predictor can be changed using this setting. |
predict_var3_name |
The name of the third predictor can be changed using this setting. |
predict_var1_level |
The default is -1 SD and +1 SD for a continuous variable, and it is the two levels for a binary variable. These can be changed using this setting. |
predict_var2_level |
The default is -1 SD and +1 SD for a continuous variable, and it is the two levels for a binary variable. These can be changed using this setting. |
predict_var3_level |
The default is -1 SD and +1 SD for a continuous variable, and it is the two levels for a binary variable. These can be changed using this setting. |
predict_var1_level_name |
The labels of the level can be change using this value (e.g., |
predict_var2_level_name |
The labels of the level can be change using this value (e.g., |
predict_var3_level_name |
The labels of the level can be change using this value (e.g., |
y_lim |
the plot's upper and lower limit for the y-axis. Length of 2. Example: |
plot_color |
default if |
return_plot_data |
default is |
return_plot |
default is |
verbose |
deafult is |
print_plot |
default is |
data |
Optional data.frame. Only used when it is not possible to extract data from the model object. |
a ggplot
object
model_1 <- lm(Sepal.Length ~ Petal.Width * Sepal.Width, data = iris) interaction_plot(model_1) model_2 <- lm(Sepal.Length ~ Petal.Width * Sepal.Width * Petal.Length, data = iris ) interaction_plot(model_2, # it will automatically select the first three-way interaction term # change the name of the variables of the plot response_var_name = 'SEPAL LENGTH', predict_var1_name = 'PETAL WIDTH', predict_var2_name = 'SEPAL WIDTH', predict_var3_name = 'PETAL LENGTH', # change the number of levels of the variables (e.g., adding the mean) predict_var1_level = c(0.43, 1.19,1.96), predict_var2_level = c(2.62, 3.05,3.49), predict_var3_level = c(1.99,3.758,5.52), predict_var1_level_name = c('-1 SD','Mean','+1 SD'), predict_var2_level_name = c('-1 SD','Mean','+1 SD'), predict_var3_level_name = c('-1 SD','Mean','+1 SD'))
model_1 <- lm(Sepal.Length ~ Petal.Width * Sepal.Width, data = iris) interaction_plot(model_1) model_2 <- lm(Sepal.Length ~ Petal.Width * Sepal.Width * Petal.Length, data = iris ) interaction_plot(model_2, # it will automatically select the first three-way interaction term # change the name of the variables of the plot response_var_name = 'SEPAL LENGTH', predict_var1_name = 'PETAL WIDTH', predict_var2_name = 'SEPAL WIDTH', predict_var3_name = 'PETAL LENGTH', # change the number of levels of the variables (e.g., adding the mean) predict_var1_level = c(0.43, 1.19,1.96), predict_var2_level = c(2.62, 3.05,3.49), predict_var3_level = c(1.99,3.758,5.52), predict_var1_level_name = c('-1 SD','Mean','+1 SD'), predict_var2_level_name = c('-1 SD','Mean','+1 SD'), predict_var3_level_name = c('-1 SD','Mean','+1 SD'))
This is a helper function that instruct users of the package how to knit a R Markdown (Rmd) files
knit_to_Rmd()
knit_to_Rmd()
no return value
knit_to_Rmd()
knit_to_Rmd()
get label name
label_name( graph_label_name, response_var_name, predict_var1_name, predict_var2_name, predict_var3_name )
label_name( graph_label_name, response_var_name, predict_var1_name, predict_var2_name, predict_var3_name )
graph_label_name |
label name |
response_var_name |
outcome variable name |
predict_var1_name |
predictor 1 name |
predict_var2_name |
predictor 2 name |
predict_var3_name |
predictor 3 name |
vector of var name
Fit a linear regression using lm()
. Linear regression is used to explore the effect of continuous variables / categorical variables in predicting a normally-distributed continuous variables.
lm_model( data, response_variable, predictor_variable, two_way_interaction_factor = NULL, three_way_interaction_factor = NULL, quite = FALSE )
lm_model( data, response_variable, predictor_variable, two_way_interaction_factor = NULL, three_way_interaction_factor = NULL, quite = FALSE )
data |
|
response_variable |
response variable. Support |
predictor_variable |
predictor variable. Support |
two_way_interaction_factor |
two-way interaction factors. You need to pass 2+ factor. Support |
three_way_interaction_factor |
three-way interaction factor. You need to pass exactly 3 factors. Specifying three-way interaction factors automatically included all two-way interactions, so please do not specify the two_way_interaction_factor argument. Support |
quite |
suppress printing output |
an object class of lm
representing the linear regression fit
fit <- lm_model( data = iris, response_variable = Sepal.Length, predictor_variable = dplyr::everything(), two_way_interaction_factor = c(Sepal.Width, Species) )
fit <- lm_model( data = iris, response_variable = Sepal.Length, predictor_variable = dplyr::everything(), two_way_interaction_factor = c(Sepal.Width, Species) )
Exploratory analyses for linear regression models with multiple response, predictor, and two-way interaction variables. (lm
models).
At the moment, multi-categorical variables are not supported as predictors or interactions (but control is fine). Binary variable should be numeric
instead of factor
lm_model_explore( data, response_variable, predictor_variable, two_way_interaction_variable = NULL, three_way_interaction_variable = NULL, control_variable = NULL, marginal_alpha = 0.1, verbose = TRUE, show_p = TRUE, return_result = FALSE, print_control = FALSE, plot_interaction = FALSE, file_dir = NULL, device = "jpeg", width = 8.5, height = 5, units = "in", y_lim = NULL )
lm_model_explore( data, response_variable, predictor_variable, two_way_interaction_variable = NULL, three_way_interaction_variable = NULL, control_variable = NULL, marginal_alpha = 0.1, verbose = TRUE, show_p = TRUE, return_result = FALSE, print_control = FALSE, plot_interaction = FALSE, file_dir = NULL, device = "jpeg", width = 8.5, height = 5, units = "in", y_lim = NULL )
data |
|
response_variable |
Response variable. Support |
predictor_variable |
Pred variable. Support |
two_way_interaction_variable |
Two-way interaction variable. Each two-way interaction variable will interact with each pred variable. Support |
three_way_interaction_variable |
Three-way interaction variable. Each three-way interaction variable will interact with each pred and two-way interaction variables. Support |
control_variable |
Control variables. Support |
marginal_alpha |
Set marginal_alpha level for marginally significant (denoted by |
verbose |
Default is |
show_p |
Default is |
return_result |
Default is |
print_control |
Default is |
plot_interaction |
Default is |
file_dir |
Path of the directory to save plot to |
device |
Default is "jpeg". See ggplot2::ggsave() for all options. |
width |
Default is 8.5 (i.e., letter size width). |
height |
Default is 5. |
units |
Default is inches. Options are "in", "cm", "mm" or "px". |
y_lim |
the plot's upper and lower limit for the y-axis. Length of 2. Example: |
data.frame
test = data.frame(y1 = rnorm(1000,2,3), y2 = rnorm(1000,10,2), y3 = rnorm(1000,1,4), x1 = rnorm(1000,100,10), x2 = rnorm(1000,10,1), x3 = rnorm(1000,6,2), m1 = rnorm(1000,3,1), m2 = rnorm(1000,2,0.5), m3 = rnorm(1000,9,0.1), c1 = rnorm(1000,5,0.4), c2 = rnorm(1000,2,0.2), c3 = rnorm(1000,7,0.9) ) lm_model_explore(data = test, response_variable = c(y1,y2,y3), predictor_variable = c(x1,x2,x3), two_way_interaction_variable = c(m1,m2,m3), control_variable = c(c1,c2,c3))
test = data.frame(y1 = rnorm(1000,2,3), y2 = rnorm(1000,10,2), y3 = rnorm(1000,1,4), x1 = rnorm(1000,100,10), x2 = rnorm(1000,10,1), x3 = rnorm(1000,6,2), m1 = rnorm(1000,3,1), m2 = rnorm(1000,2,0.5), m3 = rnorm(1000,9,0.1), c1 = rnorm(1000,5,0.4), c2 = rnorm(1000,2,0.2), c3 = rnorm(1000,7,0.9) ) lm_model_explore(data = test, response_variable = c(y1,y2,y3), predictor_variable = c(x1,x2,x3), two_way_interaction_variable = c(m1,m2,m3), control_variable = c(c1,c2,c3))
An integrated function for fitting a linear regression model.
This function will no longer be updated. Please use the these functions separately instead: model_summary
, interaction_plot
,and simple_slope
.
lm_model_summary( data, response_variable = NULL, predictor_variable = NULL, two_way_interaction_factor = NULL, three_way_interaction_factor = NULL, family = NULL, cateogrical_var = NULL, graph_label_name = NULL, model_summary = TRUE, interaction_plot = TRUE, y_lim = NULL, plot_color = FALSE, digits = 3, simple_slope = FALSE, assumption_plot = FALSE, quite = FALSE, streamline = FALSE, return_result = FALSE )
lm_model_summary( data, response_variable = NULL, predictor_variable = NULL, two_way_interaction_factor = NULL, three_way_interaction_factor = NULL, family = NULL, cateogrical_var = NULL, graph_label_name = NULL, model_summary = TRUE, interaction_plot = TRUE, y_lim = NULL, plot_color = FALSE, digits = 3, simple_slope = FALSE, assumption_plot = FALSE, quite = FALSE, streamline = FALSE, return_result = FALSE )
a list of all requested items in the order of model, model_summary, interaction_plot, simple_slope
fit <- lm_model_summary( data = iris, response_variable = "Sepal.Length", predictor_variable = dplyr::everything(), two_way_interaction_factor = c(Sepal.Width, Species), interaction_plot = FALSE, # you can also request the interaction plot simple_slope = FALSE, # you can also request simple slope estimate assumption_plot = FALSE, # you can also request assumption plot streamline = FALSE #you can change this to get the least amount of info )
fit <- lm_model_summary( data = iris, response_variable = "Sepal.Length", predictor_variable = dplyr::everything(), two_way_interaction_factor = c(Sepal.Width, Species), interaction_plot = FALSE, # you can also request the interaction plot simple_slope = FALSE, # you can also request simple slope estimate assumption_plot = FALSE, # you can also request assumption plot streamline = FALSE #you can change this to get the least amount of info )
Generate tables with multiple response, predictor, or two-way interaction variables (only lm
models are supported).
You can pass multiple variables for one type of variable (either response, pred, or interaction) only.
If you want to pass multiple variables for multiple type of variable, try lm_model_explore instead.
At the moment, multi-categorical variables are not supported as predictors or interactions (but control is fine). Binary variable should be numeric
instead of factor
lm_model_table( data, response_variable, predictor_variable, two_way_interaction_variable = NULL, control_variable = NULL, other_parameters = NULL, marginal_alpha = 0.1, return_result = FALSE, verbose = TRUE, show_p = FALSE )
lm_model_table( data, response_variable, predictor_variable, two_way_interaction_variable = NULL, control_variable = NULL, other_parameters = NULL, marginal_alpha = 0.1, return_result = FALSE, verbose = TRUE, show_p = FALSE )
data |
|
response_variable |
response variable. Support |
predictor_variable |
predictor variable. Support |
two_way_interaction_variable |
Two-way interaction variable. Each two-way interaction variable will interact with the predictor variable. Support |
control_variable |
control variables. Support |
other_parameters |
catch call for all other parameters that need to be entered (e.g., non-changing interaction terms). Have to be |
marginal_alpha |
the set marginal_alpha level for marginally significant (denoted by |
return_result |
It set to |
verbose |
default is |
show_p |
show the p-value in parenthesis |
data.frame
# If you want all varibles to be changing, try lm_model_explore. test = data.frame(y1 = rnorm(1000,2,3), y2 = rnorm(1000,10,2), y3 = rnorm(1000,1,4), x1 = rnorm(1000,100,10), x2 = rnorm(1000,10,1), x3 = rnorm(1000,6,2), m1 = rnorm(1000,3,1), m2 = rnorm(1000,2,0.5), m3 = rnorm(1000,9,0.1), c1 = rnorm(1000,5,0.4), c2 = rnorm(1000,2,0.2), c3 = rnorm(1000,7,0.9) ) # Changing response variable lm_model_table(data = test, response_variable = c(y1,y2,y3), predictor_variable = x1, control_variable = c(c1,c2,c3)) # Changing predictors lm_model_table(data = test, response_variable = y1, predictor_variable = c(x1,x2,x3), control_variable = c(c1,c2,c3)) # Changing interaction terms with a non-changing response variable lm_model_table(data = test, response_variable = y1, predictor_variable = x1, two_way_interaction_variable = c(m1,m2,m3), control_variable = c(c1,c2,c3)) # A non-changing interaction term with changing response variables lm_model_table(data = test, response_variable = c(y1,y2,y3), predictor_variable = x1, other_parameters = c('x1*m1'), control_variable = c(c1,c2,c3))
# If you want all varibles to be changing, try lm_model_explore. test = data.frame(y1 = rnorm(1000,2,3), y2 = rnorm(1000,10,2), y3 = rnorm(1000,1,4), x1 = rnorm(1000,100,10), x2 = rnorm(1000,10,1), x3 = rnorm(1000,6,2), m1 = rnorm(1000,3,1), m2 = rnorm(1000,2,0.5), m3 = rnorm(1000,9,0.1), c1 = rnorm(1000,5,0.4), c2 = rnorm(1000,2,0.2), c3 = rnorm(1000,7,0.9) ) # Changing response variable lm_model_table(data = test, response_variable = c(y1,y2,y3), predictor_variable = x1, control_variable = c(c1,c2,c3)) # Changing predictors lm_model_table(data = test, response_variable = y1, predictor_variable = c(x1,x2,x3), control_variable = c(c1,c2,c3)) # Changing interaction terms with a non-changing response variable lm_model_table(data = test, response_variable = y1, predictor_variable = x1, two_way_interaction_variable = c(m1,m2,m3), control_variable = c(c1,c2,c3)) # A non-changing interaction term with changing response variables lm_model_table(data = test, response_variable = c(y1,y2,y3), predictor_variable = x1, other_parameters = c('x1*m1'), control_variable = c(c1,c2,c3))
Exploratory analyses for linear regression models with multiple response, predictor, and two-way interaction variables. (lmer
models).
At the moment, multi-categorical variables are not supported as predictors or interactions (but control is fine). Binary variable should be numeric
instead of factor
This function also do not supports changing random slopes.
lme_model_explore( ..., data, response_variable, predictor_variable, two_way_interaction_variable = NULL, three_way_interaction_variable = NULL, random_effect, control_variable = NULL, marginal_alpha = 0.1, return_result = FALSE, print_control = FALSE, verbose = TRUE, show_p = TRUE, show_formula = FALSE )
lme_model_explore( ..., data, response_variable, predictor_variable, two_way_interaction_variable = NULL, three_way_interaction_variable = NULL, random_effect, control_variable = NULL, marginal_alpha = 0.1, return_result = FALSE, print_control = FALSE, verbose = TRUE, show_p = TRUE, show_formula = FALSE )
... |
additional parameters pass to lme4::lmer() |
data |
|
response_variable |
Response variable. Support |
predictor_variable |
Pred variable. Support |
two_way_interaction_variable |
Two-way interaction variable. Each two-way interaction variable will interact with each pred variable. Support |
three_way_interaction_variable |
Three-way interaction variable. Each three-way interaction variable will interact with each pred and two-way interaction variables. Support |
random_effect |
The random-effects terms in the format of |
control_variable |
Control variables. Support |
marginal_alpha |
Set marginal_alpha level for marginally significant (denoted by |
return_result |
Default is |
print_control |
Default is |
verbose |
Default is |
show_p |
Default is |
show_formula |
Default is |
data.frame
lme_model_explore(data = popular, response_variable = c(popular,extrav), predictor_variable = c(texp), two_way_interaction_variable = sex, random_effect = '(1 | class)')
lme_model_explore(data = popular, response_variable = c(popular,extrav), predictor_variable = c(texp), two_way_interaction_variable = sex, random_effect = '(1 | class)')
Generate tables with multiple response, predictor, or two-way interaction variables (only lmer
models are supported).
You can pass multiple variables for one type of variable (either response, pred, or interaction) only.
If you want to pass multiple variables for multiple type of variable, try lmer_model_explore instead.
At the moment, multi-categorical variables are not supported as predictors or interactions (but control is fine). Binary variable should be numeric
instead of factor
This function also do not supports changing random slopes.
Please use other_parameters
if you want to add non-changing interaction term.
lme_model_table( ..., data, response_variable, predictor_variable, two_way_interaction_variable = NULL, random_effect, control_variable = NULL, other_parameters = NULL, marginal_alpha = 0.1, return_result = FALSE, verbose = TRUE, show_p = FALSE )
lme_model_table( ..., data, response_variable, predictor_variable, two_way_interaction_variable = NULL, random_effect, control_variable = NULL, other_parameters = NULL, marginal_alpha = 0.1, return_result = FALSE, verbose = TRUE, show_p = FALSE )
... |
additional parameters pass to lmerTest::lmer() |
data |
|
response_variable |
response variable. Support |
predictor_variable |
predictor variable. Support |
two_way_interaction_variable |
Two-way interaction variable. Each two-way interaction variable will interact with the predictor variable. Support |
random_effect |
The random-effects terms in the format of |
control_variable |
control variables. Support |
other_parameters |
catch call for all other parameters that need to be entered (e.g., non-changing interaction terms). Have to be |
marginal_alpha |
the set marginal_alpha level for marginally significant (denoted by |
return_result |
It set to |
verbose |
default is |
show_p |
show the p-value in parenthesis |
data.frame
# If you want all varibles to be changing, try lmer_model_explore. # For more examples, see ?lm_model_table. # Changing interaction terms with a non-changing response variable lme_model_table(data = popular, response_variable = popular, predictor_variable = texp, two_way_interaction_variable = c(extrav,sex), random_effect = '(1 | class)') # A non-changing interaction term with changing response variables lme_model_table(data = popular, response_variable = c(popular,sex), predictor_variable = texp, other_parameters = 'texp*extrav', random_effect = '(1 | class)')
# If you want all varibles to be changing, try lmer_model_explore. # For more examples, see ?lm_model_table. # Changing interaction terms with a non-changing response variable lme_model_table(data = popular, response_variable = popular, predictor_variable = texp, two_way_interaction_variable = c(extrav,sex), random_effect = '(1 | class)') # A non-changing interaction term with changing response variables lme_model_table(data = popular, response_variable = c(popular,sex), predictor_variable = texp, other_parameters = 'texp*extrav', random_effect = '(1 | class)')
Compute the measurement invariance model (i.e., measurement equivalence model) using multi-group confirmatory factor analysis (MGCFA; Jöreskog, 1971). This function uses the lavaan::cfa() in the backend.
Users can run the configural-metric or the configural-metric-scalar comparisons (see below for detail instruction).
All arguments (except the CFA items) must be explicitly named (like model = your-model; see example for inappropriate behavior).
measurement_invariance( data, ..., model = NULL, group, ordered = FALSE, group_partial = NULL, invariance_level = "scalar", estimator = "ML", digits = 3, quite = FALSE, streamline = FALSE, return_result = FALSE )
measurement_invariance( data, ..., model = NULL, group, ordered = FALSE, group_partial = NULL, invariance_level = "scalar", estimator = "ML", digits = 3, quite = FALSE, streamline = FALSE, return_result = FALSE )
Chen (2007) suggested that change in CFI <= |-0.010| supplemented by RMSEA <= 0.015 indicate non-invariance when sample sizes were equal across groups and larger than 300 in each group (Chen, 2007). And, Chen (2007) suggested that change in CFI <= |-0.005| and change in RMSEA <= 0.010 for unequal sample size with each group smaller than 300. For SRMR, Chen (2007) recommend change in SRMR < 0.030 for metric-invariance and change in SRMR < 0.015 for scalar-invariance. For large group size, Rutowski & Svetina (2014) recommended a more liberal cut-off for metric non-invariance for CFI (change in CFI <= |-0.020|) and RMSEA (RMSEA <= 0.030). However, this more liberal cut-off DOES NOT apply to testing scalar non-invariance. If measurement-invariance is not achieved, some researchers suggesting partial invariance is acceptable (by releasing the constraints on some factors). For example, Steenkamp and Baumgartner (1998) suggested that ideally more than half of items on a factor should be invariant. However, it is important to note that no empirical studies were cited to support the partial invariance guideline (Putnick & Bornstein, 2016).
a data.frame
of the fit measure summary
Chen, F. F. (2007). Sensitivity of Goodness of Fit Indexes to Lack of Measurement Invariance. Structural Equation Modeling: A Multidisciplinary Journal, 14(3), 464–504. https://doi.org/10.1080/10705510701301834
Jöreskog, K. G. (1971). Simultaneous factor analysis in several populations. Psychometrika, 36(4), 409-426.
Putnick, D. L., & Bornstein, M. H. (2016). Measurement Invariance Conventions and Reporting: The State of the Art and Future Directions for Psychological Research. Developmental Review: DR, 41, 71–90. https://doi.org/10.1016/j.dr.2016.06.004
Rutkowski, L., & Svetina, D. (2014). Assessing the Hypothesis of Measurement Invariance in the Context of Large-Scale International Surveys. Educational and Psychological Measurement, 74(1), 31–57. https://doi.org/10.1177/0013164413498257
Steenkamp, J.-B. E. M., & Baumgartner, H. (n.d.). Assessing Measurement Invariance in Cross-National Consumer Research. JOURNAL OF CONSUMER RESEARCH, 13.
# REMEMBER, YOU MUST NAMED ALL ARGUMENT EXCEPT THE CFA ITEMS ARGUMENT # Fitting a multiple-factor measurement invariance model by passing items. measurement_invariance( x1:x3, x4:x6, x7:x9, data = lavaan::HolzingerSwineford1939, group = "school", invariance_level = "scalar" # you can change this to metric ) # Fitting measurement invariance model by passing explicit lavaan model # I am also going to only test for metric invariance instead of the default scalar invariance measurement_invariance( model = "visual =~ x1 + x2 + x3; textual =~ x4 + x5 + x6; speed =~ x7 + x8 + x9", data = lavaan::HolzingerSwineford1939, group = "school", invariance_level = "metric" ) ## Not run: # This will fail because I did not add `model = ` in front of the lavaan model. # Therefore,you must add the tag in front of all arguments # For example, `return_result = 'model'` instaed of `model` measurement_invariance( "visual =~ x1 + x2 + x3; textual =~ x4 + x5 + x6; speed =~ x7 + x8 + x9", data = lavaan::HolzingerSwineford1939 ) ## End(Not run)
# REMEMBER, YOU MUST NAMED ALL ARGUMENT EXCEPT THE CFA ITEMS ARGUMENT # Fitting a multiple-factor measurement invariance model by passing items. measurement_invariance( x1:x3, x4:x6, x7:x9, data = lavaan::HolzingerSwineford1939, group = "school", invariance_level = "scalar" # you can change this to metric ) # Fitting measurement invariance model by passing explicit lavaan model # I am also going to only test for metric invariance instead of the default scalar invariance measurement_invariance( model = "visual =~ x1 + x2 + x3; textual =~ x4 + x5 + x6; speed =~ x7 + x8 + x9", data = lavaan::HolzingerSwineford1939, group = "school", invariance_level = "metric" ) ## Not run: # This will fail because I did not add `model = ` in front of the lavaan model. # Therefore,you must add the tag in front of all arguments # For example, `return_result = 'model'` instaed of `model` measurement_invariance( "visual =~ x1 + x2 + x3; textual =~ x4 + x5 + x6; speed =~ x7 + x8 + x9", data = lavaan::HolzingerSwineford1939 ) ## End(Not run)
Mediation analysis A Monte Carlo simulation method to assess mediation based on Selig & Preacher (2008).
mediation( model_med, model_y, model_med2 = NULL, x, med, med2 = NULL, mod = NULL, mod_stage = NULL, mod_level = NULL, conf = 95, rep = 20000, verbose = TRUE, digits = 3 )
mediation( model_med, model_y, model_med2 = NULL, x, med, med2 = NULL, mod = NULL, mod_stage = NULL, mod_level = NULL, conf = 95, rep = 20000, verbose = TRUE, digits = 3 )
model_med |
a fitted model object for mediator. |
model_y |
a fitted model object for outcome |
model_med2 |
a fitted model object for the second mediator for serial mediation |
x |
a character string indicating the name of the independent variable used in the models. |
med |
a character string indicating the name of the mediator used in the models. |
med2 |
a character string indicating the name of the second mediator used in the models (for serial mediations) |
mod |
a character string indicating the name of the moderator used in the models. |
mod_stage |
a character string specifying the stage at which the moderating effect occurs. For instance, in a first-stage moderated mediation, where the moderator influences the effect of X on the mediator (Med), set this to "model_med". In a second-stage moderated mediation, where the moderator affects the relationship between the mediator (Med) and the outcome variable (Y), set this to "model_y".#' |
mod_level |
The default is -1 SD and +1 SD for a continuous variable, and it is the two levels for a binary variable. |
conf |
level of the returned two-sided confidence intervals. Default is to return the 2.5 and 97.5 percentiles of the simulated quantities (i.e., 95%). |
rep |
number of Monte Carlo draws |
verbose |
deafult is |
digits |
number of digits to round to |
Nothing to return. Print the indirect effect.
Selig, J. P., & Preacher, K. J. (2008, June). Monte Carlo method for assessing mediation: An interactive tool for creating confidence intervals for indirect effects. http://quantpsy.org/.
new_dat = iris %>% dplyr::rename(x = Petal.Length) %>% dplyr::rename(m = Sepal.Length) %>% dplyr::rename(moderator = Sepal.Width) %>% dplyr::rename(y = Petal.Width) model_1 = lm(data = new_dat, m ~ x) model_2 = lm(data = new_dat, y ~ x*moderator + m) mediation(model_med = model_1, model_y = model_2, rep = 20000, x = 'x', med = 'm', mod = 'moderator', mod_stage = 'model_y', digits = 3)
new_dat = iris %>% dplyr::rename(x = Petal.Length) %>% dplyr::rename(m = Sepal.Length) %>% dplyr::rename(moderator = Sepal.Width) %>% dplyr::rename(y = Petal.Width) model_1 = lm(data = new_dat, m ~ x) model_2 = lm(data = new_dat, y ~ x*moderator + m) mediation(model_med = model_1, model_y = model_2, rep = 20000, x = 'x', med = 'm', mod = 'moderator', mod_stage = 'model_y', digits = 3)
It currently only support simple mediation analysis using the path analysis approach with the lavaan
package. I am trying to implement multilevel mediation in lavaan
.
In the future, I will try supporting moderated mediation (through lavaan
or mediation
) and mediation with latent variable (through lavaan
).
mediation_summary( data, response_variable, mediator, predictor_variable, control_variable = NULL, group = NULL, standardize = TRUE, digits = 3, quite = FALSE, streamline = FALSE, return_result = FALSE )
mediation_summary( data, response_variable, mediator, predictor_variable, control_variable = NULL, group = NULL, standardize = TRUE, digits = 3, quite = FALSE, streamline = FALSE, return_result = FALSE )
an object from lavaan
mediation_summary( data = lmerTest::carrots, response_variable = Preference, mediator = Sweetness, predictor_variable = Crisp )
mediation_summary( data = lmerTest::carrots, response_variable = Preference, mediator = Sweetness, predictor_variable = Crisp )
The function will extract the relevant coefficients from the regression models (see below for supported model).
model_summary( model, digits = 3, assumption_plot = FALSE, quite = FALSE, streamline = TRUE, return_result = FALSE, standardize = NULL, ci_method = "satterthwaite" )
model_summary( model, digits = 3, assumption_plot = FALSE, quite = FALSE, streamline = TRUE, return_result = FALSE, standardize = NULL, ci_method = "satterthwaite" )
model |
an model object. The following model are tested for accuracy: |
digits |
number of digits to round to |
assumption_plot |
Generate an panel of plots that check major assumptions. It is usually recommended to inspect model assumption violation visually. In the background, it calls |
quite |
suppress printing output |
streamline |
print streamlined output. Only print model estimate and performance. |
return_result |
It set to |
standardize |
The method used for standardizing the parameters. Can be NULL (default; no standardization), "refit" (for re-fitting the model on standardized data) or one of "basic", "posthoc", "smart", "pseudo". See 'Details' in parameters::standardize_parameters() |
ci_method |
see options in the |
a list of model estimate data frame, model performance data frame, and the assumption plot (an ggplot
object)
Nakagawa, S., & Schielzeth, H. (2013). A general and simple method for obtaining R2 from generalized linear mixed-effects models. Methods in Ecology and Evolution, 4(2), 133–142. https://doi.org/10.1111/j.2041-210x.2012.00261.x
# I am going to show the more generic usage of this function # You can also use this package's built in function to fit the models # I recommend using the integrated_multilevel_model_summary to get everything # lme example lme_fit <- lme4::lmer("popular ~ texp + (1 | class)", data = popular ) model_summary(lme_fit) # lm example lm_fit <- lm(Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width, data = iris ) model_summary(lm_fit)
# I am going to show the more generic usage of this function # You can also use this package's built in function to fit the models # I recommend using the integrated_multilevel_model_summary to get everything # lme example lme_fit <- lme4::lmer("popular ~ texp + (1 | class)", data = popular ) model_summary(lme_fit) # lm example lm_fit <- lm(Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width, data = iris ) model_summary(lm_fit)
The function create a simple regression plot (no interaction). Can be used to visualize polynomial regression.
polynomial_regression_plot( model, model_data = NULL, predictor, graph_label_name = NULL, x_lim = NULL, y_lim = NULL, plot_color = FALSE )
polynomial_regression_plot( model, model_data = NULL, predictor, graph_label_name = NULL, x_lim = NULL, y_lim = NULL, plot_color = FALSE )
model |
object from |
model_data |
optional dataframe (in case data cannot be retrieved from the model) |
predictor |
predictor variable name (must be character) |
graph_label_name |
vector of length 3 or function. Vector should be passed in the form of |
x_lim |
the plot's upper and lower limit for the x-axis. Length of 2. Example: |
y_lim |
the plot's upper and lower limit for the y-axis. Length of 2. Example: |
plot_color |
default if |
It appears that predict
cannot handle categorical factors. All variables are converted to numeric before plotting.
an object of class ggplot
fit = lm(data = iris, Sepal.Length ~ poly(Petal.Length,2)) polynomial_regression_plot(model = fit,predictor = 'Petal.Length')
fit = lm(data = iris, Sepal.Length ~ poly(Petal.Length,2)) polynomial_regression_plot(model = fit,predictor = 'Petal.Length')
Classic data-set from Chapter 2 of Joop Hox’s Multilevel Analysis (2010). The popular dataset included student from different class (i.e., class is the nesting variable). The outcome variable is a self-rated popularity scale. Individual-level (i.e., level 1) predictors are sex, extroversion. Class level (i.e., level 2) predictor is teacher experience.
popular
popular
A data frame with 2000 rows and 6 variables:
Subject ID
Self-rated popularity scale ranging from 1 to 10
the class that students belong to (nesting variable)
extraversion scale (individual-level)
gender of the student (individual-level)
teacher experience (class-level)
http://joophox.net/mlbook2/DataExchange.zip
First, it will determine whether the data is uni-dimensional or multi-dimensional using parameters::n_factors()
. If the data is uni-dimensional, then it will print a summary
consists of alpha, G6, single-factor CFA, and descriptive statistics result. If it is multi-dimensional, it will print a summary consist of alpha, G6, omega result. You can
bypass this by specifying the dimensionality argument.
reliability_summary( data, cols, dimensionality = NULL, digits = 3, descriptive_table = TRUE, quite = FALSE, streamline = FALSE, return_result = FALSE )
reliability_summary( data, cols, dimensionality = NULL, digits = 3, descriptive_table = TRUE, quite = FALSE, streamline = FALSE, return_result = FALSE )
data |
|
cols |
items for reliability analysis. Support |
dimensionality |
Specify the dimensionality. Either |
digits |
number of digits to round to |
descriptive_table |
Get descriptive statistics. Default is |
quite |
suppress printing output |
streamline |
print streamlined output |
return_result |
If it is set to |
a psych::alpha
object for unidimensional scale, and a psych::omega
object for multidimensional scale.
fit <- reliability_summary(data = lavaan::HolzingerSwineford1939, cols = x1:x3) fit <- reliability_summary(data = lavaan::HolzingerSwineford1939, cols = x1:x9)
fit <- reliability_summary(data = lavaan::HolzingerSwineford1939, cols = x1:x3) fit <- reliability_summary(data = lavaan::HolzingerSwineford1939, cols = x1:x9)
The function uses the interaction::sim_slopes()
to calculate the slope estimate at varying level of moderators (+/- 1 SD and mean).
Additionally, it will produce a Johnson-Newman plot that shows when the slope estimate is not significant
simple_slope(model, data = NULL)
simple_slope(model, data = NULL)
model |
model object from |
data |
|
a list with the slope estimate data frame and a Johnson-Newman plot.
fit <- lm_model( data = iris, response_variable = Sepal.Length, predictor_variable = dplyr::everything(), three_way_interaction_factor = c(Sepal.Width, Petal.Width, Petal.Length) ) simple_slope_fit <- simple_slope( model = fit, )
fit <- lm_model( data = iris, response_variable = Sepal.Length, predictor_variable = dplyr::everything(), three_way_interaction_factor = c(Sepal.Width, Petal.Width, Petal.Length) ) simple_slope_fit <- simple_slope( model = fit, )
The function creates a three-way interaction plot. By default, it will create an interaction plot with -1 SD and +1 SD of the two continuous variables, or the two levels of the binary variables or dummy-coded multi-categorical variable
It has been superseded by interaction_plot
.
three_way_interaction_plot( model, interaction_term = NULL, response_var_name = NULL, predict_var1_name = NULL, predict_var2_name = NULL, predict_var3_name = NULL, predict_var1_level = NULL, predict_var2_level = NULL, predict_var3_level = NULL, predict_var1_level_name = NULL, predict_var2_level_name = NULL, predict_var3_level_name = NULL, y_lim = NULL, plot_color = FALSE, return_plot_data = FALSE, return_plot = FALSE, verbose = TRUE, print_plot = TRUE, data = NULL )
three_way_interaction_plot( model, interaction_term = NULL, response_var_name = NULL, predict_var1_name = NULL, predict_var2_name = NULL, predict_var3_name = NULL, predict_var1_level = NULL, predict_var2_level = NULL, predict_var3_level = NULL, predict_var1_level_name = NULL, predict_var2_level_name = NULL, predict_var3_level_name = NULL, y_lim = NULL, plot_color = FALSE, return_plot_data = FALSE, return_plot = FALSE, verbose = TRUE, print_plot = TRUE, data = NULL )
model |
a regression model object from effect. |
interaction_term |
default is the first interaction term in the model. The term should be given explicitly if you want to plot other interaction terms. |
response_var_name |
The name of the response variable can be changed using this setting. |
predict_var1_name |
The name of the first predictor can be changed using this setting. |
predict_var2_name |
The name of the second predictor can be changed using this setting. |
predict_var3_name |
The name of the third predictor can be changed using this setting. |
predict_var1_level |
The default is -1 SD and +1 SD for a continuous variable, and it is the two levels for a binary variable. These can be changed using this setting. |
predict_var2_level |
The default is -1 SD and +1 SD for a continuous variable, and it is the two levels for a binary variable. These can be changed using this setting. |
predict_var3_level |
The default is -1 SD and +1 SD for a continuous variable, and it is the two levels for a binary variable. These can be changed using this setting. |
predict_var1_level_name |
The labels of the level can be change using this value (e.g., |
predict_var2_level_name |
The labels of the level can be change using this value (e.g., |
predict_var3_level_name |
The labels of the level can be change using this value (e.g., |
y_lim |
the plot's upper and lower limit for the y-axis. Length of 2. Example: |
plot_color |
default if |
return_plot_data |
default is |
return_plot |
default is |
verbose |
deafult is |
print_plot |
default is |
data |
Optional data.frame. Only used when it is not possible to extract data from the model object. |
an object of class ggplot
The function creates a three-way interaction plot. By default, it will create an interaction plot with -1 SD and +1 SD of the two continuous variables, or the two levels of the binary variables or dummy-coded multi-categorical variable
It has been superseded by interaction_plot
.
two_way_interaction_plot( model, interaction_term = NULL, response_var_name = NULL, predict_var1_name = NULL, predict_var2_name = NULL, predict_var1_level = NULL, predict_var2_level = NULL, predict_var1_level_name = NULL, predict_var2_level_name = NULL, y_lim = NULL, plot_color = FALSE, return_plot_data = FALSE, return_plot = FALSE, verbose = FALSE, print_plot = TRUE, data = NULL )
two_way_interaction_plot( model, interaction_term = NULL, response_var_name = NULL, predict_var1_name = NULL, predict_var2_name = NULL, predict_var1_level = NULL, predict_var2_level = NULL, predict_var1_level_name = NULL, predict_var2_level_name = NULL, y_lim = NULL, plot_color = FALSE, return_plot_data = FALSE, return_plot = FALSE, verbose = FALSE, print_plot = TRUE, data = NULL )
model |
a regression model object from effect. |
interaction_term |
default is the first interaction term in the model. The term should be given explicitly if you want to plot other interaction terms. |
response_var_name |
The name of the response variable can be changed using this value. |
predict_var1_name |
The name of the first predictor can be changed using this value. |
predict_var2_name |
The name of the second predictor can be changed using this value. |
predict_var1_level |
default is the -1 SD and +1 SD for continuous variable. They can be changed using this value. |
predict_var2_level |
default is the -1 SD and +1 SD for continuous variable. They can be changed using this value. |
predict_var1_level_name |
The labels of the level can be change using this value (e.g., |
predict_var2_level_name |
The labels of the level can be change using this value (e.g., |
y_lim |
the plot's upper and lower limit for the y-axis. Length of 2. Example: |
plot_color |
default if |
return_plot_data |
default is |
return_plot |
default is |
verbose |
deafult is |
print_plot |
default is |
data |
Optional data.frame. Only used when it is not possible to extract data from the model object. |
It appears that “predict' cannot handle categorical factors. All variables are converted to numeric before plotting.
an object of class ggplot