Skip to content

Commit 4b8051c

Browse files
committed
update renv, added support for two classes (partial, add dots doesn't work yet)
1 parent 366dc18 commit 4b8051c

File tree

10 files changed

+606
-271
lines changed

10 files changed

+606
-271
lines changed

DESCRIPTION

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: loreplotr
22
Type: Package
33
Title: Draw logistic regression plots in R (Title Case)
4-
Version: 0.2.1
4+
Version: 0.2.2
55
Author: Sebastian Proost
66
Maintainer: Sebastian Proost <[email protected]>
77
Description: More about what it does (maybe more than one line)
@@ -10,7 +10,7 @@ License: file LICENSE
1010
Encoding: UTF-8
1111
LazyData: true
1212
Imports: ggplot2, nnet, tidyr, magrittr, dplyr, effects
13-
RoxygenNote: 7.2.3
13+
RoxygenNote: 7.3.2
1414
Suggests:
1515
testthat (>= 3.0.0)
1616
Config/testthat/edition: 3

NAMESPACE

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import(tidyr)
1111
importFrom(dplyr,mutate)
1212
importFrom(magrittr,"%>%")
1313
importFrom(stats,anova)
14+
importFrom(stats,binomial)
15+
importFrom(stats,glm)
1416
importFrom(stats,predict)
1517
importFrom(stats,reformulate)
1618
importFrom(stats,runif)

R/loreplotr.R

+29-10
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ get_group_dots_data <- function(dots_data, i, groups) {
5353
#'
5454
#' @param df The input data
5555
#' @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 !)
5757
#' @param draw_dots Show a dot in the plot for eachs sample (default=TRUE)
5858
#' @param dots_shape Shape of the dots (default=21)
5959
#' @param dots_fill Fill color of the dots (default="white")
@@ -67,6 +67,8 @@ get_group_dots_data <- function(dots_data, i, groups) {
6767
#' @import dplyr
6868
#' @importFrom magrittr %>%
6969
#' @importFrom stats anova
70+
#' @importFrom stats glm
71+
#' @importFrom stats binomial
7072
#' @import tidyr
7173
#' @importFrom stats predict reformulate
7274
#' @export
@@ -75,19 +77,37 @@ plot_area <- function(df, x, y, draw_dots=TRUE, dots_shape=21, dots_fill="white"
7577
wdf = df %>% select(c({{x}}, {{y}}))
7678
.GlobalEnv$wdf = df %>% select(c({{x}}, {{y}}))
7779

78-
# Fit multinomial and generate data for areas
80+
# Fit multinomial and generate data for areas (use glm for binary, multinom for > 2 classes)
7981
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+
}
81101

82102
print(summary(mnom_model))
83103

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+
}
86109

87-
print(anova(mnom_model, null_model))
88110

89-
predicted_probabilities = Effect(x, mnom_model, xlevels=300)
90-
probabilities_df = data.frame(predicted_probabilities$x, predicted_probabilities$prob)
91111

92112
melt_data = pivot_longer(probabilities_df, -{{x}}, names_to = y, values_to = "value")
93113
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"
101121
axis.title.y = element_blank(),
102122
)
103123

104-
if (draw_dots) {
124+
if (draw_dots && num_classes > 2) {
105125
groups = unique(melt_data[[y]])
106126

107127
dots_data <- as.data.frame(cbind(df[[y]], df[[x]], predict(mnom_model, newdata = df[x], "probs")))
108128

109-
110129
for (i in 1:length(groups)) {
111130
group_dots_data = get_group_dots_data(dots_data, i, groups)
112131

README.md

+24
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,30 @@ t <- t + scale_fill_manual(values = c("#DC9362", "#6BE19F", "#A373E5"))
8888
t
8989
```
9090

91+
Support for two classes is now partially implemented, though the classes need to be converted to 0 and 1 for the plot to work! (Note that dots currently aren't show when using two classes)
92+
93+
```R
94+
library(dplyr)
95+
library(ggplot2)
96+
library(loreplotr)
97+
98+
data("mtcars")
99+
100+
mtcars$cyl <- paste("cyl", mtcars$cyl, sep="_")
101+
102+
mtcars$cyl <- gsub("cyl_4", 0, mtcars$cyl)
103+
mtcars$cyl <- gsub("cyl_6", 0, mtcars$cyl)
104+
mtcars$cyl <- gsub("cyl_8", 1, mtcars$cyl)
105+
106+
mtcars$cyl <- as.numeric(mtcars$cyl)
107+
108+
t <- mtcars %>% loreplotr("mpg", "cyl")
109+
t
110+
111+
t <- t + scale_fill_manual(values = c("#DC9362", "#6BE19F"))
112+
t
113+
```
114+
91115
![Example loreplot using mtcars dataset and custom dots](./docs/img/loreplot_custom_dots.png)
92116

93117
## Troubleshooting/FAQ

example.R

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ data("mtcars")
66

77
mtcars$cyl <- paste("cyl", mtcars$cyl, sep="_")
88

9+
# mtcars$cyl <- gsub("cyl_4", 0, mtcars$cyl)
10+
# mtcars$cyl <- gsub("cyl_6", 0, mtcars$cyl)
11+
# mtcars$cyl <- gsub("cyl_8", 1, mtcars$cyl)
12+
#
13+
# mtcars$cyl <- as.numeric(mtcars$cyl)
14+
915
t <- mtcars %>% loreplotr("mpg", "cyl", dots_colour="black", dots_size=2, dots_alpha = 1, dots_shape=3)
1016
t
1117

loreplotr.Rproj

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Version: 1.0
2+
ProjectId: 7608749e-a747-44bd-88d7-c74ac3e301ba
23

34
RestoreWorkspace: Default
45
SaveWorkspace: Default

man/plot_area.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)