Ask Your Question
0

R coding , packages ?

asked 2018-01-01 05:35:39 -0500

ortollj gravatar image

Hi

I would like to start in R language. is there way to access the function x = rbinom (1000,1, .5) or dbinom(46:54, 100, 0.5) I suppose I must include a package ?

"%r"
a = r([10.4,5.6,3.1,6.4,21.7]); a
sum(a)
x = rbinom(1000,1,.5)
total = sum(x)

NameError Traceback (most recent call last) <ipython-input-7-529997c59866> in <module>() 2 a = r([RealNumber('10.4'),RealNumber('5.6'),RealNumber('3.1'),RealNumber('6.4'),RealNumber('21.7')]); a 3 sum(a) ----> 4 x = rbinom(Integer(1000),Integer(1),RealNumber('.5')) 5 total = sum(x) NameError: name 'rbinom' is not defined**

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2018-01-01 06:07:19 -0500

tmonteil gravatar image

updated 2018-01-01 07:35:39 -0500

Not every R function is sent to the Sage namespace, in particular rbinom is not available directly, you have to specify that wou want to use this function from the r interface by typing r.rbinom instead:

sage: x = r.rbinom(1000,1,.5)
sage: x
   [1] 0 1 1 0 1 1 0 0 1 0 0 1 1 1 0 0 1 1 1 1 1 0 1 1 1 0 1 1 1 0 0 1 1 1 0 0 0 1 0 0 1 1 1 1 0 1 1
  [48] 0 1 0 1 0 1 0 1 0 1 0 0 0 0 1 1 1 1 1 1 0 1 1 1 1 1 0 1 1 0 0 0 0 1 0 0 0 0 0 1 0 1 1 0 0 1 1
  [95] 1 0 1 1 1 0 0 0 0 1 1 1 0 0 1 0 1 1 0 1 1 0 1 0 1 1 1 1 0 1 0 0 0 0 1 1 0 0 0 0 0 1 1 0 1 0 1
 [142] 0 0 0 0 1 0 0 0 0 0 1 0 0 1 1 0 0 1 1 0 1 1 0 0 1 1 1 0 0 0 0 1 1 1 1 1 1 0 1 1 1 0 1 1 0 0 0
 [189] 0 1 1 0 1 1 1 1 0 1 1 0 0 1 1 0 1 1 1 1 1 0 0 1 0 1 1 0 0 0 1 1 0 0 1 0 1 1 1 1 0 1 1 1 1 0 1
 [236] 0 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1 1 1 1 0 1 1 0 0 0 1 0 0 0 0 1 1 1 0 1 1 0 1 1 0 0 0 1 1
 [753] 0 1 0 1 0 1 1 0 0 1 0 0 1 1 1 0 1 1 0 1 0 1 0 0 1 0 0 1 0 1 1 0 0 1 0 1 1 0 1 1 0 1 0 1 0 1 0
 [800] 1 1 0 1 0 0 0 1 1 1 1 0 1 1 1 0 0 0 0 0 0 0 0 1 1 0 0 1 0 1 1 0 0 0 1 1 1 0 1 1 0 1 0 0 1 1 0
 [847] 1 0 1 0 1 1 1 1 1 1 1 0 1 1 0 0 1 1 0 1 1 1 0 0 0 1 0 0 1 1 0 1 1 1 1 1 0 1 1 1 0 0 1 1 1 0 1
 [894] 1 1 1 1 1 1 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 1 0 1 0 0 0 1 1 0 0 0 1 1 1 1 0 1 1 0 0 1 0 0 0 0
 [941] 1 1 1 1 0 1 0 1 1 1 0 1 0 1 1 1 0 0 1 1 1 0 1 0 1 0 0 1 1 1 0 1 1 1 0 1 1 0 1 0 1 1 1 1 1 1 0
 [988] 1 1 0 0 0 0 1 0 0 0 1 1 1
sage: sum(x)
[1] 508

If you want to use it many times and do not want to use the r.rbinom, you can always do:

sage: rbinom=r.rbinom
edit flag offensive delete link more

Comments

thank you tmonteil.

ortollj gravatar imageortollj ( 2018-01-01 06:30:22 -0500 )edit

but it seems r.sin(30:87) is not accepted ?

m=r.mean(a);m 
10*r.mean(r.sin(30:87))
ortollj gravatar imageortollj ( 2018-01-01 06:45:40 -0500 )edit

The Sage/Python interpreter tries to understand 30:87 which does not makes sense in Python, so either you have to turn it into something Pythonic:

sage: r.sin([30..87])

or you have to pass it raw (as a string) to r:

sage: r.sin(r('30:87'))
tmonteil gravatar imagetmonteil ( 2018-01-01 07:30:11 -0500 )edit

Ok thanks a lot tmonteil

just for info: R TRUE must be replaced by True, example:

"%r"
x = r('1:5');x
r.sample(x,3)
#r.sample(r('x,12,replace=TRUE')) # does not work 
#r.sample(r('x,12,replace=True')) # does not work 
r.sample(x,12,replace=True) # OK


#  Now let's roll the die 5000 times and see what fraction of the rolls give 6. We expect that about 1/6 of them will be. 
# Simulate 1000 rolls
x = r.sample(r('1:6'),1000, replace=True); x

# x == 6 gives a vector of TRUE or FALSE. R is clever, when we sum the vector: each TRUE counts as 1 and each FALSE counts as 0. So the sum is the number of TRUE's. In this case that means the number of 6's, which happens to be 168. 
r.sum(x == 6)

link text

ortollj gravatar imageortollj ( 2018-01-01 09:53:36 -0500 )edit

"%r" # Goal: estimate the probability of getting a sum of 7 when rolling two dice.

ntrials = 10000

x = r.matrix(r.sample(r('1:6'), 2*ntrials, replace=True), nrow=2, ncol=ntrials)

y = r.colSums(x)

r.mean(y == 7)

source: MIT Tutorial R coding

ortollj gravatar imageortollj ( 2018-01-04 07:51:28 -0500 )edit

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

Asked: 2018-01-01 05:35:39 -0500

Seen: 38 times

Last updated: Jan 01