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

Butcher on a glm with a data.frame that has a single column #277

Closed
guiguilegui opened this issue Jan 8, 2025 · 2 comments · Fixed by #278
Closed

Butcher on a glm with a data.frame that has a single column #277

guiguilegui opened this issue Jan 8, 2025 · 2 comments · Fixed by #278
Labels
bug an unexpected problem or unintended behavior

Comments

@guiguilegui
Copy link

Hey!

The problem

I'm having trouble using butcher on a glm with a data.frame that has a single column:

Error in is_scalar_vector(x) && is.na(x) :
'length = 10' in coercion to 'logical(1)'

Digging a little bit, I see it's a problem using rlang::is_na(out) from the butcher:::exchange function.

Reproducible example

library(butcher)
#This does not work
df <- data.frame(y = 1:10)
model <- glm(y ~1, data = df)
butcher(model)

#Adding a second column is a workaround
df <- data.frame(y = 1:10, x = 1:10)
model <- glm(y ~1, data = df)
butcher(model)

#For comparison, a single column works with lm
df <- data.frame(y = 1:10)
model <- lm(y ~ 1, data = df)
butcher(model)

#Session Info:

sessionInfo()
# R version 4.4.2 (2024-10-31 ucrt)                                                                        
# Platform: x86_64-w64-mingw32/x64                                                                         
# Running under: Windows 11 x64 (build 22631)                                                              
                                                                                                         
# Matrix products: default                                                                                 
                                                                                                         
                                                                                                         
# locale:
# [1] LC_COLLATE=French_Canada.1252  LC_CTYPE=French_Canada.1252    LC_MONETARY=French_Canada.1252
# [4] LC_NUMERIC=C                   LC_TIME=French_Canada.1252

# time zone: America/Toronto
# tzcode source: internal

# attached base packages:
# [1] stats     graphics  grDevices datasets  utils     methods   base

# other attached packages:
# [1] butcher_0.3.4

# loaded via a namespace (and not attached):
# [1] compiler_4.4.2 tools_4.4.2    renv_1.0.11

Thanks for the great package!

@EmilHvitfeldt EmilHvitfeldt added the bug an unexpected problem or unintended behavior label Jan 8, 2025
@juliasilge
Copy link
Member

I am not quite sure why we are using rlang::is_na() in this way:

if (!rlang::is_na(out)[1]) {

It says it is always supposed to have a scalar predicate and return a scalar, but notice how we have [1] there like we are trying to subset.

df <- data.frame(y = 1:10)
model <- glm(y ~1, data = df)
out <- purrr::pluck(model, "data", .default = NA)
rlang::is_na(out)
#> Error in is_scalar_vector(x) && is.na(x): 'length = 10' in coercion to 'logical(1)'

Created on 2025-01-10 with reprex v2.1.1

Looks like back in 2019 we were trying to solve #88. Maybe instead we should use NULL, like this:

df <- data.frame(y = 1:10)
mod <- glm(y ~ 1, data = df)
out <- purrr::pluck(mod, "data", .default = NULL)
rlang::is_null(out)
#> [1] FALSE


mod <- earth::earth(Volume ~ ., data = trees)
out <- purrr::pluck(mod, "data", .default = NULL)
rlang::is_null(out)
#> [1] TRUE

Created on 2025-01-10 with reprex v2.1.1

@juliasilge
Copy link
Member

Thank you again for this report @guiguilegui! I have fixed this problem in #278 and you can install the version of butcher with the fix via:

# install.packages("pak")
pak::pak("tidymodels/butcher")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants