Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

modify lm or loess function to use it within ggplot2's geom_smooth

Tags:

r

ggplot2

lm

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's debug tools it is difficult for me to debug them. Honestly, I don't get what I should put inside the return() call.

like image 833
dalloliogm Avatar asked Mar 03 '10 11:03

dalloliogm


People also ask

What is method lm in Geom_smooth?

We have our scatterplot, and we're adding a trend line as a new layer with ' + ' and geom_smooth() . But in this case, we're adding a straight-line linear model instead of a LOESS line. To do this, we simply set method = 'lm' . (If you haven't figured it out, ' lm ' means "linear model.")

What is the difference between Geom_line and Geom_smooth?

Geom_line creates a single line for both panels and distributes the colors according to the colour variable, while geom_smooth does not draw the smooth line in the 2nd panel.

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")

What is the difference between Geom_smooth and Stat_smooth?

geom_smooth() and stat_smooth() are effectively aliases: they both use the same arguments. Use stat_smooth() if you want to display the results with a non-standard geom.


1 Answers

There is some weirdness in using ... as an argument in a function call that I don't fully understand (it has something to do with ... being a list-type object).

Here is a version that works by taking the function call as an object, setting the function to be called to lm and then evaluating the call in the context of our own caller. The result of this evaluation is our return value (in R the value of the last expression in a function is the value returned, so we do not need an explicit return).

foo <- function(formula,data,...){
   print(head(data))
   x<-match.call()
   x[[1]]<-quote(lm)
   eval.parent(x)
}

If you want to add arguments to the lm call, you can do it like this:

x$na.action <- 'na.exclude'

If you want to drop arguments to foo before you call lm, you can do it like this

x$useless <- NULL

By the way, geom_smooth and stat_smooth pass any extra arguments to the smoothing function, so you need not create a function of your own if you only need to set some extra arguments

qplot(data=diamonds, carat, price, facets=~clarity) + 
  stat_smooth(method="loess",span=0.5)
like image 173
Jyotirmoy Bhattacharya Avatar answered Nov 15 '22 12:11

Jyotirmoy Bhattacharya