Comments (6)
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()
from ggeffects.
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.
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.
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.
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.
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)
- ggpredict produces single predicted value for continuous variable HOT 4
- Question about sources of uncertainty when estimatinc confidence intervals HOT 2
- Johnson-Neyman FDR Correction HOT 10
- Relicensing {ggeffects} HOT 5
- Error of ggpredict with glmmPQL HOT 3
- Population-level predictions for gam/comparable predictions for gam and glmer fits HOT 5
- plot a subset for 4-way-interactions HOT 3
- New incompatibilities with emmeans for quantile regression HOT 4
- ggpredict() does not recognise `` quoted response variable with a : symbol in mixed models HOT 1
- "Collapse_group" produces many data points HOT 4
- ggpredict doesn't work for vglm models where multiple.responses = TRUE HOT 1
- Problem with ggpredict when fixing a parameter value in glmmTMB HOT 2
- ggpredict() returns 90% CI when ppd = TRUE for rstanarm models, but labeled as 95% HOT 1
- Difference between insight::get_predicted and ggeffects::ggpredict when calculating PI
- ggpredict outputs only 6 values at the time HOT 1
- `tinytable` S4 refactor: output formats HOT 5
- Issue with plotting an interaction with a quadratic variable through plot_model from a multi-level and logistic model HOT 2
- hypothesis_test on the lme4 object makes rstudio crash HOT 14
- Prediction intervals from predict_response(type="random") for meta-analysis using nlme
- Errors with new ggeffects version & zero-inflated negative binomial model (glmmTMB) 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 ggeffects.