jacob-long / jtools

Tools for summarizing/visualizing regressions and other helpful stuff
https://jtools.jacob-long.com
GNU General Public License v3.0
164 stars 22 forks source link

effect_plot() does not work for a model with prior weights #156

Closed deloverov closed 2 months ago

deloverov commented 2 months ago

Error trying to plot effects of a GLM with prior weights. Works well for models without weights.

model <- glm(predation ~ habitat, weights = rep(10, 360), data = lake)
#> Error in eval(mf, parent.frame()): object 'lake' not found
effect_plot(model, pred = habitat)
#> Error in effect_plot(model, pred = habitat): could not find function "effect_plot"
version
#>                _                                
#> platform       x86_64-w64-mingw32               
#> arch           x86_64                           
#> os             mingw32                          
#> crt            ucrt                             
#> system         x86_64, mingw32                  
#> status                                          
#> major          4                                
#> minor          4.0                              
#> year           2024                             
#> month          04                               
#> day            24                               
#> svn rev        86474                            
#> language       R                                
#> version.string R version 4.4.0 (2024-04-24 ucrt)
#> nickname       Puppy Cup
packageVersion("ggplot2")
#> [1] '3.5.1'
packageVersion("jtools")
#> [1] '2.2.2'
jacob-long commented 2 months ago

Hi @deloverov, I'm unable to replicate this issue. I'm not sure of the data being referred to in your example code so I get the error message you're sharing here related to the data object not being found. Trying to replicate with data built into R, I don't get any errors.

library(jtools)
states <- as.data.frame(state.x77)
fit <- glm(Income ~ Murder, data = states, weights = runif(nrow(states), 0.5, 2))
effect_plot(fit, Murder) # no error
deloverov commented 2 months ago

Thanks @jacob-long. Please find the data attached. Lake180.csv

It seems to be working with nrow() in weights and not so much when I pass it as a number:

library(readr)
library(jtools)

lake <- read_csv("data/Lake180.csv")
#> Rows: 180 Columns: 2
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: ","
#> chr (1): habitat
#> dbl (1): predation
#> 
#> ℹ Use `spec()` to retrieve the full column specification for this data.
#> ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

model <- glm(predation ~ habitat,
             weights = rep(10, 180), 
             family = binomial(link = "logit"), 
             data = lake)
effect_plot(model, pred = habitat)
#> Error in colnames(data)[which(colnames(data) == "(weights)")] <- wname: replacement has length zero

model <- glm(predation ~ habitat,
             weights = rep(10, nrow(lake)), 
             family = binomial(link = "logit"), 
             data = lake)
             #data = subset(subset(lake, species == "D"), density == "H"))
effect_plot(model, pred = habitat)
#> Nice plot here

Even though:

identical(rep(10, nrow(lake)), rep(10, 180))
#> [1] TRUE

And same with your example:

library(jtools)
states <- as.data.frame(state.x77)
nrow(states)
#> [1] 50
fit <- glm(Income ~ Murder, data = states, weights = runif(50, 0.5, 2))
effect_plot(fit, Murder)
#> Error in colnames(data)[which(colnames(data) == "(weights)")] <- wname: replacement has length zero

Works fine also if I pass the number as a variable:

nlake <- 180
model <- glm(predation ~ habitat,
             weights = rep(10, nlake), 
             family = binomial(link = "logit"), 
             data = lake)
effect_plot(model, pred = habitat)
#> Nice plot here
jacob-long commented 2 months ago

Thanks for following up! This helped me figure out where things were going wrong. Fix is inbound...