@@ -53,7 +53,7 @@ get_group_dots_data <- function(dots_data, i, groups) {
53
53
# '
54
54
# ' @param df The input data
55
55
# ' @param x Continuous variable to be shown on the x-axis
56
- # ' @param y Categorical variable, predicated probabilities shown on y-axis
56
+ # ' @param y Categorical variable, predicated probabilities shown on y-axis (if there are only two classes they need to be 0 and 1 !)
57
57
# ' @param draw_dots Show a dot in the plot for eachs sample (default=TRUE)
58
58
# ' @param dots_shape Shape of the dots (default=21)
59
59
# ' @param dots_fill Fill color of the dots (default="white")
@@ -67,6 +67,8 @@ get_group_dots_data <- function(dots_data, i, groups) {
67
67
# ' @import dplyr
68
68
# ' @importFrom magrittr %>%
69
69
# ' @importFrom stats anova
70
+ # ' @importFrom stats glm
71
+ # ' @importFrom stats binomial
70
72
# ' @import tidyr
71
73
# ' @importFrom stats predict reformulate
72
74
# ' @export
@@ -75,19 +77,37 @@ plot_area <- function(df, x, y, draw_dots=TRUE, dots_shape=21, dots_fill="white"
75
77
wdf = df %> % select(c({{x }}, {{y }}))
76
78
.GlobalEnv $ wdf = df %> % select(c({{x }}, {{y }}))
77
79
78
- # Fit multinomial and generate data for areas
80
+ # Fit multinomial and generate data for areas (use glm for binary, multinom for > 2 classes)
79
81
formula = reformulate(x , response = y )
80
- mnom_model = multinom(formula , data = wdf )
82
+
83
+ num_classes <- length(unique(wdf [[y ]]))
84
+
85
+ if (num_classes > 2 ) {
86
+ mnom_model <- multinom(formula , data = wdf )
87
+ predicted_probabilities = Effect(x , mnom_model , xlevels = 300 )
88
+ probabilities_df = data.frame (predicted_probabilities $ x , predicted_probabilities $ prob )
89
+ } else {
90
+ wdf [[y ]] = as.numeric(wdf [[y ]])
91
+ mnom_model <- glm(formula , family = binomial , data = wdf )
92
+
93
+ # Generate predicted probabilities manually for binary logistic regression
94
+ x_seq <- seq(min(wdf [[x ]]), max(wdf [[x ]]), length.out = 300 )
95
+ new_data <- data.frame (x_seq )
96
+ colnames(new_data ) <- x
97
+ probabilities <- predict(mnom_model , newdata = new_data , type = " response" )
98
+ probabilities_df <- data.frame (x_seq , prob_0 = 1 - probabilities , prob_1 = probabilities )
99
+ colnames(probabilities_df )[1 ] <- x
100
+ }
81
101
82
102
print(summary(mnom_model ))
83
103
84
- null_formula = reformulate(" 1" , response = y )
85
- null_model = multinom(null_formula , data = wdf )
104
+ if (num_classes > 2 ) {
105
+ null_formula <- reformulate(" 1" , response = y )
106
+ null_model <- multinom(null_formula , data = wdf )
107
+ print(anova(mnom_model , null_model ))
108
+ }
86
109
87
- print(anova(mnom_model , null_model ))
88
110
89
- predicted_probabilities = Effect(x , mnom_model , xlevels = 300 )
90
- probabilities_df = data.frame (predicted_probabilities $ x , predicted_probabilities $ prob )
91
111
92
112
melt_data = pivot_longer(probabilities_df , - {{x }}, names_to = y , values_to = " value" )
93
113
melt_data [[y ]] = gsub(' prob.' , ' ' , melt_data [[y ]])
@@ -101,12 +121,11 @@ plot_area <- function(df, x, y, draw_dots=TRUE, dots_shape=21, dots_fill="white"
101
121
axis.title.y = element_blank(),
102
122
)
103
123
104
- if (draw_dots ) {
124
+ if (draw_dots && num_classes > 2 ) {
105
125
groups = unique(melt_data [[y ]])
106
126
107
127
dots_data <- as.data.frame(cbind(df [[y ]], df [[x ]], predict(mnom_model , newdata = df [x ], " probs" )))
108
128
109
-
110
129
for (i in 1 : length(groups )) {
111
130
group_dots_data = get_group_dots_data(dots_data , i , groups )
112
131
0 commit comments