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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:26:37+00:00 2026-05-12T15:26:37+00:00

In an effort to help populate the R tag here, I am posting a

  • 0

In an effort to help populate the R tag here, I am posting a few questions I have often received from students. I have developed my own answers to these over the years, but perhaps there are better ways floating around that I don’t know about.

The question: I just ran a regression with continuous y and x but factor f (where levels(f) produces c("level1","level2"))

 thelm <- lm(y~x*f,data=thedata)

Now I would like to plot the predicted values of y by x broken down by groups defined by f. All of the plots I get are ugly and show too many lines.

My answer: Try the predict() function.

##restrict prediction to the valid data 
##from the model by using thelm$model rather than thedata

 thedata$yhat <- predict(thelm,
      newdata=expand.grid(x=range(thelm$model$x),
                          f=levels(thelm$model$f)))

 plot(yhat~x,data=thethedata,subset=f=="level1")
 lines(yhat~x,data=thedata,subset=f=="level2")

Are there other ideas out there that are (1) easier to understand for a newcomer and/or (2) better from some other perspective?

  • 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-12T15:26:37+00:00Added an answer on May 12, 2026 at 3:26 pm

    The effects package has good ploting methods for visualizing the predicted values of regressions.

    thedata<-data.frame(x=rnorm(20),f=rep(c("level1","level2"),10))
    thedata$y<-rnorm(20,,3)+thedata$x*(as.numeric(thedata$f)-1)
    
    library(effects)
    model.lm <- lm(formula=y ~ x*f,data=thedata)
    plot(effect(term="x:f",mod=model.lm,default.levels=20),multiline=TRUE)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm here again asking questions. I hope somebody would put some effort in answering
Do any libraries or other development resources exist that can help reduce the effort
After going through the effort of finally purging distro ruby packages from my Ubuntu
I have written the following code in an effort to try and compute the
Guys i need some help here , im getting this error here at the
In a seemingly never ending effort to learn more about iphone development, I have
I have different questions about a full architecture idea. I hope someone with great
I have a report which is generated from different models and I am displaying
I try to keep the Java Runtime Environment from being enabled in an effort
Because jQuery is a widely used and mature collaborative effort, I can't help but

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.