Coder Social home page Coder Social logo

Comments (6)

strengejacke avatar strengejacke commented on May 26, 2024 1

That's the reason why factors are returned as numeric by default, so it's easier to create - I call it "Stata-like" - plots. I think you mean something like this?

library(ggeffects)
library(lme4)
#> Loading required package: Matrix
library(ggplot2)
data("sleepstudy")

m <- lmer(Reaction ~ Days + (1 + Days | Subject), data = sleepstudy)
pr <- ggpredict(m, "Days")

ggplot(pr, aes(x = x, y = predicted, ymin = conf.low, ymax = conf.high)) +
  geom_point() +
  geom_line() +
  geom_errorbar()

rplot

from ggeffects.

strengejacke avatar strengejacke commented on May 26, 2024 1

I think it doesn't make sense to change the input type from numeric to factor in the returned data frame - however, to get "error-bar-styled" CI's also for numeric terms, I added a ci.style-argument for the plot()-method. I think this is the most helpful option, and should address your initial request:

however ggpredict() plots a line as for numeric values, even when trying to force it not to do so by adding x.as.factor = TRUE.

from ggeffects.

strengejacke avatar strengejacke commented on May 26, 2024

Is your variable really still a factor? Numeric values will never be a factor in the returned data frame, and factors only if you set x.as.factor = TRUE. See these examples:

library(ggeffects)
data(efc)
efc$c172code <- as.factor(efc$c172code)
fit <- lm(barthtot ~ c12hour + c172code + e42dep, data = efc)

ggpredict(fit, "c172code", x.as.factor = TRUE) %>% plot()
ggpredict(fit, "c172code", x.as.factor = FALSE) %>% plot()
ggpredict(fit, "e42dep", x.as.factor = TRUE) %>% plot()
ggpredict(fit, "e42dep", x.as.factor = FALSE) %>% plot()

ggpredict(fit, "c172code", x.as.factor = TRUE) %>% str()
ggpredict(fit, "c172code", x.as.factor = FALSE) %>% str()
ggpredict(fit, "e42dep", x.as.factor = TRUE) %>% str()
ggpredict(fit, "e42dep", x.as.factor = FALSE) %>% str()

Here's one with output. There you can see that, although for the 2nd example x.as.factor = FALSE, the plot will still produce points with error bars - that's because the variable was a factor, and this information is stored in the returned object.

library(ggeffects)
data(efc)
efc$c172code <- as.factor(efc$c172code)
fit <- lm(barthtot ~ c12hour + c172code + e42dep, data = efc)

ggpredict(fit, "c172code", x.as.factor = TRUE) %>% str()
#> Classes 'ggeffects' and 'data.frame':    3 obs. of  6 variables:
#>  $ x        : Factor w/ 3 levels "1","2","3": 1 2 3
#>  $ predicted: num  63.3 64.9 65.3
#>  $ std.error: num  1.441 0.859 1.547
#>  $ conf.low : num  60.5 63.2 62.2
#>  $ conf.high: num  66.2 66.6 68.3
#>  $ group    : Factor w/ 1 level "1": 1 1 1
#>  - attr(*, "x.is.factor")= chr "1"

ggpredict(fit, "c172code", x.as.factor = FALSE) %>% str()
#> Classes 'ggeffects' and 'data.frame':    3 obs. of  6 variables:
#>  $ x        : num  1 2 3
#>   ..- attr(*, "labels")= Named num  1 2 3
#>   .. ..- attr(*, "names")= chr  "1" "2" "3"
#>  $ predicted: num  63.3 64.9 65.3
#>  $ std.error: num  1.441 0.859 1.547
#>  $ conf.low : num  60.5 63.2 62.2
#>  $ conf.high: num  66.2 66.6 68.3
#>  $ group    : Factor w/ 1 level "1": 1 1 1
#>  - attr(*, "rawdata")='data.frame':  823 obs. of  3 variables:
#>   ..$ response: num  75 75 35 0 25 60 5 35 0 25 ...
#>   ..$ x       : num  2 2 1 2 2 2 2 2 2 2 ...
#>   .. ..- attr(*, "labels")= Named num  1 2 3
#>   .. .. ..- attr(*, "names")= chr  "1" "2" "3"
#>   ..$ group   : Factor w/ 1 level "1": 1 1 1 1 1 1 1 1 1 1 ...
#>  - attr(*, "x.is.factor")= chr "1"

