Finding the Distribution of a sample - r

Hello everyone I’m a beginner and I am having issues setting up this problem. Let's say you toss two coins. Let x be the resulting number of heads and y be the number of tails. I need to find the distribution in R. Here is what I have attempted:
Sample(0:2, x,y, size=10), prob=1/4, replace=true)

You can use the sample function to generate random samples as suggested by the comments.
Your experiment is to toss 2 coins, but you didn't specify the number of times. Let's start with ten, as your attempted code suggests.
The sample function has a prob argument to specify the probabilities. The number of heads obtained from tossing 2 coins can be either 0, 1, or 2 (as your code correctly specifies) and these frequencies will occur with probabilities 1/4, 1/2, and 1/4, respectively. So, the R command would be:
n.heads <- sample(0:2, size=10, replace=TRUE, prob=c(1/4, 1/2, 1/4)); n.heads
[1] 0 1 1 1 2 1 1 0 2 1
And you can visualise this using a barplot:
barplot(table(n.heads)/10)
For illustration purposes, it is good practice to set the random number seed so others can follow.
set.seed(1)
n.heads <- sample(0:2, size=10, replace=TRUE, prob=c(1/4, 1/2, 1/4)); n.heads
[1] 1 1 2 0 1 0 0 2 2 1
barplot(table(n.heads)/10)
You may like to increase the size argument to see how the distribution changes. The number of tails should be similar (by symmetry).

Related

How to generate a series of number by function of sample in R, with given different probability in each try?

For example I have a vector about possibility is
myprob <- (0.58, 0.51, 0.48, 0.46, 0.62)
And I want to sampling a series of number between 1 and 0 each time by the probability of c(1-myprob, myprob),
which means in the first number in the series, the function sample 1 and 0 by (0.42, 0.58), the second by (0.49, 0.50) and so on,
how can I generate the 5 numbers by sample?
The syntax of
Y <- sample(c(1,0), 1, replace=F, prob=c(1-myprob, prob))
would have incorrect number of probabilities and only 1 number output if I specify the prob;
while the syntax of
Y <- sample(c(1,0), 5, replace=F, prob=c(1-myprob, prob))
would have the probabilities focus on only 0.62(or not I am not sure, but the results seems not correct at all)
Thanks for any reply in advance!
If myprob is the probability of drawing 1 for each iteration, then you can use rbinom, with n = 5 and size = 1 (5 iterations of a 1-0 draw).
set.seed(2)
rbinom(n = 5, size = 1, prob = myprob)
[1] 1 0 1 0 0
Maël already proposed a great solution sampling from a binomial distribution. There are probably many more alternatives and I just wanted to suggest two of them:
runif()
as.integer(runif(5) > myprob)
This will first generate a series of 5 uniformly distributed random numbers between 0 and 1, then compare that vector against myprob and convert the logical values TRUE/FALSE to 1/0.
vapply(sample())
vapply(myprob, function(p) sample(1:0, 1, prob = c(1-p, p)), integer(1))
This is what you may have been looking for in the first place. This executes the sample() command by iterating over the values of myprob as p and returns the 5 draws as a vector.

How do I sample real numbers?

