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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:28:23+00:00 2026-06-15T06:28:23+00:00

I’m trying to do a simple linear regression on my data frame that looks

  • 0

I’m trying to do a simple linear regression on my data frame that looks something like what follows. The actual data set has more factors and more predictors (x’s) all trying to predict y.

f1 f2 x y
x  a  1 3.3
x  a  2 3.2
x  a  3 3.04
x  b  1 4.5
x  b  2 4.9
x  b  3 8
y  a  1 20.1
y  a  2 20.3
y  a  3 21.9
y  b  1 101.2
y  b  2 201.8
y  b  3 332.8

Notice, for every combination of f1 & f2 the trends vary. What I want to do is build a lm model for each combination of f1 & f2, store it in some kind of list and then when I call predict, I should be able to use the appropriate model and predict y based on x. I think I should use ldply to create a list of models, as shown below

lm.model.list = ldply(x,.(f1,f2),function(x) {
 fit = lm(x$y ~ x$x)
 return(fit)
 }

This gives an error,

Error: attempt to apply non-function

Also, assume I get it all into a list, how do I work with predict after that?

edit: I realize I could use indicator variables for the factors in the modelling itself, but I want to avoid this.

  • 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-15T06:28:25+00:00Added an answer on June 15, 2026 at 6:28 am

    I think what you want is just:

    fit <- lm(y ~ x+ f1*f2, data=dfrm)
    

    This will give a different prediction for each level of the interaction of f1 with f2. It’s just one model but could be “queried” for predictions with the predict function using any desired combo of f1 and f2. You should look at ?formula and spend some time understanding how linear models get interpreted.

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

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I want to construct a data frame in an Rcpp function, but when I

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.