Ask Your Question
1

use t.test from r in sage

asked 2017-08-30 18:26:34 +0100

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

hello, I have loaded two large numeric lists from a .sobj-file and stored them in two variables A and B. now I want to use the r.t.test(A, B) function from r in the interactive shell, but that doesn't work. is there a way I can load A and B into the r.console() and use them there, or can I use r.t.test() in sage somehow? I don't want to use any other alternative t-tests from sage, scipy, statsmodels, etc. It has to be the one from r! Thank you!

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2017-09-01 09:50:03 +0100

Emmanuel Charpentier gravatar image

Hmmm... Vincent, I beg to differ.

What's wrong with :

sage: A=[1,3,2,5]
sage: B=[2,2,1,3]
sage: r.t_test(A,B) ## Note : "t_test", NOT "t.test" !!! See below

Welch Two Sample t-test

data:  sage4 and sage9
t = 0.79241, df = 4.3033, p-value = 0.4695
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -1.806398  3.306398
sample estimates:
mean of x mean of y 
     2.75      2.00

?

And, BTW :

sage: r.t_test(A,B).sage()

{'DATA': {'alternative': 'two.sided',
  'conf_int': {'DATA': [-1.80639768054045, 3.30639768054045],
   'conf_level': 0.95},
  'data_name': 'sage5 and sage0',
  'estimate': {'DATA': [2.75, 2], '_Names': ['mean of x', 'mean of y']},
  'method': 'Welch Two Sample t-test',
  'null_value': {'DATA': 0, '_Names': 'difference in means'},
  'p_value': 0.469526519083806,
  'parameter': {'DATA': 4.3033359193173, '_Names': 'df'},
  'statistic': {'DATA': 0.792405815693061, '_Names': 't'}},
 '_Names': ['statistic',
  'parameter',
  'p.value',
  'conf.int',
  'estimate',
  'null.value',
  'alternative',
  'method',
  'data.name'],
 '_r_class': 'htest'}

The point is, don't forget to replace the "." in R's object's names with "_" (the dot is a "normal character" in R, whereas it has a syntactic value in Python ; the underscore is a "normal character" in Python, whereas it used to represent symbol assignment in (antique versions of) R).

This has been documented ; but I didn't check if this documentation still exists...

edit flag offensive delete link more

Comments

thanks a lot! also use ' " instead of just ' or " for strings, i.e:

r.t_test(A, B, conf_level = 0.975, alternative = '"less"')

frank f. gravatar imagefrank f. ( 2017-09-02 04:38:51 +0100 )edit

Nice! Would be cool to have this documented at

sage: r?

(which is really poor in information).

vdelecroix gravatar imagevdelecroix ( 2017-09-02 07:49:02 +0100 )edit
0

answered 2017-08-31 05:41:26 +0100

vdelecroix gravatar image

This is not straightforward in Sage (for now)

sage: A = [1, 3, 2, 5]
sage: B = [2, 2, 1, 3]
sage: Ar = r(A)
sage: Br = r(B)
sage: print r.eval('t.test(%s, %s)' %(Ar._name, Br._name))

        Welch Two Sample t-test

data:  sage4 and sage9
t = 0.79241, df = 4.3033, p-value = 0.4695
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -1.806398  3.306398
sample estimates:
mean of x mean of y 
     2.75      2.00
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2017-08-30 18:26:34 +0100

Seen: 478 times

Last updated: Sep 01 '17