2 |
3 | [](https://cran.r-project.org/package=interplot) 
4 |
5 | ------------------------------------------------------------------------
6 | interplot
7 | =========
8 |
9 | `interplot` is a tool for plotting the conditional coefficients ("marginal effects") of variables included in multiplicative interaction terms. The function plots the changes in the coefficient of one variable in a two-way interaction term conditional on the value of the other included variable. The plot also includes simulated 95% confidential intervals of these coefficients.
10 |
11 | To install:
12 |
13 | * the latest released version: `install.packages("interplot")`.
14 | * the latest developing version: `remotes::install_github("sammo3182/interplot")`.
15 |
16 |
17 | More details are available at:
18 |
19 | http://cran.r-project.org/web/packages/interplot/vignettes/interplot-vignette.html
20 |
21 |
22 | Please note that this project is released with a [Contributor Code of Conduct](https://github.com/sammo3182/interplot/blob/master/CONDUCT.md). By participating in this project you agree to abide by its terms.
23 |
--------------------------------------------------------------------------------
/CONDUCT.md:
--------------------------------------------------------------------------------
1 | # Contributor Code of Conduct
2 |
3 | As contributors and maintainers of this project, we pledge to respect all people who
4 | contribute through reporting issues, posting feature requests, updating documentation,
5 | submitting pull requests or patches, and other activities.
6 |
7 | We are committed to making participation in this project a harassment-free experience for
8 | everyone, regardless of level of experience, gender, gender identity and expression,
9 | sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
10 |
11 | Examples of unacceptable behavior by participants include the use of sexual language or
12 | imagery, derogatory comments or personal attacks, trolling, public or private harassment,
13 | insults, or other unprofessional conduct.
14 |
15 | Project maintainers have the right and responsibility to remove, edit, or reject comments,
16 | commits, code, wiki edits, issues, and other contributions that are not aligned to this
17 | Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed
18 | from the project team.
19 |
20 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by
21 | opening an issue or contacting one or more of the project maintainers.
22 |
23 | This Code of Conduct is adapted from the Contributor Covenant
24 | (http:contributor-covenant.org), version 1.0.0, available at
25 | http://contributor-covenant.org/version/1/0/0/
26 |
--------------------------------------------------------------------------------
/dev/runBeforeRelease.R:
--------------------------------------------------------------------------------
1 | # Check List for A New Release
2 |
3 | ## Vignette building
4 |
5 | devtools::install(build_vignettes = TRUE)
6 |
7 | ## Spell checking
8 |
9 | library(devtools)
10 | library(roxygen2)
11 |
12 | spell_check()
13 |
14 | rhub::validate_email(email = "yuehu@tsinghua.edu.cn", token = "345011f4ca60404abf76007e9ae89e3e")
15 |
16 | check_rhub(email = "yuehu@tsinghua.edu.cn")
17 |
18 |
19 | ## Run the CRAN check
20 | # 1. Under the `Build` tab
21 | # 1. Fixed all the errors and warnings
22 |
23 | ## Documentation
24 |
25 | # 1. Update the version number in `DESCRIPTION`
26 | # 1. Update `NEWS`
27 |
28 | ## Removing the htmls to release space
29 |
30 | library(here)
31 |
32 | ls_html <-
33 | list.files(here("inst/tutorials"),
34 | pattern = "*.html$",
35 | recursive = TRUE,
36 | full.names = TRUE)
37 |
38 | file.remove(ls_html)
39 |
40 | ## Release
41 |
42 | devtools::release()
43 |
44 | ## Change the version number in `DESCRIPTION` back to the dev version (i.e., "9999")
45 |
46 | ## Post the Release note when the package is on CRAN
47 |
48 |
49 | ## (No need if the package already has a website) Package website building
50 | library(devtools)
51 |
52 | usethis::git_vaccinate() #Adds .DS_Store, .Rproj.user, .Rdata, .Rhistory, and .httr-oauth to your global (a.k.a. user-level) .gitignore. This is good practice as it decreases the chance that you will accidentally leak credentials to GitHub.
53 |
54 | use_github_links()
55 | use_github_action_check_standard()
56 |
57 | library(pkgdown)
58 | build_site()
59 |
--------------------------------------------------------------------------------
/docs/bootstrap-toc.css:
--------------------------------------------------------------------------------
1 | /*!
2 | * Bootstrap Table of Contents v0.4.1 (http://afeld.github.io/bootstrap-toc/)
3 | * Copyright 2015 Aidan Feldman
4 | * Licensed under MIT (https://github.com/afeld/bootstrap-toc/blob/gh-pages/LICENSE.md) */
5 |
6 | /* modified from https://github.com/twbs/bootstrap/blob/94b4076dd2efba9af71f0b18d4ee4b163aa9e0dd/docs/assets/css/src/docs.css#L548-L601 */
7 |
8 | /* All levels of nav */
9 | nav[data-toggle='toc'] .nav > li > a {
10 | display: block;
11 | padding: 4px 20px;
12 | font-size: 13px;
13 | font-weight: 500;
14 | color: #767676;
15 | }
16 | nav[data-toggle='toc'] .nav > li > a:hover,
17 | nav[data-toggle='toc'] .nav > li > a:focus {
18 | padding-left: 19px;
19 | color: #563d7c;
20 | text-decoration: none;
21 | background-color: transparent;
22 | border-left: 1px solid #563d7c;
23 | }
24 | nav[data-toggle='toc'] .nav > .active > a,
25 | nav[data-toggle='toc'] .nav > .active:hover > a,
26 | nav[data-toggle='toc'] .nav > .active:focus > a {
27 | padding-left: 18px;
28 | font-weight: bold;
29 | color: #563d7c;
30 | background-color: transparent;
31 | border-left: 2px solid #563d7c;
32 | }
33 |
34 | /* Nav: second level (shown on .active) */
35 | nav[data-toggle='toc'] .nav .nav {
36 | display: none; /* Hide by default, but at >768px, show it */
37 | padding-bottom: 10px;
38 | }
39 | nav[data-toggle='toc'] .nav .nav > li > a {
40 | padding-top: 1px;
41 | padding-bottom: 1px;
42 | padding-left: 30px;
43 | font-size: 12px;
44 | font-weight: normal;
45 | }
46 | nav[data-toggle='toc'] .nav .nav > li > a:hover,
47 | nav[data-toggle='toc'] .nav .nav > li > a:focus {
48 | padding-left: 29px;
49 | }
50 | nav[data-toggle='toc'] .nav .nav > .active > a,
51 | nav[data-toggle='toc'] .nav .nav > .active:hover > a,
52 | nav[data-toggle='toc'] .nav .nav > .active:focus > a {
53 | padding-left: 28px;
54 | font-weight: 500;
55 | }
56 |
57 | /* from https://github.com/twbs/bootstrap/blob/e38f066d8c203c3e032da0ff23cd2d6098ee2dd6/docs/assets/css/src/docs.css#L631-L634 */
58 | nav[data-toggle='toc'] .nav > .active > ul {
59 | display: block;
60 | }
61 |
--------------------------------------------------------------------------------
/docs/docsearch.js:
--------------------------------------------------------------------------------
1 | $(function() {
2 |
3 | // register a handler to move the focus to the search bar
4 | // upon pressing shift + "/" (i.e. "?")
5 | $(document).on('keydown', function(e) {
6 | if (e.shiftKey && e.keyCode == 191) {
7 | e.preventDefault();
8 | $("#search-input").focus();
9 | }
10 | });
11 |
12 | $(document).ready(function() {
13 | // do keyword highlighting
14 | /* modified from https://jsfiddle.net/julmot/bL6bb5oo/ */
15 | var mark = function() {
16 |
17 | var referrer = document.URL ;
18 | var paramKey = "q" ;
19 |
20 | if (referrer.indexOf("?") !== -1) {
21 | var qs = referrer.substr(referrer.indexOf('?') + 1);
22 | var qs_noanchor = qs.split('#')[0];
23 | var qsa = qs_noanchor.split('&');
24 | var keyword = "";
25 |
26 | for (var i = 0; i < qsa.length; i++) {
27 | var currentParam = qsa[i].split('=');
28 |
29 | if (currentParam.length !== 2) {
30 | continue;
31 | }
32 |
33 | if (currentParam[0] == paramKey) {
34 | keyword = decodeURIComponent(currentParam[1].replace(/\+/g, "%20"));
35 | }
36 | }
37 |
38 | if (keyword !== "") {
39 | $(".contents").unmark({
40 | done: function() {
41 | $(".contents").mark(keyword);
42 | }
43 | });
44 | }
45 | }
46 | };
47 |
48 | mark();
49 | });
50 | });
51 |
52 | /* Search term highlighting ------------------------------*/
53 |
54 | function matchedWords(hit) {
55 | var words = [];
56 |
57 | var hierarchy = hit._highlightResult.hierarchy;
58 | // loop to fetch from lvl0, lvl1, etc.
59 | for (var idx in hierarchy) {
60 | words = words.concat(hierarchy[idx].matchedWords);
61 | }
62 |
63 | var content = hit._highlightResult.content;
64 | if (content) {
65 | words = words.concat(content.matchedWords);
66 | }
67 |
68 | // return unique words
69 | var words_uniq = [...new Set(words)];
70 | return words_uniq;
71 | }
72 |
73 | function updateHitURL(hit) {
74 |
75 | var words = matchedWords(hit);
76 | var url = "";
77 |
78 | if (hit.anchor) {
79 | url = hit.url_without_anchor + '?q=' + escape(words.join(" ")) + '#' + hit.anchor;
80 | } else {
81 | url = hit.url + '?q=' + escape(words.join(" "));
82 | }
83 |
84 | return url;
85 | }
86 |
--------------------------------------------------------------------------------
/dev/interplot_test.R:
--------------------------------------------------------------------------------
1 | library(mi)
2 | library(mitools)
3 | library(Amelia)
4 |
5 | p2005 <- read_sav("../Meritocracy_Rep/data/pew/merit/version_a/Dec05/Dec05c.sav")
6 | p2005x <- data.frame(
7 | resp = p2005$resp,
8 | fips = as.numeric(p2005$qfips),
9 | state = floor(as.numeric(p2005$qfips)/1000),
10 | rej_merit = ifelse(p2005$q14k<=2, 0, ifelse(p2005$q14k>4, NA, 1)),
11 | income = ifelse(p2005$income<=9, p2005$income, NA), # 1 to 9
12 | educ = ifelse(p2005$educ<=7, p2005$educ, NA), # 1 to 7
13 | age = ifelse(p2005$age<99, p2005$age, NA),
14 | male = ifelse(p2005$sex==1, 1, 0),
15 | white = ifelse(p2005$race==1 & p2005$hisp!=1, 1, 0),
16 | union = ifelse(p2005$labor<=3, 1, ifelse(p2005$labor==4, 0, NA)),
17 | ideo = 6 - ifelse(p2005$ideo<=5, p2005$ideo, NA), # 1 to 5
18 | attend = 7 - ifelse(p2005$attend<=6, p2005$attend, NA) # 1 to 6
19 | )
20 |
21 | m1 <- glm(rej_merit ~ income*educ + age, data = p2005x, family = binomial(link = "logit"))
22 | interplot(m1, "income", "educ")
23 |
24 | p2005x_mi <- missing_data.frame(p2005x)
25 | p2005x_mi <- change_type(p2005x_mi, y = "resp", to = "irrelevant")
26 | p2005x_mi <- change_type(p2005x_mi, y = "fips", to = "irrelevant")
27 | p2005x_mi <- change_type(p2005x_mi, y = "state", to = "irrelevant")
28 | p2005x_mi <- change_type(p2005x_mi, y = "income", to = "ordered-categorical")
29 | p2005x_mi <- change_type(p2005x_mi, y = "educ", to = "ordered-categorical")
30 | p2005x_mi <- change_type(p2005x_mi, y = "attend", to = "ordered-categorical")
31 |
32 | ptm <- proc.time()
33 | p2005x_im <- mi(p2005x_mi, n.chains=8, n.iter=30, seed=324, max.minutes=60)
34 | proc.time() - ptm
35 |
36 | m2_mi <- pool(formula = rej_merit ~ as.numeric(income) * as.numeric(educ) + age,
37 | family=binomial(link="logit"),
38 | data = p2005x_im)
39 | # class "pooled" should be implemented
40 |
41 | p2005x_mi_list <- complete(p2005x_im, m = 10)
42 | p2005x_mi_list2 <- p2005x_mi_list
43 | for (i in 1:10) {
44 | p2005x_mi_list2[[i]]$income <- as.numeric(p2005x_mi_list[[i]]$income)
45 | p2005x_mi_list2[[i]]$educ <- as.numeric(p2005x_mi_list[[i]]$educ)
46 | p2005x_mi_list2[[i]]$attend <- as.numeric(p2005x_mi_list[[i]]$attend)
47 | }
48 | p2005x_mi_list3 <- imputationList(p2005x_mi_list2)
49 |
50 |
51 | m2_mitools <- with(p2005x_mi_list3,
52 | glm(formula = rej_merit ~ income * educ + age,
53 | family=binomial(link="logit")))
54 | interplot(m2_mitools, "income", "educ")
55 |
56 | m3_mitools <- with(p2005x_mi_list3,
57 | glmer(formula = rej_merit~income * educ + age +
58 | (1+income|state), family=binomial(link="logit")))
59 | interplot(m3_mitools, "income", "educ")
60 |
61 |
62 | a <- amelia(p2005x, m = 5, ords = 4) # lgstc doesn't seem to work
63 | a2 <- imputationList(a$imputations)
64 |
65 | m4_amelia <- with(a2, glm(formula = rej_merit ~ income * educ + age,
66 | family=binomial(link="logit")))
67 | interplot(a4, "income", "educ")
--------------------------------------------------------------------------------
/docs/pkgdown.js:
--------------------------------------------------------------------------------
1 | /* http://gregfranko.com/blog/jquery-best-practices/ */
2 | (function($) {
3 | $(function() {
4 |
5 | $('.navbar-fixed-top').headroom();
6 |
7 | $('body').css('padding-top', $('.navbar').height() + 10);
8 | $(window).resize(function(){
9 | $('body').css('padding-top', $('.navbar').height() + 10);
10 | });
11 |
12 | $('[data-toggle="tooltip"]').tooltip();
13 |
14 | var cur_path = paths(location.pathname);
15 | var links = $("#navbar ul li a");
16 | var max_length = -1;
17 | var pos = -1;
18 | for (var i = 0; i < links.length; i++) {
19 | if (links[i].getAttribute("href") === "#")
20 | continue;
21 | // Ignore external links
22 | if (links[i].host !== location.host)
23 | continue;
24 |
25 | var nav_path = paths(links[i].pathname);
26 |
27 | var length = prefix_length(nav_path, cur_path);
28 | if (length > max_length) {
29 | max_length = length;
30 | pos = i;
31 | }
32 | }
33 |
34 | // Add class to parent YEAR: 2015 60 | COPYRIGHT HOLDER: Frederick Solt, Yue HU 61 |62 | 63 |
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
62 |We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
63 |Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
64 |Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
65 |Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
66 |This Code of Conduct is adapted from the Contributor Covenant (http:contributor-covenant.org), version 1.0.0, available at http://contributor-covenant.org/version/1/0/0/
67 |
60 | All functions61 | 62 | |
63 | |
|---|---|
| 64 | 65 | | 66 |Plot Conditional Coefficients in (Generalized) Linear Models with Interaction Terms |
67 |
| 68 | 69 | | 70 |Plot Conditional Coefficients in Mixed-Effects Models with Interaction Terms |
71 |
| 72 | 73 | | 74 |Plot Conditional Coefficients in (Generalized) Linear Models with Imputed Data and Interaction Terms |
75 |
| 76 | 77 | | 78 |Plot Conditional Coefficients in Mixed-Effects Models with Imputed Data and Interaction Terms |
79 |
| 80 | 81 | | 82 |Plot Conditional Coefficients in Models with Interaction Terms |
83 |
| 84 | 85 | | 86 |Plot Conditional Coefficients of a Variable in an Interaction Term |
87 |
pkgdown 
interplot is a tool for plotting the conditional coefficients (“marginal effects”) of variables included in multiplicative interaction terms. The function plots the changes in the coefficient of one variable in a two-way interaction term conditional on the value of the other included variable. The plot also includes simulated 95% confidential intervals of these coefficients.
To install:
90 |install.packages("interplot").remotes::install_github("sammo3182/interplot").More details are available at:
95 |http://cran.r-project.org/web/packages/interplot/vignettes/interplot-vignette.html
96 |Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.
97 |R/Interplot_mlm.R
58 | interplot.lmerMod.Rdinterplot.mlm is a method to calculate conditional coefficient estimates from the results of multilevel (mixed-effects) regression models with interaction terms.
# S3 method for lmerMod
67 | interplot(
68 | m,
69 | var1,
70 | var2,
71 | plot = TRUE,
72 | steps = NULL,
73 | ci = 0.95,
74 | adjCI = FALSE,
75 | hist = FALSE,
76 | var2_dt = NA,
77 | predPro = FALSE,
78 | var2_vals = NULL,
79 | point = FALSE,
80 | sims = 5000,
81 | xmin = NA,
82 | xmax = NA,
83 | ercolor = NA,
84 | esize = 0.5,
85 | ralpha = 0.5,
86 | rfill = "grey70",
87 | stats_cp = "none",
88 | txt_caption = NULL,
89 | facet_labs = NULL,
90 | ...
91 | )A model object including an interaction term, or, alternately, a data frame recording conditional coefficients.
The name (as a string) of the variable of interest in the interaction term; its conditional coefficient estimates will be plotted.
The name (as a string) of the other variable in the interaction term.
A logical value indicating whether the output is a plot or a dataframe including the conditional coefficient estimates of var1, their upper and lower bounds, and the corresponding values of var2.
Desired length of the sequence. A non-negative number, which for seq and seq.int will be rounded up if fractional. The default is 100 or the unique categories in the var2 (when it is less than 100. Also see unique).
A numeric value defining the confidence intervals. The default value is 95% (0.95).
Not working for `lmer` outputs yet.
A logical value indicating if there is a histogram of `var2` added at the bottom of the conditional effect plot.
A numerical value indicating the frequency distribution of `var2`. It is only used when `hist == TRUE`. When the object is a model, the default is the distribution of `var2` of the model.
A logical value with default of `FALSE`. When the `m` is an object of class `glmerMod` and the argument is set to `TRUE`, the function will plot predicted probabilities at the values given by `var2_vals`.
A numerical value indicating the values the predicted probabilities are estimated, when `predPro` is `TRUE`.
A logical value determining the format of plot. By default, the function produces a line plot when var2 takes on ten or more distinct values and a point (dot-and-whisker) plot otherwise; option TRUE forces a point plot.
Number of independent simulation draws used to calculate upper and lower bounds of coefficient estimates: lower values run faster; higher values produce smoother curves.
A numerical value indicating the minimum value shown of x shown in the graph. Rarely used.
A numerical value indicating the maximum value shown of x shown in the graph. Rarely used.
A character value indicating the outline color of the whisker or ribbon.
A numerical value indicating the size of the whisker or ribbon.
A numerical value indicating the transparency of the ribbon.
A character value indicating the filling color of the ribbon.
A character value indicating what statistics to present as the plot note. Three options are available: "none", "ci", and "ks". The default is "none". See the Details for more information.
A character string to add a note for the plot, a value will sending to ggplot2::labs(caption = txt_caption)).
An optional character vector of facet labels to be used when plotting an interaction with a factor variable.
Other ggplot aesthetics arguments for points in the dot-whisker plot or lines in the line-ribbon plots. Not currently used.
The function returns a ggplot object.
interplot.mlm is a S3 method from the interplot. It works on mixed-effects objects with class lmerMod and glmerMod.
Because the output function is based on ggplot, any additional arguments and layers supported by ggplot2 can be added with the +.
interplot visualizes the conditional effect based on simulated marginal effects. The simulation provides a probabilistic distribution of moderation effect of the conditioning variable (var2) at every preset values (including the minimum and maximum values) of the conditioned variable (var1), denoted as Emin and Emax. This output allows the function to further examine the conditional effect statistically in two ways. One is to examine if the distribution of \(Emax - Emin\) covers zero. The other is to directly compare Emin and Emax through statistical tools for distributional comparisons. Users can choose either method by setting the argument stats_cp to "ci" or "ks".
"ci" provides the confidence interval of the difference of \(Emax - Emin\). An interval including 0 suggests no statistical difference before and after the conditional effect is applied, and vise versa.
"ks" presents the result of a two-sample Kolmogorov-Smirnov test of the simulated distributions of Emin and Emax. The output includes a D statistics and a p-value of the null hypothesis that the two distributions come from the same distribution at the 0.05 level.
See an illustration in the package vignette.
201 |