/
pathway.R
122 lines (119 loc) · 4.43 KB
/
pathway.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#' Pathway case
#'
#' Calculation of pathway values, defined as the difference between residuals of
#' full model and reduced model lacking the pathway variable. The larger the
#' difference, the more a case qualifies as a pathway case suitable for the
#' analysis of mechanisms.
#'
#' The difference between the absolute residuals of the full and reduced model
#' follows the approach developed by Weller and Barnes (2014): \emph{Finding
#' Pathways: Mixed-Method Research for Studying Causal Mechanisms.}
#' Cambridge: Cambridge University Press.
#' \url{https://doi.org/10.1017/CBO9781139644501}).
#'
#' The calculation of the absolute difference between the full-model and
#' reduced-model residuals, given a case's reduced-model residual is larger
#' than its full-model residual, follows the proposal by
#' Gerring (2007): Is There a (Viable) Crucial-Case Method?
#' \emph{Comparative Political Studies} 40 (3): 231-253.
#' \url{https://journals.sagepub.com/doi/10.1177/0010414006290784})
#'
#' @param full_model Full model including covariate of interest
#' (= pathway variable)
#' @param reduced_model Reduced model excluding covariate of interest
#'
#' @return A dataframe with
#'
#' - all full model variables,
#'
#' - full model residuals (\code{full_resid}),
#'
#' - reduced model residuals (\code{reduced_resid}),
#'
#' - pathway values following Weller/Barnes (\code{pathway_wb}),
#'
#' - pathway values following Gerring (\code{pathway_gvalue}),
#'
#' - variable showing whether Gerring's criterion for a pathway
#' case is met (\code{pathway_gstatus})
#'
#' @importFrom stats lm residuals
#'
#' @examples
#' df_full <- lm(mpg ~ disp + wt, data = mtcars)
#' df_reduced <- lm(mpg ~ wt, data = mtcars)
#' pathway(df_full, df_reduced)
#'
#' @export
pathway <- function(full_model, reduced_model) {
if (class(full_model) == "lm") {
if (class(reduced_model) == "lm") {
# full model
full_resid <- residuals(full_model)
# reduced model
reduced_resid <- residuals(reduced_model)
# difference between absolute residuals
pathway_wb <- abs(reduced_resid) - abs(full_resid)
# absolute difference between residuals
pathway_gvalue <- abs(reduced_resid - full_resid)
# check for Gerring's criterion for pathway values
pathway_gtype <- ifelse(abs(reduced_resid) > abs(full_resid), "yes", "no")
comb <- cbind(full_model$model, full_resid, reduced_resid,
pathway_wb, pathway_gvalue, pathway_gtype)
return(comb)
}
else{
stop("Reduced model object is not of class lm")
}
}
else{
(stop("Full model object is not of class lm"))
}
}
#' Plot of residuals against pathway variable
#'
#' @param full_model Full model including covariate of interest
#' (= pathway variable)
#' @param reduced_model Reduced model excluding covariate of interest
#' @param pathway_type Type of pathway values. \code{pathway_wb} are
#' pathway values proposed by Weller and Barnes. \code{pathway_gvalue}
#' are values as calculated by Gerring.
#'
#' @return A plot of the chosen type of pathway values against the pathway
#' variable created with \code{\link{ggplot2}}.
#'
#' @import ggplot2
#'
#' @examples
#' df_full <- lm(mpg ~ disp + wt, data = mtcars)
#' df_reduced <- lm(mpg ~ wt, data = mtcars)
#' pathway_xvr(df_full, df_reduced, pathway_type = "pathway_wb")
#'
#' @export
pathway_xvr <- function(full_model, reduced_model, pathway_type) {
pwdf <- pathway(full_model, reduced_model)
if (pathway_type == "pathway_wb") {
pwplot <- ggplot2::ggplot() +
geom_point(data = pwdf,
mapping = aes_string(x = setdiff(names(full_model$model),
names(reduced_model$model)),
y = pathway_type)) +
geom_hline(yintercept = 0, linetype = 5) +
scale_y_continuous("Pathway values") +
theme_classic() -> pwplot
}
else{
pwplot <- ggplot2::ggplot() +
geom_point(data = pwdf,
mapping = aes_string(x = setdiff(names(full_model$model),
names(reduced_model$model)),
y = pathway_type,
color = "pathway_gtype")) +
geom_hline(yintercept = 0, linetype = 5) +
scale_y_continuous("Pathway values") +
scale_color_viridis_d("Reduced > full residuals") +
theme_classic() +
theme(legend.position = "bottom") -> pwplot
}
return(pwplot)
}