-1

I'm struggling with this, can anyone help me plz? My current code is

N <- probspace(urnsamples(pcartas, 3,replace=FALSE, ordered=TRUE))
CDE <- intersect(subset(N, (N$outcomes[[]][[2]][1] == "Spade")), 
      subset(N, (N$outcomes[[]][[2]][2] == "Heart")), subset(N, (N$outcomes[[]][[2]][3] == "Club"))) 
pcorazones_diamantes_espadas <- Prob(CDE)

But it is not working. Thank you very much

  • 1
    Please show a small reprodcuible example. I see some strange indexing `N$outcomes[[]][[2]][1]` what is `[[]]`. Is that part of the package. Not sure without the package info andd reproducble example – akrun Feb 22 '20 at 18:31
  • 2
    Where do `probspace` and `urnsamples` come from? [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making a question that's easier to answer – camille Feb 22 '20 at 18:32
  • Why not just `13/52 * 13/51 * 13/50` (= 0.01656862745) – Allan Cameron Feb 22 '20 at 18:40
  • I must use r prob functions, to show that I know how they work but thanks – unknown1809 Feb 22 '20 at 21:07
  • From one deck of cards? From an urn containing 10 spades, 9 hearts, and 6 clubs? – Edward Feb 23 '20 at 03:40
  • What is `pcartas`? Can you `dput` this in your question? – Edward Feb 23 '20 at 03:43

1 Answers1

1

I think your pcartas object should be a simple vector, not a data frame. Otherwise you'll have difficulties with extracting elements of a list.

Clear the R workspace, install the prob package and load it.

rm(list=ls())
install.packages("prob")
library(prob)

Create the sample space.

pcartas <- cards()$suit

You'll see that this is a factor vector. I'm going to convert this to a character vector because the urnsamples function converts the labels into their numeric codes, which makes it difficult when you want to calculate the probabilities. You'll see the problem if you don't do this next step.

pcartas <- unclass(as.character(pcartas))

Sample without replacement from this sample space and convert to a probability space (your first line of code above):

L <- urnsamples(pcartas, size=3, replace=FALSE, ordered=TRUE);
N <- probspace(L); 

Calculate the probability of any event. For example, the probability of obtaining a "Spade" on the first card is:

Prob(N, X1=="Spade")  # Answer=0.25 or 13/52

And the probability of obtaining a spade on the first card and a heart on the second is:

Prob(N, X1=="Spade" & X2=="Heart")  # Answer=0.0637 or 13/52 * 13/51

And finally, the probability of obtaining a spade on the first card and a heart on the second card and a club on the third is:

Prob(N, X1=="Spade" & X2=="Heart" & X3=="Club") # Answer=0.016568 or 13/52 * 13/51 * 13/50

If you didn't convert the sample space object to a character vector, then you would have to use the numeric codes to get the probabilities:

Prob(N, X1==1 & X2==2 & X3==3) 
Edward
  • 10,360
  • 2
  • 11
  • 26