Is it possible to sample real numbers with the sample() function? (I.e., sample from a continuous distribution, e.g. [1,10] so that when I sample I won't get an integer but a real number, e.g 5.467.)
If not, is there another function to do that?
The purpose of sample is to... well sample, uniformly or not, with replacement or not, from a finite set.
If you want a sample from another kind of distribution, it depends on the distribution, and there are many functions in R to achieve that. The most common are runif (uniform distribution on a bounded interval) and rnorm (the normal distribution).
See ?sample and ?Distributions in the documentation. A more comprehensive list can be found here.
For instance:
Uniform sample with replacement, from {1, 2, 3, 4}:
> sample(1:4, 10, replace=T)
[1] 3 1 4 1 2 1 1 3 4 3
Uniform sample from [0, 1]:
> runif(10)
[1] 0.6529710 0.1977511 0.7946746 0.6351793 0.9518663
0.9356524 0.2945506 0.4623502 0.8198065 0.8516961
Normal sample with mean 0 and variance 1:
> rnorm(10)
[1] -1.13195127 0.43434313 -0.44183752 0.13632993 0.75902968
0.76079877 0.37919727 0.41350485 0.76233633 0.09400158
These functions can take parameters: runif(n, a, b) yields n samples from the interval [a, b] for instance.

how can I decide a series of 0-1 combination is stochastically distributed?

I have a series composed by 0 and 1, and the 0 shows up without specfic order (as far as I can tell), how can I decide if the 0 is stochastically distributed?
pls find the toy sample for reference
library(magrittr)
s1 <- runif(10)*10 %>% mod(10) %>% round(0) %>% `>`(5) %>% ifelse(1,0)
s2 <- c(0,0,1,0,1,1,1,0,1,0)
The runs test is what you want:
The Wald–Wolfowitz runs test (or simply runs test), named after
statisticians Abraham Wald and Jacob Wolfowitz is a non-parametric
statistical test that checks a randomness hypothesis for a two-valued
data sequence. More precisely, it can be used to test the hypothesis
that the elements of the sequence are mutually independent.
It is implemented in the snpar package.
Are you looking for rbinom? This function simulates a Bernoulli process with a chance of success (1) equal to some probability p. Otherwise, the result is 0.
The usage of rbinom is rbinom(n, size, prob), where n is the number of random numbers to generate, size is the number of trials, and prob is the probability of getting a success. So to generate a bunch of binomial random numbers with equal probability of 1 or 0, use:
set.seed(100) # for reproducibility
rbinom(n = 10, size = 1, prob = 0.5)
[1] 0 0 1 0 0 0 1 0 1 0

Confusion Between 'sample' and 'rbinom' in R

Why are these not equivalent?
#First generate 10 numbers between 0 and .5
set.seed(1)
x <- runif(10, 0, .5)
These are the two statements I'm confused by:
#First
sample(rep(c(0,1), length(x)), size = 10, prob = c(rbind(1-x,x)), replace = F)
#Second
rbinom(length(x), size = 1, prob=x)
I was originally trying to use 'sample'. What I thought I was doing was generating ten (0,1) pairs, then assigning the probability that each would return either a 0 or a 1.
The second one works and gives me the output I need (trying to run a sim). So I've been able to solve my problem. I'm just curious as to what's going on under the hood with 'sample' so that I can understand R better.
The first area of difference is the location of the length of the vector specification in the parameter list. The names size have different meanings in these two functions. (I hadn't thought about that source of confusion before, and I'm sure I have made this error myself many times.)
The random number generators (starting with r and having a distribution suffix) have that choice as the first parameter, whereas sample has it as the second parameter. So the length of the second one is 10 and the length of the first is 1. In sample the draw is from the values in the first argument, while 'size' is the length of the vector to create. In the rbinom function, n is the length of the vector to create, while size is the number of items to hypothetically draw from a theoretical urn having a distribution determined by 'prob'. The result returned is the number of "ones". Try:
rbinom(length(x), size = 10, prob=x)
Regarding the argument to prob: I don't think you need the c().
The difference between the two function is quite simple.
Think of a pack of shuffled cards, and choose a number of cards from it. That is exactly the situation that sample simulates.
This code,
> set.seed(123)
> sample(1:40, 5)
[1] 12 31 16 33 34
randomly extract five numbers from the 1:40 vector of numbers.
In your example, you set size = 1. It means you choose only one element from the pool of possible values. If you set size = 10 you will get ten values as you desire.
set.seed(1)
x <- runif(10, 0, .5)
> sample(rep(c(0,1), length(x)), size = 10, prob = c(rbind(1-x,x)), replace = F)
[1] 0 0 0 0 0 0 0 1 0 1
Instead, the goal of the rbinom function is to simulate events where the results are "discrete", such as the flip of a coin. It considers, as parameters, the probability of success on a trial, such as the flip of the coin, according to a given probability of 0.5. Here we simulate 100 flips. If you think that the coin could be stacked in order to favor one specific outcome, we could simulate this behaviour by setting probability equals to 0.8, as in the example below.
> set.seed(123)
> table(rbinom(100, 1, prob = 0.5))
0 1
53 47
> table(rbinom(100, 1, prob = 0.8))
0 1
19 81

Choose specific number with probability

How can one choose a number with a specific probability p?
Say we must choose between {0, 1} and the probability p stands for choosing 1.
So when p=0.8 we choose 1 with 80% and 0 with 20%.
Is there a simple solution in R for this?
Take a look at sample function.
> set.seed(1)
> sample(c(0,1), size=10, replace=TRUE, prob=c(0.2,0.8))
[1] 1 1 1 0 1 0 0 1 1 1
From the helpfile you can read:
sample takes a sample of the specified size from the elements of x using either with or without replacement.
and the argument prob in sample acts as ...
A vector of probability weights for obtaining the elements of the vector being sampled.

Resources