R
"R is a language and environment for statistical computing and graphics." -- http://www.r-project.org/
Sage includes R and is able to invoke it. It is possible to send lines of code, invoke commands in a standardized fashion, work with results of calculations as objects and convert those objects back into python data structures. The last point is essential to write native Sage commands that use R as backend.
Work
http://trac.sagemath.org/sage_trac/ticket/839
Command Completion
sage: r.k[TAB] r.kappa r.kappa_qr r.kernel r.knots r.ks_test r.kappa_default r.kappa_tri r.kill r.kronecker r.ksmooth r.kappa_lm r.kernapply r.kmeans r.kruskal_test
Examples
sequence
sage: r.seq(1,9) [1] 1 2 3 4 5 6 7 8 9
Matrix
Matrix m as Sage object and R printout - then a translation to a Sage matrix.
sage: m = r.matrix(r.seq(1,12),3)
sage: type(m)
<class 'sage.interfaces.r.RElement'>
sage: m
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12argument byrow=True
sage: m = r.matrix(r.seq(1,12),3, byrow=True)
sage: m
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12translation:
sage: sageobj(m)
or
sage: m._sage_() [ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12] sage: type(sageobj(m)) <type 'sage.matrix.matrix_integer_dense.Matrix_integer_dense'>
t-Test
sage: r.t_test(r.c(1,2,3,5),r.c(1,2,4,5))
or just
sage: r.t_test([1,2,3],[1,2,4])
Welch Two Sample t-test
data: sage6 and sage7
t = -0.2, df = 5.973, p-value = 0.8481
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-3.311938 2.811938
sample estimates:
mean of x mean of y
2.75 3.00 and native
sage: t = ttest([1,2,3,4,5],[1,2,3,3.5,5.121])
sage: t
(0.941026372027427,
{'DATA': {'alternative': 'two.sided',
'conf_int': {'DATA': [-2.2140805547522402, 2.3656805547522399],
'conf_level': 0.94999999999999996},
'data_name': 'sage5 and sage11',
'estimate': {'DATA': [3, 2.9241999999999999],
'_Names': ['mean of x', 'mean of y']},
'method': 'Welch Two Sample t-test',
'null_value': {'DATA': 0, '_Names': 'difference in means'},
'p_value': 0.941026372027427,
'parameter': {'DATA': 7.9983864630024701, '_Names': 'df'},
'statistic': {'DATA': 0.076336405881313296, '_Names': 't'}},
'_Names': ['statistic',
'parameter',
'p.value',
'conf.int',
'estimate',
'null.value',
'alternative',
'method',
'data.name'],
'_r_class': 'htest'})
sage: t[0]
0.941026372027427
sage: ttest([1,2,3,4,5],[1,2,3,3.5,5.121])[0]
0.941026372027427
table
sage: rt = r.table(x = r.c(3,3,3,1,2,3,1), y = r.c(2,1,2,2,2,1,2))
sage: rt
x
y 1 2 3
1 0 0 2
2 2 1 2
sage: rt.summary()
Number of cases in table: 7
Number of factors: 2
Test for independence of all factors:
Chisq = 2.1, df = 2, p-value = 0.3499
Chi-squared approximation may be incorrect
F. A. Q.
- Q: "r" doesn't work!
A: You have probably assigned a new value to it - just assign it again: r = R() or any other variable you like!
Q: I cannot pass string to R function, getting errors like Error: object "sage1" not found or random output
A: Use double-quoted strings inside single-quoted strings, like r.paste(1, 2, sep='"+"') - maybe as you read this it's no longer needed, check http://trac.sagemath.org/sage_trac/ticket/2963