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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:34:21+00:00 2026-06-07T18:34:21+00:00

In R, one can use the following command for plotting regression line: res=lm(height~age) abline(res)

  • 0

In R, one can use the following command for plotting regression line:

res=lm(height~age)
abline(res)

As suggested by http://msenux.redwoods.edu/math/R/regression.php

How can I do the same thing with rpy2? I tried

from rpy2 import robjects
r = robjects.r
r.png('test.png')
x = range(10)
y = range(10)
r.plot(x, y)
r.abline(r.lm(x, y))

but got complained by rpy2:

Error in formula.default(object, env = baseenv()) : invalid formula
Traceback (most recent call last):
  File "plot_ratio_price.py", line 34, in <module>
    r.abline(r.lm(x, y))
  File "/Library/Python/2.7/site-packages/rpy2/robjects/functions.py", line 82, in __call__
    return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/rpy2/robjects/functions.py", line 34, in __call__
    res = super(Function, self).__call__(*new_args, **new_kwargs)
rpy2.rinterface.RRuntimeError: Error in formula.default(object, env = baseenv()) : invalid formula

Any hints? 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-06-07T18:34:22+00:00Added an answer on June 7, 2026 at 6:34 pm

    Following on to @joran’s comment, Rpy2 requires you provide a special object for the formula. The documentation says that the class robjects.Formula represents an R formula. So prior to your last line (the call to r.abline) you need to create a Formula object and pass that in to your lm() call.

    By the way, your problem code looks close enough to the rpy2 example that you might consider using it as a template:

    import array
    from rpy2.robjects import IntVector, Formula
    from rpy2.robjects.packages import importr
    stats = importr('stats')
    
    x = IntVector(range(1, 10))
    y = IntVector(range(1, 10))
    
    fmla = Formula('y ~ x')
    env = fmla.environment
    env['x'] = x
    env['y'] = y
    
    fit = stats.lm(fmla)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

One can use a command such as the following to substitute the word before
Possible Duplicate: Deflate command line tool Yes, I know I can use PHP itself
I want a command line option like this one in the following KornShell (ksh)
In *nix systems one can use which to find out the full path to
In servlet 3.0 one can use startAsync to put long work in another thread,
According to man 5 proc , one can use the /proc filesystem to access
On Google App Engine to query the data store with Python, one can use
How can one use Triggers for Logging History of database changes in MySQL? If
I am learning WCF,one of the benefits of WCF is that you can use
How can I use UIViewAnimationTransition type in one of UINavigation / TabBar transitions ?

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.