Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

partial_dep() with BY argument: error for tibble X argument #92

Closed
RoelVerbelen opened this issue Oct 28, 2023 · 4 comments · Fixed by #93
Closed

partial_dep() with BY argument: error for tibble X argument #92

RoelVerbelen opened this issue Oct 28, 2023 · 4 comments · Fixed by #93

Comments

@RoelVerbelen
Copy link

library(hstats)
library(tibble)

## Dobson (1990) Page 93: Randomized Controlled Trial :
counts <- c(18,17,15,20,10,20,25,13,12)
outcome <- gl(3,1,9)
treatment <- gl(3,3)
df <- data.frame(treatment, outcome, counts) # showing data
glm.D93 <- glm(counts ~ outcome + treatment, family = poisson())

# PD with categorical BY

# Works
partial_dep(glm.D93, v = "outcome", X = df, BY = "treatment")
#> Partial dependence object (9 rows). Extract via $data. Top rows:
#> 
#>   treatment outcome        y
#> 1         1       1 3.044522
#> 2         1       2 2.590267
#> 3         1       3 2.751535

# Does not work
partial_dep(glm.D93, v = "outcome", X = tibble(df), BY = "treatment")
#> Error in data.frame(..., check.names = FALSE): arguments imply differing number of rows: 3, 0

Created on 2023-10-28 with reprex v2.0.2

Root cause seems to be the different behaviour when subsetting tibbles compared to data frames, see here.

That gets done here in the code for prepare_by().

@mayer79
Copy link
Collaborator

mayer79 commented Oct 28, 2023

Tibbles...

Will try:

if (is.data.frame(X)) {
  BY <- X[[by_name]]
} else {
  BY <- X[, by_name]
}

This problem might occur at other places as well. I will try to fix it.

@RoelVerbelen
Copy link
Author

Double brackets should work for both cases, so you should be able to avoid if statements:

iris
var = names(iris)[1]
iris[[var]]
tibble(iris)[[var]]

@mayer79
Copy link
Collaborator

mayer79 commented Oct 28, 2023

That covers the matrix case (tibble is also a data.frame). The same error happens actually also with case weights w.

@mayer79
Copy link
Collaborator

mayer79 commented Oct 28, 2023

Very good catch, thanks again!

@mayer79 mayer79 closed this as completed Oct 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants