I need to modify the lm (or eventually loess) function so I can use it in ggplot2's geom_smooth (or stat_smooth).
For example, this is how stat_smooth is used normally:
> qplot(data=diamonds, carat, price, facets=~clarity) + stat_smooth(method='lm')`
I would like to define a custom 'lm2' function to use as value for the 'method' parameter in stat_smooth, so I can customize its behaviour.
> lm2 <- function(formula, data, ...)
{
print(head(data))
return(lm(formula, data, ...))
}
> qplot(data=diamonds, carat, price, facets=~clarity) + stat_smooth(method='lm2')
Note that I have used method='lm2' as parameter in stat_smooth. When I execute this code a get the error:
'Error in eval(expr, envir, enclos) : 'nthcdr' needs a list to CDR down'
Which I don't understand very well. The lm2 method works very well when run outside of stat_smooth. I played with this a bit and I have got different types of error, but since I am not comfortable with R' debug tools it is difficult for me to debug them. Honestly, I don't get what I should put inside the 'return()' call.