linear.predictors NULL when using ro.r.glm; works using rmagic

Issue #378 invalid
GregS created an issue

Running the following model:

formula = Formula('pass~n')
fit = ro.r.glm(formula, family = R('binomial(link = "logit")'), data = analytical_set)

Trying to extract linear.predictors:

s = ro.r.summary(fit)
s.rx2('linear.predictors')

yields:

Out[13]: 
rpy2.rinterface.NULL

Run model using rmagic:

%load_ext rpy2.ipython
%R -i analytical_set
%R formula <- 'pass ~ n'
%R fit <- glm(formula, data=analytical_set, family=(binomial(link="logit")))

Pull into python:

%Rpull  fit

Print out by name:

fit.rx2('linear.predictors')
Out[26]: 

R object with classes: ('numeric',) mapped to:
<FloatVector - Python:0x10d2d3a70 / R:0x7ff71bc7e600>
[1.559446, 1.559446, 1.559446, ..., 1.559446, 1.559446, 1.559446]

Comments (6)

  1. Laurent Gautier

    I am marking this as invalid because I do not think that this is a problem with rpy2, but rather a question (to ask on StackOverflow).

    Note: please provide self-sufficient code. This makes the life of would-be helpers much easier (and increase the chances of receiving help). For example, here is some setup:

    import rpy2.robjects.packages as rpacks
    from rpy2.robjects import Formula
    
    stats = rpacks.importr("stats")
    datasets = rpacks.importr("datasets")
    iris = rpacks.data(datasets).fetch("iris")["iris"]
    

    Calling glm is almost straightforward. The catch is that sometimes R functions can interchangeably take symbols or strings, as the lazy evaluation of parameters in a function call makes it possible.

    The most frequent example would be when loading an R package in R: library("utils") or library(utils) will work. In the latter case utils does not exist as a symbol but will be turned to a string inside the function library(). Here the same happens for "logit", and this is why it is working in the magic, but not when in Python. There is unfortunately no way to predict when this is the case (and this can be a problem when writing R code - outside of rpy2). With that in mind, the call to glm is then:

    fit = stats.glm(formula = Formula('Species ~ Sepal.Length'),
                    family = stats.binomial(link = "logit"),
                    data=iris)
    

    Our linear predictors are then:

    >>> fit.rx2("linear.predictors")
             1          2          3          4          5          6          7                                                   
    -1.4324610 -2.4676006 -3.5027402 -4.0203100 -1.9500308  0.1202485 -4.0203100                                                   
             8          9         10         11         12         13         14                                                   
    -1.9500308 -5.0554496 -2.4676006  0.1202485 -2.9851704 -2.9851704 -5.5730195 
    (...)
    
  2. GregS reporter

    Thanks! (It worked! I see what you mean by "almost straightforward")... I'll stick to StackOverflow for these types of problems.

  3. Log in to comment