Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 669639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:10:22+00:00 2026-05-14T00:10:22+00:00

I need to modify the lm (or eventually loess ) function so I can

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T00:10:23+00:00Added an answer on May 14, 2026 at 12:10 am

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 353k
  • Answers 353k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Separate out the class so that you can have a… May 14, 2026 at 7:44 am
  • Editorial Team
    Editorial Team added an answer It is rather messy but you need to do something… May 14, 2026 at 7:44 am
  • Editorial Team
    Editorial Team added an answer When your page first loads, "this" is the entire page,… May 14, 2026 at 7:44 am

Related Questions

I need to modify the following MySQL statement to include information from a third
I need to modify the contents of the child rows of a hierarchical Infragistics
I need to modify the vertical and horizontal resolutions without changing the image dimensions.
I need to modify the versions.aspx page... No idea how to nor do I
I need to modify the HTML structure of a joomla module. I would like

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.