Sean Burgess
Sean Burgess

Reputation: 67

set mininum and maximum for plotting predicted values of regression

I ran a regression that looks as follows:

fit <- lmer(support ~ income + (1 | country), data = df)

When using summary(df), it shows me that for income, the minimum is -2.4 and the maximum is 2.6.

I would like to plot the predicted values. I tried by using the following code:

library(ggeffects)
library(ggplot2)
p1 <- ggpredict(reg1, terms = "income")
ggplot(p1, aes(x, predicted)) + geom_line() + geom_ribbon(aes(ymin = conf.low, ymax = conf.high), alpha = 0.1)

However, the plot goes from -3 to 3. How can I set the minimum and maximum values for the plot? I tried with min and max, but it did not work

Upvotes: 0

Views: 677

Answers (1)

Daniel
Daniel

Reputation: 7832

By default, for continuous variables, a "pretty" range is chosen for the x-axis. This may include values that don't appear in the data. But using [all] might work, see this example, where in the 2nd case the predicted values range from 0.1 to 2.5, instead 0 to 2.6.

library(ggeffects)
data(iris)

m <- lm(Sepal.Length ~ Petal.Width, data = iris)

ggpredict(m, "Petal.Width")
#> 
#> # Predicted values of Sepal.Length
#> # x = Petal.Width
#> 
#>    x predicted std.error conf.low conf.high
#>  0.0     4.778     0.073    4.635     4.921
#>  0.4     5.133     0.057    5.022     5.244
#>  0.6     5.311     0.050    5.213     5.408
#>  1.0     5.666     0.040    5.587     5.745
#>  1.4     6.022     0.040    5.943     6.101
#>  1.6     6.199     0.044    6.113     6.286
#>  2.0     6.555     0.057    6.444     6.666
#>  2.6     7.088     0.082    6.927     7.248

ggpredict(m, "Petal.Width [all]")
#> 
#> # Predicted values of Sepal.Length
#> # x = Petal.Width
#> 
#>    x predicted std.error conf.low conf.high
#>  0.1     4.866     0.069    4.732     5.001
#>  0.4     5.133     0.057    5.022     5.244
#>  0.6     5.311     0.050    5.213     5.408
#>  1.2     5.844     0.039    5.767     5.920
#>  1.5     6.110     0.042    6.028     6.193
#>  1.7     6.288     0.047    6.197     6.380
#>  2.0     6.555     0.057    6.444     6.666
#>  2.5     6.999     0.077    6.847     7.151

Created on 2019-03-29 by the reprex package (v0.2.1)

This vignette could be helpful, too.

Upvotes: 0

Related Questions