Comments (11)
The values of the "at" argument should type/value match the original data, and it internally dows any transformations etc.
In this case, gear
is numeric (kinda), so:
model <- lm(mpg ~as.factor(gear), data = mtcars)
emmeans::ref_grid(model, at = list(gear = c(3,4,5)))
#> 'emmGrid' object with variables:
#> gear = 3, 4, 5
Created on 2020-05-19 by the reprex package (v0.3.0)
This is also why these two give the same result:
m1 <- lm(mpg ~ wt, data = mtcars)
m2 <- lm(mpg ~ scale(wt), data = mtcars)
emmeans::emmeans(m1, ~ wt, at = list(wt = 3))
#> wt emmean SE df lower.CL upper.CL
#> 3 21.3 0.552 30 20.1 22.4
#>
#> Confidence level used: 0.95
emmeans::emmeans(m2, ~ wt, at = list(wt = 3))
#> wt emmean SE df lower.CL upper.CL
#> 3 21.3 0.552 30 20.1 22.4
#>
#> Confidence level used: 0.95
Created on 2020-05-19 by the reprex package (v0.3.0)
from modelbased.
This is also why these two give the same result:
I think this is because the association is actually the same, it does not change when you scale your input (the coefficients do, not the "slope"). With predict()
you get also identical results:
m1 <- lm(mpg ~ wt, data = mtcars)
m2 <- lm(mpg ~ scale(wt), data = mtcars)
library(ggeffects)
ggpredict(m1, "wt [3]")
#>
#> # Predicted values of mpg
#> # x = wt
#>
#> x | Predicted | SE | 95% CI
#> -------------------------------------
#> 3 | 21.25 | 0.55 | [20.17, 22.33]
ggpredict(m2, "wt [3]")
#>
#> # Predicted values of mpg
#> # x = wt
#>
#> x | Predicted | SE | 95% CI
#> -------------------------------------
#> 3 | 21.25 | 0.55 | [20.17, 22.33]
Created on 2020-05-19 by the reprex package (v0.3.0)
from modelbased.
but this is annoying because it makes changing type in the formula fail with emmeans with the new implementation of its wrapper:
modelbased/R/utils_emmeans_wrapper.R
Lines 68 to 83 in a852fe8
And the only way to easily accommodate would be in insight? So that it returns non-modified data?
Worst case scenario we can just do nothing, since modifying types in formulas is not recommended anyway, but...
from modelbased.
@strengejacke I have no idea how predict works - I am only an emmeans expert! ð ðĪŠ
from modelbased.
@strengejacke I have no idea how predict works - I am only an emmeans expert! ð ðĪŠ
;-)
They work pretty similar - though not exactly the same.
from modelbased.
So that it returns non-modified data?
I'm not sure how to detect if data is non-modified? But I think that emmeans either requires numerics, if really "numeric" (including numeric factor levels), and else the character version (see below).
levels <- "gear"
data(mtcars)
mtcars$gear <- factor(sjmisc::rec(mtcars$gear, rec = "3=a;4=b;5=c"))
model <- lm(mpg ~gear, data = mtcars)
at <- insight::get_data(model)[levels]
at <- sapply(at, modelbased::visualisation_matrix, simplify = FALSE)
emmeans::ref_grid(model, at = at)
#> 'emmGrid' object with variables:
#> gear = a, b, c
Created on 2020-05-19 by the reprex package (v0.3.0)
Thus, check if factor at
has numeric levels:
is.factor(x) && !anyNA(suppressWarnings(as.numeric(levels(x))))
and if so, convert to numeric before passing to sapply(vismatrix).
from modelbased.
Does that work? (I hope so...)
from modelbased.
Seems it works:
data(mtcars)
levels <- "gear"
model <- lm(mpg ~as.factor(gear), data = mtcars)
at <- insight::get_data(model)[levels]
at <- sapply(at, modelbased::visualisation_matrix, simplify = FALSE)
.numeric_levels <- function(x) {
is.factor(x) && !anyNA(suppressWarnings(as.numeric(levels(x))))
}
at <- lapply(at, function(i) {
if (.numeric_levels(i)) {
as.numeric(levels(i))
} else {
i
}
})
emmeans::ref_grid(model, at = at)
#> 'emmGrid' object with variables:
#> gear = 3, 4, 5
Created on 2020-05-19 by the reprex package (v0.3.0)
from modelbased.
data(mtcars)
levels <- "gear"
data <- mtcars
data$gear <- as.factor(data$gear)
model <- lm(mpg ~gear, data = data)
at <- insight::get_data(model)[levels]
at <- sapply(at, modelbased::visualisation_matrix, simplify = FALSE)
.numeric_levels <- function(x) {
is.factor(x) && !anyNA(suppressWarnings(as.numeric(levels(x))))
}
at <- lapply(at, function(i) {
if (.numeric_levels(i)) {
as.numeric(levels(i))
} else {
i
}
})
emmeans::ref_grid(model, at = at)
#> Error in `contrasts<-`(`*tmp*`, value = contrasts.arg[[nn]]): contrasts apply only to factors
Created on 2020-05-19 by the reprex package (v0.3.0)
Doesn't work then it the regular case, need to find a way to conditionally run this fix
from modelbased.
Because I think the issue is that the pulled variable from the model's data (through insight::get_data()) does not match the type of the variable in the model, as the latter has been changed "in the formula", which doesn't affect the data in the model.
from modelbased.
You might have a more clever idea than my patch ð
from modelbased.
Related Issues (20)
- estimate_expectation truncated_poisson glmmTMB bug HOT 5
- Fix failing tests HOT 2
- CRAN submission HOT 15
- between_within or ml1 df for post-hoc tests based on glmm HOT 3
- Provide `verbose` parameter to key functions to suppress messages/warnings
- Fix broken example
- Error in CRAN checks on R-devel-windows
- estimate_contrasts , p-value and 95% CI do not agree HOT 4
- Possible bug with estimate_grouplevel() for brms model HOT 5
- `estimate_contrasts()`: Different results using `as.factor()` within or outside formula
- Plotting from `estimate_means()` with a poisson model fails HOT 3
- methods other than "pairwise" for `estimate_contrasts()` fail
- `modelization_approach` vignette fails to render HOT 1
- Pairwise paired contrasts?
- Feature Request: Contrasting Simple Slopes HOT 1
- `visualization_recipe()` for `estimate_slopes()` should not map alpha to discrete values HOT 5
- CRAN submission (deadline 20/02) HOT 2
- Harmonize ggeffects and modelbased
- Keep code R-3.6 compatible
- Unable to install package on cran HOT 4
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 modelbased.