Comments (8)
If the object is an S3 object, this should work --
getS3method("predict", class(object)[1]) |> rlang::ns_env_name()
If the object is S4 (eg, from lme4) --
attr(findClass('lmerMod')[[1]], "name")
from insight.
@bwiernik Thanks for your suggestions. I will try to test them soon when I have a free moment.
Could such functionality be integrated as a function in the package?
from insight.
Can you say more about the use case? If you have the package to fit the model, then the package should certainly be available to supply its methods
from insight.
@bwiernik, the context is parallel processing with my ale
package. Most of the package functions receive a model as input and then analyze the model for various interpretable machine learning (IML) tasks.
So, for example, when my ale()
function runs sequentially (without parallel processing), there is no problem. As you indicated, as long as the package with which the model was created is installed on the system, the ale()
function runs fine. But since I added parallel processing, the process has started choking, even though I use furrr
, an advanced parallel processing package that takes care of almost everything automagically. The problem is that furrr
has to send the necessary package environments to each parallel worker so that each worker can independently run the code. It cannot automatically detect that my code needs further packages, so it chokes. So, my code needs to tell furrr
which extra packages are needed for the parallel workers to do their tasks.
The current version of my code requires users to specify a model_packages
argument just for the sake of parallel processing. I would like to modify my code so that it automatically detects the model's package so that users would not need to supply this argument. This is what I would like insight
to do for me.
So, I think that my use case could be generalized to parallel processing when the source package of certain complex objects (in my case, models) needs to be detected.
from insight.
If the object is an S3 object, this should work --
getS3method("predict", class(object)[1]) |> rlang::ns_env_name()
@bwiernik Thanks; I have now implemented this check for S3 objects in my package and it now automatically detects and loads the appropriate packages.
I don't understand the S4 check code you gave (probably because I rarely work with S4 objects), so my package is now configured to automatically check for the S3 package and then give a graceful error message if it cannot be detected. Then users can explicitly specify the packages with my existing manual mechanism. That is acceptable, since it should work automatically for most users and only require manual intervention for a few complicated cases.
Would it be feasible to incorporate such a check into the {insight}
package, extended with checks for S4 objects as well?
Regardless, I appreciate your help. Your little S3 code has let me simplify my function usage for most users.
from insight.
R has two widely used class systems—S3 and S4 (and several less-used ones). S3 is most widely used, but some major modeling packages do use S4 (lme4 and OpenMx are the first ones that come to mind).
The function isS4()
can detect if an object is S4 or not. The code above will return the namespace associated with the class of the S4 model.
from insight.
If the object is S4 (eg, from lme4) --
attr(findClass('lmerMod')[[1]], "name")
But you specified the name of the S4 class in the code. I am not sufficiently familiar with S4 to convert this to code where I have an object of undetermined type and then probe its namespace (as with your S3 snippet above).
from insight.
Oh sorry
attr(findClass(object)[[1]], "name")
from insight.
Related Issues (20)
- `check_if_installed()` issue on R old-release? HOT 11
- test code needs glmmTMB HOT 3
- Test for `find_weights()` failing on CRAN HOT 1
- `find_transformation` incorrectly detects power transformations HOT 2
- insight::get_data issue with subset argument provided via eval(parse(text=...)) HOT 5
- `find_predictors()` with multiple `strata()` variables in `clogit()` HOT 1
- Issues with CRAN version HOT 1
- is_singular with lme models uses a lot of memory HOT 1
- glm model misreported as bernoulli HOT 2
- `brms::gr()` is not supported HOT 1
- AIC varies with order of factors in binomial? HOT 8
- `get_variance()` and `null_model()` functions not working depending on the way you call the objects of your model HOT 1
- Add family == "gaulss" to `find_formula` and `find_variables` for `mgcv::gam` HOT 2
- ICC for brms Beta model: returning NA HOT 11
- `find_predictors()`: false positives with some user inputs in GAM splines HOT 2
- rename marginaleffects' `s.value` column name
- Fix `apply_table_theme()`
- `insight::find_variables()`: error with `brms` and `mm()` HOT 8
- Tests failing on CRAN HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from insight.