Closed deloverov closed 3 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
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
Thanks for following up! This helped me figure out where things were going wrong. Fix is inbound...
Error trying to plot effects of a GLM with prior weights. Works well for models without weights.