How to get back the fits

12 views
Skip to first unread message

James Rome

unread,
Jun 23, 2011, 3:23:53 PM6/23/11
to ggp...@googlegroups.com
If I use stat_smooth(method = "lm") in my factored ggplot2 graph, is
there a way to get the fits returned to me in numeric form so that I can
subtract them out from the data?

Thanks,
Jim Rome

Joran

unread,
Jun 23, 2011, 4:52:09 PM6/23/11
to ggplot2
See here:

http://stackoverflow.com/questions/2666805/can-one-extract-model-fit-parameters-after-a-ggplot-stat-smooth-call

so, no, I guess not.

But as Hadley says above, you can always fit the model and adjust the
data yourself.

James McCreight

unread,
Jun 23, 2011, 5:56:22 PM6/23/11
to James Rome, ggp...@googlegroups.com
(meant to post this to the entire list)
Hi Jim-

this kind of pulling stats out is not really officially sanctioned, but it can be done. in fact, using the ggplot_build() as I used before to check the calculated box plots. When I asked about doing this, Koshke mentioned that i can not rely on it to exist in the future... 

the only problem with this is that you'll have to do a smoothing spline or feed some other param to stat_smooth to get the value at every data point.


z<- rnorm( 1000)
f=data.frame( x=1:length(z), y=z)
g <- ggplot( f, aes( x=x, y=y) ) + stat_smooth(method='lm') + geom_point()
gb <- ggplot_build( g )
str(gb$data)
List of 2
 $ 1:List of 1
  ..$ :'data.frame': 80 obs. of  6 variables:
  .. ..$ y    : num [1:80] -0.0331 -0.0325 -0.032 -0.0314 -0.0308 ...
  .. ..$ ymin : num [1:80] -0.159 -0.156 -0.153 -0.15 -0.147 ...
  .. ..$ ymax : num [1:80] 0.0926 0.0907 0.0889 0.0872 0.0854 ...
  .. ..$ x    : num [1:80] 1 13.6 26.3 38.9 51.6 ...
  .. ..$ se   : num [1:80] 0.064 0.0628 0.0616 0.0604 0.0592 ...
  .. ..$ group: num [1:80] 1 1 1 1 1 1 1 1 1 1 ...
  ..- attr(*, "dim")= int [1:2] 1 1
 $ 2:List of 1
  ..$ :'data.frame': 1000 obs. of  3 variables:
  .. ..$ y    : num [1:1000] -0.95 -0.883 1.429 1.78 -0.922 ...
  .. ..$ x    : num [1:1000] 1 2 3 4 5 6 7 8 9 10 ...
  .. ..$ group: num [1:1000] 1 1 1 1 1 1 1 1 1 1 ...
  ..- attr(*, "dim")= int [1:2] 1 1
 

James


--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2



--
-
******************************************************************************
James McCreight                               
cell: (831) 261-5149
VoIP (to cell): (720) 897-7546

Hadley Wickham

unread,
Jun 26, 2011, 7:32:35 PM6/26/11
to James McCreight, James Rome, ggp...@googlegroups.com
> this kind of pulling stats out is not really officially sanctioned, but it
> can be done. in fact, using the ggplot_build() as I used before to check the
> calculated box plots. When I asked about doing this, Koshke mentioned that i
> can not rely on it to exist in the future...

Yes, this will definitely change in the next version.

I highly recommend learning how to fit multiple models outside of
ggplot2 - it will stand you in good stead for many other problems.

Hadley


--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Reply all
Reply to author
Forward
0 new messages