ggpredict(fit, "e42dep", x.as.factor = TRUE) %>% str()
#> Classes 'ggeffects' and 'data.frame':    4 obs. of  6 variables:
#>  $ x        : num  1 2 3 4
#>  $ predicted: num  103 82.3 61.7 41
#>  $ std.error: num  2.13 1.63 1.44 1.69
#>  $ conf.low : num  98.8 79.1 58.9 37.7
#>  $ conf.high: num  107.1 85.5 64.5 44.3
#>  $ group    : Factor w/ 1 level "1": 1 1 1 1
#>  - attr(*, "rawdata")='data.frame':  823 obs. of  3 variables:
#>   ..$ response: num  75 75 35 0 25 60 5 35 0 25 ...
#>   ..$ x       : num  3 3 3 4 4 4 4 4 4 4 ...
#>   .. ..- attr(*, "label")= chr "elder's dependency"
#>   .. ..- attr(*, "labels")= Named num  1 2 3 4
#>   .. .. ..- attr(*, "names")= chr  "independent" "slightly dependent" "moderately dependent" "severely dependent"
#>   ..$ group   : Factor w/ 1 level "1": 1 1 1 1 1 1 1 1 1 1 ...
#>  - attr(*, "x.is.factor")= chr "0"

ggpredict(fit, "e42dep", x.as.factor = FALSE) %>% str()
#> Classes 'ggeffects' and 'data.frame':    4 obs. of  6 variables:
#>  $ x        : num  1 2 3 4
#>  $ predicted: num  103 82.3 61.7 41
#>  $ std.error: num  2.13 1.63 1.44 1.69
#>  $ conf.low : num  98.8 79.1 58.9 37.7
#>  $ conf.high: num  107.1 85.5 64.5 44.3
#>  $ group    : Factor w/ 1 level "1": 1 1 1 1
#>  - attr(*, "rawdata")='data.frame':  823 obs. of  3 variables:
#>   ..$ response: num  75 75 35 0 25 60 5 35 0 25 ...
#>   ..$ x       : num  3 3 3 4 4 4 4 4 4 4 ...
#>   .. ..- attr(*, "label")= chr "elder's dependency"
#>   .. ..- attr(*, "labels")= Named num  1 2 3 4
#>   .. .. ..- attr(*, "names")= chr  "independent" "slightly dependent" "moderately dependent" "severely dependent"
#>   ..$ group   : Factor w/ 1 level "1": 1 1 1 1 1 1 1 1 1 1 ...
#>  - attr(*, "x.is.factor")= chr "0"

from ggeffects.

z3tt avatar z3tt commented on May 26, 2024

Hi Daniel, thank you very much for your quick answer! Indeed, it became transformed to a numeric on the way - now it works! But as I got from your answer, it is not possible to plot error bars like those for factors for specific values of a numeric explanatory?

from ggeffects.

strengejacke avatar strengejacke commented on May 26, 2024

Just to get a bit back to topic: would you like x.as.factor to return a factor also for numeric terms, or lead to a different plot result?

from ggeffects.

z3tt avatar z3tt commented on May 26, 2024

Sorry for being silent, have been busy running new models. At the moment, I do not need the suggested option but it might be helpful in some cases. Since at the moment x.as.factor does not change anything when applied to numeric terms, it might be a good thing to enable this for some purposes I can't imagine at the moment. But only if this is a meaningful thing from the stats' point of view.

from ggeffects.

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.