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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:10:44+00:00 2026-05-28T04:10:44+00:00

I am using rpy2 for regressions. The returned object has a list that includes

  • 0

I am using rpy2 for regressions. The returned object has a list that includes coefficients, residuals, fitted values, rank of the fitted model, etc.)

However I can’t find the standard errors (nor the R^2) in the fit object. Running lm directly model in R, standard errors are displayed with the summary command, but I can’t access them directly in the model’s data frame.

How can I get extract this info using rpy2?

Sample python code is

from scipy import random
from numpy import hstack, array, matrix
from rpy2 import robjects 
from rpy2.robjects.packages import importr

def test_regress():
    stats=importr('stats')
    x=random.uniform(0,1,100).reshape([100,1])
    y=1+x+random.uniform(0,1,100).reshape([100,1])
    x_in_r=create_r_matrix(x, x.shape[1])
    y_in_r=create_r_matrix(y, y.shape[1])
    formula=robjects.Formula('y~x')
    env = formula.environment
    env['x']=x_in_r
    env['y']=y_in_r
    fit=stats.lm(formula)
    coeffs=array(fit[0])
    resids=array(fit[1])
    fitted_vals=array(fit[4])
    return(coeffs, resids, fitted_vals) 

def create_r_matrix(py_array, ncols):
    if type(py_array)==type(matrix([1])) or type(py_array)==type(array([1])):
        py_array=py_array.tolist()
    r_vector=robjects.FloatVector(flatten_list(py_array))
    r_matrix=robjects.r['matrix'](r_vector, ncol=ncols)
    return r_matrix

def flatten_list(source):
    return([item for sublist in source for item in sublist])

test_regress()
  • 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-28T04:10:44+00:00Added an answer on May 28, 2026 at 4:10 am

    So this seems to work for me:

    def test_regress():
        stats=importr('stats')
        x=random.uniform(0,1,100).reshape([100,1])
        y=1+x+random.uniform(0,1,100).reshape([100,1])
        x_in_r=create_r_matrix(x, x.shape[1])
        y_in_r=create_r_matrix(y, y.shape[1])
        formula=robjects.Formula('y~x')
        env = formula.environment
        env['x']=x_in_r
        env['y']=y_in_r
        fit=stats.lm(formula)
        coeffs=array(fit[0])
        resids=array(fit[1])
        fitted_vals=array(fit[4])
        modsum = base.summary(fit)
        rsquared = array(modsum[7])
        se = array(modsum.rx2('coefficients')[2:4])
        return(coeffs, resids, fitted_vals, rsquared, se) 
    

    Although, as I said, this is literally my first foray into RPy2, so there may be a better way to do that. But this version appears to output arrays containing the R-squared value along with the standard errors.

    You can use print(modsum.names) to see the names of the components of the R object (kind of like names(modsum) in R) and then .rx and .rx2 are the equivalent of [ and [[ in R.

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

Sidebar

Related Questions

Using C#, I need a class called User that has a username, password, active
Using ASP.NET MVC there are situations (such as form submission) that may require a
Using TortoiseSVN against VisualSVN I delete a source file that I should not have
Using C# and System.Data.SqlClient, is there a way to retrieve a list of parameters
I'm using rpy2 to do data analysis and plotting in python. It works fine
Using the navigator.geolocation object in JavaScript. Trying to establish accurate ranges, but wondering exactly
Using android 2.3.3, I have a background Service which has a socket connection. There's
I am trying to access the as.vector R function from within Python, using rpy2.
Using C++ preprocessor directives, is it possible to test if a preprocessor symbol has
Here's what should and does happen using rpy2.robjects.packages.importr for base R-packages (e.g. stats ):

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.