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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:34:42+00:00 2026-05-26T04:34:42+00:00

I am running the same regression with small alterations of x variables several times.

  • 0

I am running the same regression with small alterations of x variables several times. My aim is after having determined the fit and significance of each variable for this linear regression model to view all all major plots. Instead of having to create each plot one by one, I want a function to loop through my variables (x1…xn) from the following list.

fit <-lm( y ~ x1 + x2 +… xn))

The plots I want to create for all x are
1) ‘x versus y’ for all x in the function above
2) ‘x versus predicted y
3) x versus residuals
4) x versus time, where time is not a variable used in the regression but provided in the dataframe the data comes from.

I know how to access the coefficients from fit, however I am not able to use the coefficient names from the summary and reuse them in a function for creating the plots, as the names are characters.

I hope my question has been clearly described and hasn’t been asked already.

Thanks!

  • 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-26T04:34:43+00:00Added an answer on May 26, 2026 at 4:34 am

    Create some mock data

    dat <- data.frame(x1=rnorm(100), x2=rnorm(100,4,5), x3=rnorm(100,8,27), 
      x4=rnorm(100,-6,0.1), t=(1:100)+runif(100,-2,2))
    dat <- transform(dat, y=x1+4*x2+3.6*x3+4.7*x4+rnorm(100,3,50))
    

    Make the fit

    fit <- lm(y~x1+x2+x3+x4, data=dat)
    

    Compute the predicted values

    dat$yhat <- predict(fit)
    

    Compute the residuals

    dat$resid <- residuals(fit)
    

    Get a vector of the variable names

    vars <- names(coef(fit))[-1]
    

    A plot can be made using this character representation of the name if you use it to build a string version of a formula and translate that. The four plots are below, and the are wrapped in a loop over all the vars. Additionally, this is surrounded by setting ask to TRUE so that you get a chance to see each plot. Alternatively you arrange multiple plots on the screen, or write them all to files to review later.

    opar <- par(ask=TRUE)
    for (v in vars) {
      plot(as.formula(paste("y~",v)), data=dat)
      plot(as.formula(paste("yhat~",v)), data=dat)
      plot(as.formula(paste("resid~",v)), data=dat)
      plot(as.formula(paste("t~",v)), data=dat)
    }
    par(opar)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several servers running the same code but each handling specific messages (they
in my project I have until now synchronized multiple threads (each running the same
When several threads are running the same piece of code, how CLR manages to
I have two cakephp2 applications running on same database, but having different Auth tables
I have 2 machines running same workers. One machine shoud be primary as it
multiple webapp running on same tomcat using same jvm. sometime, one webapp that have
I have two instances of VS2010 running on same machine.One VS instance has a
I am testing spawning off many threads running the same function on a 32
I have a bunch of remote machines all running the same WCF service over
I have multiple servers across the globe, running the same web application. The user

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.