Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of band width in ggplot geom_smooth lm

Tags:

r

ggplot2

With the following code:

library(ggplot2) ggplot(mtcars, aes(x=wt, y=mpg)) +     geom_point(aes(colour=factor(cyl))) +     geom_smooth(method="lm") 

I can get this plot:

enter image description here

My question is how does the grey zone defined? What's the meaning of it. And how can I play around with various parameter that control the width of that band?

like image 207
neversaint Avatar asked Apr 10 '15 06:04

neversaint


People also ask

What does Geom_smooth mean in R?

Key R function: geom_smooth() for adding smoothed conditional means / regression line. Key arguments: color , size and linetype : Change the line color, size and type. fill : Change the fill color of the confidence region.

What does Geom_smooth do in Ggplot?

The geom smooth function is a function for the ggplot2 visualization package in R. Essentially, geom_smooth() adds a trend line over an existing plot.

What does Geom_smooth method lm do?

We have used geom_smooth() function to add a regression line to our scatter plot by providing “method=lm” as an argument. We have set method=lm as lm stands for Linear Model, which plots a linear regression line.

What does Geom_smooth () using formula YX mean?

The warning geom_smooth() using formula 'y ~ x' is not an error. Since you did not supply a formula for the fit, geom_smooth assumed y ~ x, which is just a linear relationship between x and y. You can avoid this warning by using geom_smooth(formula = y ~ x, method = "lm")


2 Answers

By default, it is the 95% confidence level interval for predictions from a linear model ("lm"). The documentation from ?geom_smooth states that:

The default stat for this geom is stat_smooth see that documentation for more options to control the underlying statistical transformation.

Digging one level deeper, doc from ?stat_smooth tells us about the methods used to calculate the smoother's area.

For quick results, one can play with one of the arguments for stat_smooth which is level : level of confidence interval to use (0.95 by default)

By passing that parameter to geom_smooth, it is passed in turn to stat_smooth, so that if you wish to have a narrower region, you could use for instance .90 as a confidence level:

ggplot(mtcars, aes(x=wt, y=mpg)) +     geom_point(aes(colour=factor(cyl))) +     geom_smooth(method="lm", level=0.90) 

enter image description here

like image 73
Dominic Comtois Avatar answered Sep 29 '22 15:09

Dominic Comtois


It's the confidence interval. You can use se=FALSE if you do not want to display it. You can also use level = 0.99 if you want to have a 99% CI instead of a 95% CI. See ?stat_smooth for all the details.

like image 36
shadow Avatar answered Sep 29 '22 15:09

shadow