Code Monkey home page Code Monkey logo

Comments (11)

mattansb avatar mattansb commented on July 20, 2024

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.

strengejacke avatar strengejacke commented on July 20, 2024

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.

DominiqueMakowski avatar DominiqueMakowski commented on July 20, 2024

but this is annoying because it makes changing type in the formula fail with emmeans with the new implementation of its wrapper:

.emmeans_wrapper <- function(model, levels = NULL, fixed = NULL, modulate = NULL, transform = "response", length = 10, ...) {
levels <- c(levels, modulate)
fixed <- c(fixed, modulate)
# Get emmeans refgrid
at <- insight::get_data(model)[levels]
at <- sapply(at, visualisation_matrix, length = length, simplify = FALSE)
suppressMessages(refgrid <- emmeans::ref_grid(model, at = at))
# Run emmeans
means <- emmeans::emmeans(refgrid, levels, by = fixed, transform = transform, ...)
means
}

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.

mattansb avatar mattansb commented on July 20, 2024

@strengejacke I have no idea how predict works - I am only an emmeans expert! 😅ðŸĪŠ

from modelbased.

strengejacke avatar strengejacke commented on July 20, 2024

@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.

strengejacke avatar strengejacke commented on July 20, 2024

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.

strengejacke avatar strengejacke commented on July 20, 2024

Does that work? (I hope so...)

from modelbased.

strengejacke avatar strengejacke commented on July 20, 2024

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.

DominiqueMakowski avatar DominiqueMakowski commented on July 20, 2024
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.

DominiqueMakowski avatar DominiqueMakowski commented on July 20, 2024

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.

DominiqueMakowski avatar DominiqueMakowski commented on July 20, 2024

You might have a more clever idea than my patch 😁

from modelbased.

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.