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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:55:10+00:00 2026-06-13T14:55:10+00:00

How can I add RMSE, slope, intercept and r^2 to a plot using R?

  • 0

How can I add RMSE, slope, intercept and r^2 to a plot using R? I have attached a script with sample data, which is a similar format to my real dataset–unfortunately, I am at a stand-still. Is there an easier way to add these statistics to the graph than to create an object from an equation and insert that into text()? I would ideally like the statistics to be displayed stacked on the graph. How can I accomplish this?

## Generate Sample Data
x = c(2,4,6,8,9,4,5,7,8,9,10)
y = c(4,7,6,5,8,9,5,6,7,9,10)

# Create a dataframe to resemble existing data
mydata = data.frame(x,y)

#Plot the data
plot(mydata$x,mydata$y)
abline(fit <- lm(y~x))

# Calculate RMSE
model = sqrt(deviance(fit)/df.residual(fit))

# Add RMSE value to plot
text(3,9,model)
  • 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-13T14:55:11+00:00Added an answer on June 13, 2026 at 2:55 pm

    Here is a version using base graphics and ?plotmath to draw the plot and annotate it

    ## Generate Sample Data
    x = c(2,4,6,8,9,4,5,7,8,9,10)
    y = c(4,7,6,5,8,9,5,6,7,9,10)
    
    ## Create a dataframe to resemble existing data
    mydata = data.frame(x,y)
    
    ## fit model
    fit <- lm(y~x, data = mydata)
    

    Next calculate the values you want to appear in the annotation. I prefer bquote() for this, where anything marked-up in .(foo) will be replaced by the value of the object foo. The Answer @mnel points you to in the comments uses substitute() to achieve the same thing but via different means. So I create objects in the workspace for each value you might wish to display in the annotation:

    ## Calculate RMSE and other values
    rmse <- round(sqrt(mean(resid(fit)^2)), 2)
    coefs <- coef(fit)
    b0 <- round(coefs[1], 2)
    b1 <- round(coefs[2],2)
    r2 <- round(summary(fit)$r.squared, 2)
    

    Now build up the equation using constructs described in ?plotmath:

    eqn <- bquote(italic(y) == .(b0) + .(b1)*italic(x) * "," ~~ 
                      r^2 == .(r2) * "," ~~ RMSE == .(rmse))
    

    Once that is done you can draw the plot and annotate it with your expression

    ## Plot the data
    plot(y ~ x, data = mydata)
    abline(fit)
    text(2, 10, eqn, pos = 4)
    

    Which gives:

    enter image description here

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

Sidebar

Related Questions

We can add an image to an photo album using UIImage *img = [UIImage
Can someone please direct me on how I can add something similar to inputAccessoryView
I wrote library which can add and update objects in salesforce. I use beatbox
I can add a Combobox to a DataGrid using following xmal: <local:DataGridTemplateColumn Header=SomeHeader Width=106
I can add bytes to a NSMutableData instance easily by using the appendData method,
You can add product to compare. I have to show the link Add to
How can I add an encoding attribute to the body tag using Savon? Some
You can add as many text boxes as you want as shown by using
How I can add Header params in restlet android? I have following code: ClientResource
I can add a QPixMapImage to a QGraphicsScene, but then I have two issues.

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.