RCall : how to get values of a R object?

Hi,

Some R functions retrieve many statistical parameters such as pvalue, confidence interval etc… I would like to know how it is possible to extract those parameters from a R object ? For example here how can I get pvalue, t, df or the confidence interval of a simple t test ?

``````julia> using RCall

julia> x = randn(10)
10-element Array{Float64,1}:
-0.47693327074660957
1.0387442907193456
1.471145177183246
1.2446523937552667
-0.2859950512876116
-0.237134944977618
-0.46232255238398723
-0.5811878146338813
1.6876079198157776
-1.2109048316637905

julia> r = R"t.test(\$x)"
RObject{VecSxp}

One Sample t-test

data:  `#JL`\$x
t = 0.67176, df = 9, p-value = 0.5186
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
-0.5179367  0.9554709
sample estimates:
mean of x
0.2187671

julia> fieldnames(RObject{VecSxp})
(:p,)

julia> getfield(r,:p)
Ptr{VecSxp} @0x000000000355c7d8

julia> r.p
Ptr{VecSxp} @0x000000000355c7d8
``````

Maybe this way:

``````R> pValue <- t.test(\$x)\$p.value

R> pValue
[1] 0.7442176

julia> pValue = @rget(pValue)
0.744217565219065
``````

The rest in a similar way:

``````R> str(t.test(\$x))
List of 10
\$ statistic  : Named num 0.336
..- attr(*, "names")= chr "t"
\$ parameter  : Named num 9
..- attr(*, "names")= chr "df"
\$ p.value    : num 0.744
\$ conf.int   : num [1:2] -0.661 0.892
..- attr(*, "conf.level")= num 0.95
\$ estimate   : Named num 0.115
..- attr(*, "names")= chr "mean of x"
\$ null.value : Named num 0
..- attr(*, "names")= chr "mean"
\$ stderr     : num 0.343
\$ alternative: chr "two.sided"
\$ method     : chr "One Sample t-test"
\$ data.name  : chr "`#JL`\$x"
- attr(*, "class")= chr "htest"
``````
1 Like

@Fred, it was already late when I wrote my first answer and therefore this supplement: Why do you take R for the t-test? Julia also offers the t-Test in all variations:

julia> EqualVarianceTTest(x, y)

Population details:
parameter of interest: Mean difference
value under h_0: 0
point estimate: -0.19901727472999997
95% confidence interval: (-0.4737, 0.0757)
Test summary:
outcome with 95% confidence: fail to reject h_0
two-sided p-value: 0.1546
Details:
number of observations: [100,100]
t-statistic: -1.4287730680428585
degrees of freedom: 198
empirical standard error: 0.13929243151441464

Attention, advertising block! The t-test with Julia and RCall is described here: ISBN-13: 978-3749485086

In fact the t.test is just a simple example for illustration purpose. What I tried do do is much more complex : I have to computes a lot of hazard ratios and their pvalues. Unfortunately in Julia package I did not found the equivalent of the R packages.

I thought to use the hazard ratio from R into Julia. But, the functions I need are not in R base, and I have to load at least 2 R packages

``````library(survival)
library(qvalue)
``````

I realized that for each hasard ratio + pvalue computation I will have to load not only a R function but also some R packages, such as

``````julia> R"""
library(survival)
library(qvalue)
hazRatio  <- function(time, status, Groupe2, tab){
# computations
}
...
"""
``````

I thus imagined that the time I would gain computing loops in Julia will be certainly lost by the overaid of a lot of many “heavy” RCalls (loading not only functions but packages). Do you agree ?

So I finally continue to do that in pure R

@Fred, possibly. If you want to use some R-packages and even more R-functions, which are not available in Julia (-packages) yet, it is surely right to work with R. Creating an R script and then importing the required R information into Julia is certainly too much effort. I once did a PCA (This is my sample script page for the book) in Julia with R support, just for info.

I wish you good luck!