-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerics.R
89 lines (82 loc) · 2.63 KB
/
generics.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
#' @export
print.cvinereg <- function(x, ...) {
cat("C-vine regression model: ")
n_predictors <- length(x$order)
if (n_predictors <= 10) {
predictors <- paste(x$order, collapse = ", ")
} else {
predictors <- paste(x$order[1:10], collapse = ", ")
predictors <- paste0(predictors, ", ... (", n_predictors - 10, " more)")
}
cat(names(x$model_frame)[1], "|", predictors, "\n")
stats <- unlist(x$stats[1:5])
stats <- paste(names(stats), round(stats, 2), sep = " = ")
cat(paste(stats, collapse = ", "), "\n")
invisible(x)
}
#' @export
summary.cvinereg <- function(object, ...) {
data.frame(
var = c(names(object$model_frame)[1], object$order),
edf = object$stats$var_edf,
cll = object$stats$var_cll,
caic = object$stats$var_caic,
cbic = object$stats$var_cbic,
p_value = object$stats$var_p_value
)
}
#' Plot marginal effects of a C-vine regression model
#'
#' The marginal effects of a variable is the expected effect, where expectation
#' is meant with respect to all other variables.
#'
#' @param object a `cvinereg` object.
#' @param alpha vector of quantile levels.
#' @param vars vector of variable names.
#'
#' @examples
#' # simulate data
#' x <- matrix(rnorm(200), 100, 2)
#' y <- x %*% c(1, -2)
#' data <- data.frame(y = y, x = x)
#'
#' # fit vine regression model
#' fit <- cvinereg(y ~ ., data)
#'
#' # plot
#' plot_effects(fit)
#'
#' @export
plot_effects <- function(object,
alpha = c(0.1, 0.5, 0.9),
vars = object$order) {
if (!requireNamespace("ggplot2", quietly = TRUE)) {
stop("The 'ggplot2' package must be installed to plot.")
}
mf <- expand_factors(object$model_frame)
if (!all(vars %in% colnames(mf)[-1])) {
stop(
"unknown variable in 'vars'; allowed values: ",
paste(colnames(mf)[-1], collapse = ", ")
)
}
preds <- fitted(object, alpha)
preds <- lapply(seq_along(alpha), function(a)
cbind(data.frame(alpha = alpha[a], prediction = preds[[a]])))
preds <- do.call(rbind, preds)
df <- lapply(vars, function(var)
cbind(data.frame(var = var, value = as.numeric(unname(mf[, var])), preds)))
df <- do.call(rbind, df)
df$value <- as.numeric(df$value)
df$alpha <- as.factor(df$alpha)
value <- prediction <- NULL # for CRAN checks
suppressWarnings(
ggplot2::ggplot(df, ggplot2::aes(value, prediction, color = alpha)) +
ggplot2::geom_point(alpha = 0.15) +
ggplot2::geom_smooth(se = FALSE) +
ggplot2::facet_wrap(~var, scale = "free_x") +
ggplot2::ylab(quote(Q(y * "|" * x[1] * ",...," * x[p]))) +
ggplot2::xlab(quote(x[k])) +
ggplot2::theme(legend.position = "bottom")
)
}