Giter VIP home page Giter VIP logo

Comments (5)

strengejacke avatar strengejacke commented on June 18, 2024

Do you have a reproducible example?

from insight.

AlpDYel avatar AlpDYel commented on June 18, 2024

I will try to get one. I am using a custom function to fit and save models in parallel and then load and name the resulting models so I would have to simplify certain things from the pipeline to get it to reproduce without publishing a bunch of tangentially related code.

from insight.

AlpDYel avatar AlpDYel commented on June 18, 2024

So I had to revisit the issue in a new project and I went ahead and isolated the issue:

ibrary(tidyverse)
library(lme4)
library(performance)

data(troutegg,package="faraway")
  
#This does not work
fit_mod <- function(formula,weights,data,family,subset){
  mod <-glmer(as.formula(formula),weight=eval(parse(text=weights)),
              data=data,family= eval(parse(text = family)),subset=eval(parse(text=subset)))
  return(mod)
}
mod <- fit_mod(formula = "survive/total~period+(1|location)",
               weights = "total",
               data=troutegg,
               family="binomial('logit')",
               subset = "total>0")

performance::binned_residuals(mod)
#This works
mod2 <-glmer(as.formula("survive/total~period+(1|location)"),weight=eval(parse(text="total")),
             data=troutegg,nAGQ=0,family= eval(parse(text = "binomial('logit')")))
performance::binned_residuals(mod2)

#Digging deeper, problem comes from
insight::get_data(mod)

#This does not work too...
fit_mod_alt <- function(formula,data,weights,troutegg,family,subset){
  mod <-glmer(as.formula(formula),weight=eval(parse(text=weights)),
              data=get(data,envir = .GlobalEnv),family= eval(parse(text = family)),subset=eval(parse(text=subset)))
  return(mod)
}
mod3 <- fit_mod_alt(formula = "survive/total~period+(1|location)",
               weights = "total",
               data="troutegg",
               family="binomial('logit')",
               subset = "total>0")
performance::binned_residuals(mod3)

If I understand correctly, the data for the model shows up as data internally when fitted with the fit_mod function (as opposed to troutegg), which causes issues with the get_data function from insight that is used internally within some performance library functions such as binned_residuals

from insight.

AlpDYel avatar AlpDYel commented on June 18, 2024

I also tried assigning troutegg to data to see if that helps, it also did not

#This also does not work
data <- troutegg
fit_mod_alt <- function(formula,data,weights,troutegg,family,subset){
  mod <-glmer(as.formula(formula),weight=eval(parse(text=weights)),
              data=data,family= eval(parse(text = family)),subset=eval(parse(text=subset)))
  return(mod)
}
mod3 <- fit_mod_alt(formula = "survive/total~period+(1|location)",
               weights = "total",
               data=data,
               family="binomial('logit')",
               subset = "total>0")
performance::binned_residuals(mod3)

from insight.

AlpDYel avatar AlpDYel commented on June 18, 2024

So I finally figured out the root of the issue:

The get_data function does not work intended when I used the subset argument. It works OK when I subset within the as an input (example with tidyverse filter) as opposed to the subset argument. It is likely a none-issue for 99% of the cases.

#Works!
mod4 <- fit_mod_alt(formula = "survive/total~period+(1|location)",
                    weights = "total",
                    data= troutegg,
                    family="binomial('logit')")
insight::get_data(mod4)
#Alternative to subset with tidyverse
mod5 <- fit_mod_alt(formula = "survive/total~period+(1|location)",
                    weights = "total",
                    data= troutegg %>% filter(eval(parse(text="location !=5"))),
                    family="binomial('logit')")

insight::get_data(mod5)

from insight.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.