Exponential Smoothing for ggplot2's stat_smooth()

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Exponential Smoothing for ggplot2's stat_smooth()

Michael King-7
Hello everyone,

I was wondering if anyone was aware of a way in which I could use ggplot's
stat_smooth() function for add an exponential moving average.

I was thinking that I could maybe use something like:

>myggplot + stat_smooth (method = 'HoltWinters( data , .9 , 0, 0)')

but my efforts were futile.

Perhaps there is a way to write my own custom method to throw into
stat_smooth??

Thanks,
- Mike

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Exponential Smoothing for ggplot2's stat_smooth()

hadley wickham
Hi Michael,

To use stat_smooth as is, you need a smoothing function that basically
works like lm.  So you have two options: to make an interface to
HoltWinters (or find another exponential smooth that already has that
interface) or write your own stat object, which might look something
like this:

StatExpo <- proto(Stat, {
  objname <- "expo"
  desc <- "Exponential smoothing"
  default_geom <- function(.) GeomLine

  calculate_groups <- function(., data, scales, variable="x", ...) {
    data$y <- HoltWinters(data$x, ...)
  }
})
stat_expo <- StatExpo$new

Then you could use myggplot + stat_expo(alpha = 0.9, beta = 0, gamma = 0)

Hadley


On 11/16/07, Michael King <[hidden email]> wrote:

> Hello everyone,
>
> I was wondering if anyone was aware of a way in which I could use ggplot's
> stat_smooth() function for add an exponential moving average.
>
> I was thinking that I could maybe use something like:
>
> >myggplot + stat_smooth (method = 'HoltWinters( data , .9 , 0, 0)')
>
> but my efforts were futile.
>
> Perhaps there is a way to write my own custom method to throw into
> stat_smooth??
>
> Thanks,
> - Mike
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>


--
http://had.co.nz/

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.