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

  • SEARCH
  • Home
  • 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 3593472
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:34:54+00:00 2026-05-18T19:34:54+00:00

I am writing a function in R that will evaluate the fit of a

  • 0

I am writing a function in R that will evaluate the fit of a model, but each model takes the same arguments. How can I avoid repeating the same argument to each call to a model?

It is probably more clear here, where the arguments

  data=data,
  na.action = na.exclude,
  subset = block == site)

Are repeated.

modelfit <- function(order, response, predictor, site) {
   if(order == 0) {
     m <- lm(response ~ 1, 
             data=data,
             na.action = na.exclude,
             subset = block == site)
   } else if (is.numeric(order)) {
     m <- lm(response ~ poly(predictor, order), 
             data=data,
             na.action = na.exclude,
             subset = block == site)
   } else if (order == 'monod') {
     x<-predictor
     m <- nls(response ~ a*x/(b+x),
              start = list(a=1, b=1),
              data=data,
              na.action = na.exclude,
              subset = block == site)
   } else if (order == 'log') {
     m <- lm(response ~ poly(log(predictor), 1),
             data=data,
             na.action = na.exclude,
              subset = block == site)
   }
   AIC(m)
 }

Additional suggestions for better approaches to this question always appreciated.

  • 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-18T19:34:55+00:00Added an answer on May 18, 2026 at 7:34 pm

    You can use the ... idiom to do this. You include ... in the argument definition of your function and then within the lm() calls include ... as an extra argument. The ... effectively is a placeholder for all the extra arguments you wish to pass. Here is a (not tested) modification of your function that employs this approach:

    modelfit <- function(order, response, predictor, site, ...) {
       if(order == 0) {
         m <- lm(response ~ 1, ...)
       } else if (is.numeric(order)) {
         m <- lm(response ~ poly(predictor, order), ...)
       } else if (order == 'monod') {
         x<-predictor
         m <- nls(response ~ a*x/(b+x), start = list(a=1, b=1), ...)
       } else if (order == 'log') {
         m <- lm(response ~ poly(log(predictor), 1), ...)
       }
       AIC(m)
     }
    

    You then call this function and provide the repeated arguments in place of ...:

    with(myData, modelfit(2, myResponse, myPredictor, mySite, data = myData, 
                          na.action = na.exclude, subset = block == mySite))
    

    where myResponse, myPredictor and mySite are the variables you want to use that exist in your myData data frame.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a Function that has a return-type which will dynamically change depending
I'm writing a program that sends an array through a function (upperhess) that will
Suppose I'm writing a function that takes a list of integers and returns only
I am writing a function that takes in a output target file and a
I am writing a function that takes a string, string pointer and an int.
I am writing a function that takes in an argument. From that argument, I
I am writing a function that will take a parameter and, among other things,
I am writing a native function that will return multiple Python objects PyObject *V
I am writing a PHP function that will need to loop over an array
I'm writing a function in SQL that we can use to validate First/Middle/Last names

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.