-
Notifications
You must be signed in to change notification settings - Fork 2
/
miscvisualize.R
100 lines (86 loc) · 3.42 KB
/
miscvisualize.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
# miscvisualize.R
local({
tmp_require_package_namespace <- function(...) {
packages <- as.character(match.call(expand.dots = FALSE)[[2]])
for (p in packages) if (!requireNamespace(p)) install.packages(p)
}
tmp_require_package_namespace(
arm,
ggplot2
)
})
#==============================================================================
# Namespace-like method: http://stackoverflow.com/questions/1266279/#1319786
#==============================================================================
miscvisualize <- new.env()
#==============================================================================
# Visualize regression output from an lme4 logistic regression
#==============================================================================
# SEE ALSO https://rpubs.com/mcgill_linguistics/63173, for visualization
# of logistic regression with multiple factors.
miscvisualize$visualize_regression_predictions_1cov_1factor <- function(
model, # lme4 model
factorname,
xvarname,
constants,
num_x_values = 200,
inverse_link = arm::invlogit,
ylab_link = "logit",
ylab_response = "p"
) {
fulldata <- model@frame
# ?predict.merMod
# methods(class = "merMod")
# We're going to make a grid of values for the prediction, using
# expand.grid. That works like:
# expand.grid(a = c(1, 2), b = c(3, 4))
# expand.grid(list(a = c(1, 2), b = c(3, 4)))
# We'll use the latter form.
# But to make a named list, arbitrarily...
# Start with the constants.
expandables <- as.list(constants)
expandable_names <- names(constants)
# Add in the factors
expandables[[length(expandables) + 1]] <- unique(fulldata[, factorname])
expandable_names <- c(expandable_names, factorname)
# Add in the variable
xvar <- fulldata[, xvarname]
expandables[[length(expandables) + 1]] <- seq(min(xvar), max(xvar),
length.out = num_x_values)
expandable_names <- c(expandable_names, xvarname)
# Name our expandables
names(expandables) <- expandable_names
# We don't need to bother with random effects, since we will turn off
# predictions relating to those.
# Now expand!
newdata <- expand.grid(expandables)
# Now predict!
newdata <- within(newdata, {
predicted_link <- predict(model, newdata, re.form = NA)
predicted_response <- inverse_link(predicted_link)
})
# Now plot!
link_plot <- (
ggplot(newdata, aes_string(x = xvarname,
y = "predicted_link",
colour = factorname))
+ geom_line()
+ ylab(ylab_link)
)
response_plot <- (
ggplot(newdata, aes_string(x = xvarname,
y = "predicted_response",
colour = factorname))
+ geom_line()
+ ylab(ylab_response)
)
return(list(
link_plot = link_plot,
response_plot = response_plot
))
}
#==============================================================================
# Namespace-like method: http://stackoverflow.com/questions/1266279/#1319786
#==============================================================================
if ("miscvisualize" %in% search()) detach("miscvisualize")
attach(miscvisualize) # subsequent additions not found, so attach at the end