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 7805341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:11:41+00:00 2026-06-02T02:11:41+00:00

I realized a strange behavior today with in my R code. I tried a

  • 0

I realized a strange behavior today with in my R code.
I tried a package {boot.StepAIC} which includes a bootstrap function for the results of the stepwise regression with the AIC. However I do not think the statistical background is here the problem (I hope so).
I can use the function at the top level of R. This is my example code.

require(MASS)
require(boot.StepAIC)

n<-100
x<-rnorm(n); y<-rnorm(n,sd=2); z<-rnorm(n,sd=3); res<-x+y+z+rnorm(n,sd=0.1)
dat.test<-as.data.frame(cbind(x,y,z,res))
form.1<-as.formula(res~x+y+z)
boot.stepAIC(lm(form.1, dat.test),dat.test) # should be OK - works at me

However, I wanted to wrap that in an own function. I pass the data and the formula to that function. But I get an error within boot.stepAIC() saying:

the model fit failed in 100 bootstrap samples Error in
strsplit(nam.vars, “:”) : non-character argument

# custom function
fun.boot.lm.stepAIC<-function(dat,form) {
  if(!inherits(form, "formula")) stop("No formula given")
  fit.lm<-lm(formula=form,data=dat)
  return(boot.stepAIC(object=fit.lm,data=dat))
 }
fun.boot.lm.stepAIC(dat=dat.test,form=form.1)
# results in an error 

So where is the mistake? I suppose it must have something to do with the local and global environment, doesn’t it?

  • 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-06-02T02:11:43+00:00Added an answer on June 2, 2026 at 2:11 am

    Using do.call as in anova test fails on lme fits created with pasted formula provides the answer.

    boot.stepAIC doesn’t have access to form when run within a function; that can be recreated in the global environment like this; we see that lm is using form.1 as the formula, and removing it makes boot.stepAIC fail.

    > form.1<-as.formula(res~x+y+z)
    > mm <- lm(form.1, dat.test)
    > mm$call
    lm(formula = form.1, data = dat.test)
    > rm(form.1)
    > boot.stepAIC(mm,dat.test)
    # same error as OP
    

    Using do.call does work. Here I use as.name as well; otherwise the mm object carries around the entire dataset instead of just the name of it.

    > form.1<-as.formula(res~x+y+z)
    > mm <- do.call("lm", list(form.1, data=as.name("dat.test")))
    > mm$call
    lm(formula = res ~ x + y + z, data = dat.test)
    > rm(form.1)
    > boot.stepAIC(mm,dat.test)
    

    To apply this to the original problem, I’d do this:

    fun.boot.lm.stepAIC<-function(dat,form) {
      if(!inherits(form, "formula")) stop("No formula given")
      mm <- do.call("lm", list(form, data=as.name(dat)))
      do.call("boot.stepAIC", list(mm,data=as.name(dat)))
    }    
    form.1<-as.formula(res~x+y+z)
    fun.boot.lm.stepAIC(dat="dat.test",form=form1)
    

    This works too but the entire data set gets included in the final output object, and the final output to console, as well.

    fun.boot.lm.stepAIC<-function(dat,form) {
      if(!inherits(form, "formula")) stop("No formula given")
      mm <- do.call("lm", list(form, data=dat))
      boot.stepAIC(mm,data=dat)
    }    
    form.1<-as.formula(res~x+y+z)
    fun.boot.lm.stepAIC(dat=dat.test,form=form.1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using php copy() function and am getting strange results which made me
I realized today that I have blindly just followed this requirement for years without
I just realized while testing an onClick function with firebug that it would be
I am having a very strange behavior in JBoss, and I'd like avail myself
I realize it's probably something strange, but here is what I have. I have
I realized this issue when trying to sum upvotes AND downvotes because the value
I realized that I can have problems with single quotes in php arrays: <?php
I realized that appears this interface in .NET Framework 4.5 I was looking first
I realized that ShowHeaderWhenEmpty only works in PostBacks. Is there any way to make
I realized that my application doesn't show up in searches on Google Play if

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.