Comments (8)
No. Since for this code line:
stats::dbinom(round(n * resp), round(n), predicted, log = TRUE) * w
The predicted values are p
for one model, and 1-p
for the other model, we need to correct the response value, which should be "inversed" for the two models. This should be fixed on the PR #835
d <- mtcars
d$zero <- factor(d$vs, levels = c(0, 1))
d$ones <- factor(d$vs, levels = c(1, 0))
ml_zero <- glm(zero ~ mpg, family = binomial, data = d)
ml_ones <- glm(ones ~ mpg, family = binomial, data = d)
logLik(ml_zero)
#> 'log Lik.' -12.76667 (df=2)
insight::get_loglikelihood(ml_zero)
#> 'log Lik.' -12.76667 (df=2)
logLik(ml_ones)
#> 'log Lik.' -12.76667 (df=2)
insight::get_loglikelihood(ml_ones)
#> 'log Lik.' -12.76667 (df=2)
performance::compare_performance(ml_zero, ml_ones)
#> When comparing models, please note that probably not all models were fit
#> from same data.
#> # Comparison of Model Performance Indices
#>
#> Name | Model | AIC (weights) | AICc (weights) | BIC (weights) | Tjur's R2 | RMSE | Sigma | Log_loss | Score_log | Score_spherical | PCP
#> ---------------------------------------------------------------------------------------------------------------------------------------------
#> ml_zero | glm | 29.5 (0.500) | 29.9 (0.500) | 32.5 (0.500) | 0.474 | 0.361 | 1.000 | 0.399 | -14.308 | 0.093 | 0.741
#> ml_ones | glm | 29.5 (0.500) | 29.9 (0.500) | 32.5 (0.500) | 0.474 | 0.361 | 1.000 | 2.104 | -18.429 | 0.079 | 0.259
Created on 2023-11-27 with reprex v2.0.2
from insight.
Issue in insight.
df <- mtcars
df$zero <- factor(df$vs, levels = c(0, 1))
df$ones <- factor(df$vs, levels = c(1, 0))
ml_zero <- glm(zero ~ mpg, family = binomial, data = df)
ml_ones <- glm(ones ~ mpg, family = binomial, data = df)
logLik(ml_zero)
#> 'log Lik.' -12.76667 (df=2)
insight::get_loglikelihood(ml_zero)
#> 'log Lik.' -12.76667 (df=2)
logLik(ml_ones)
#> 'log Lik.' -12.76667 (df=2)
insight::get_loglikelihood(ml_ones)
#> 'log Lik.' -67.33844 (df=2)
Created on 2023-11-27 with reprex v2.0.2
from insight.
Tagging @bwiernik @DominiqueMakowski
from insight.
doesn't get_loklikelihood pull lokLik when it can? 🤔
from insight.
I think the implementation (from you?) is:
- Pull per-observation log-likelihoods, if possible (
Line 212 in 09551e9
- Only fall-back to
logLik()
if we don't have per-observation lls (Line 406 in 09551e9
If I recall right, your intention was:
attr(out, "per_obs") <- lls # This is useful for some models comparison tests
from insight.
library(insight)
df <- mtcars
df$zero <- factor(df$vs, levels = c(0, 1))
df$ones <- factor(df$vs, levels = c(1, 0))
ml_zero <- glm(zero ~ mpg, family = binomial, data = df)
ml_ones <- glm(ones ~ mpg, family = binomial, data = df)
get_response(ml_zero, as_proportion = TRUE)
#> [1] 0 0 1 1 0 1 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 0 1 0 0 0 1
#> Levels: 0 1
get_response(ml_ones, as_proportion = TRUE)
#> [1] 0 0 1 1 0 1 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 0 1 0 0 0 1
#> Levels: 1 0
Created on 2023-11-27 with reprex v2.0.2
The response is the same
from insight.
Yeah, it's get_predicted()
, which is different here:
df <- mtcars
df$zero <- factor(df$vs, levels = c(0, 1))
df$ones <- factor(df$vs, levels = c(1, 0))
ml_zero <- glm(zero ~ mpg, family = binomial, data = df)
ml_ones <- glm(ones ~ mpg, family = binomial, data = df)
logLik(ml_zero)
#> 'log Lik.' -12.76667 (df=2)
insight::get_loglikelihood(ml_zero)
#> 'log Lik.' -12.76667 (df=2)
stats::deviance(ml_zero)
#> [1] 25.53334
insight::get_predicted(ml_zero, ci = NULL, verbose = FALSE)
#> Predicted values:
#>
#> [1] 0.55122251 0.55122251 0.72717879 0.59333677 0.31338533 0.26065097
#> [7] 0.06427450 0.84144476 0.72717879 0.36143691 0.23654569 0.14500953
#> [13] 0.19990013 0.09188885 0.01265744 0.01265744 0.07543906 0.99401399
#> [19] 0.98595713 0.99685235 0.60367900 0.10324638 0.09188885 0.04275502
#> [25] 0.36143691 0.94869098 0.91354266 0.98595713 0.11582865 0.41243065
#> [31] 0.08495358 0.59333677
#>
#> NOTE: Confidence intervals, if available, are stored as attributes and can be accessed using `as.data.frame()` on this output.
logLik(ml_ones)
#> 'log Lik.' -12.76667 (df=2)
insight::get_loglikelihood(ml_ones)
#> 'log Lik.' -67.33844 (df=2)
stats::deviance(ml_ones)
#> [1] 25.53334
insight::get_predicted(ml_ones, ci = NULL, verbose = FALSE)
#> Predicted values:
#>
#> [1] 0.448777493 0.448777493 0.272821214 0.406663228 0.686614674 0.739349027
#> [7] 0.935725503 0.158555240 0.272821214 0.638563093 0.763454313 0.854990468
#> [13] 0.800099872 0.908111148 0.987342561 0.987342561 0.924560943 0.005986014
#> [19] 0.014042865 0.003147652 0.396321000 0.896753623 0.908111148 0.957244981
#> [25] 0.638563093 0.051309021 0.086457341 0.014042865 0.884171352 0.587569354
#> [31] 0.915046417 0.406663228
#>
#> NOTE: Confidence intervals, if available, are stored as attributes and can be accessed using `as.data.frame()` on this output.
Created on 2023-11-27 with reprex v2.0.2
And since later:
lls <- stats::dbinom(round(n * resp), round(n), predicted, log = TRUE) * w
the per-observation lls
are different (because the predictions are p
for ml_zero, and 1-p
for ml_ones), and again later we have:
out <- sum(lls)
That's where the difference comes from.
from insight.
If I am understanding this right, is it the standard R value that is wrong/incorrect?
from insight.
Related Issues (20)
- `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
- `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
- Detect the package that is needed to create or predict a model object HOT 8
- rename marginaleffects' `s.value` column name
- `find_predictors()`: false positives with some user inputs in GAM splines HOT 2
- Fix `apply_table_theme()`
- `insight::find_variables()`: error with `brms` and `mm()` HOT 8
- Tests failing on CRAN HOT 1
- Add support for package "did" HOT 2
- warnings/approximation of observation-level variance in .get_variance_distributional
- checking diagonal elements of a covariance matrix is insufficient to diagnose singularity
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.