sample(x = 1:10, size = 4, replace = F)
#> [1] 1 2 3 49 Probability and Statistics
1 Generating random data
-
The function
sample()is used to generate random values from a vector, and it has the following arguments:-
x\(\rightarrow\) A vector of outcome you want to sample from -
size\(\rightarrow\) The number of samples (observations) you want to draw -
replace\(\rightarrow\) It can take eitherTRUEorFALSE -
prob\(\rightarrow\) Specifies probability of selection of different elements ofx
-
Select 10 numbers from 0 to 100
sample(x = 0:100, size = 10, replace = F) # replace=FALSE
#> [1] 52 61 82 41 78 66 49 57 12 7sample(x = 0:100, size = 10, replace = T) # replace=TRUE
#> [1] 78 56 75 59 66 70 9 32 81 72- Select students’ grades randomly
sample(replace = TRUE, x = LETTERS[1:4], size = 10)
#> [1] "A" "A" "C" "C" "B" "A" "A" "A" "C" "B"- Tossing a fair coin 10 times
- Tossing a biased coin 10 times
2 Use of initial seed in generating random numbers
4 rbinom() and rnorm
-
rbinom()is used to draw a sample from a binomial distributionsize\(\rightarrow\) number of Bernoulli trialsprob\(\rightarrow\) probability of successn\(\rightarrow\) number of observations
Draw a sample of size 8 from \(B(10, 0.75)\)
rbinom(size = 10, prob = .75, n = 8)
#> [1] 9 8 8 6 8 7 9 7-
rnorm()is used to draw a sample from a normal distributionmean\(\rightarrow\) mean of the distribution \((\mu)\)sd\(\rightarrow\) standard deviation of the distribution \((\sigma)\)n\(\rightarrow\) number of observations
Draw a sample of size 5 from \(N(10, 16)\)
rnorm(mean = 10, sd = 4, n = 5)
#> [1] 14.743527 8.970948 11.748854 8.539669 11.986696
5 pnorm()
For \(X \sim N(50, 3^2)\), find \(P(45<X<55)\).
\(P(a < X ≤ b) = F(b) − F(a)\)
6 dnorm()
- For \(X \sim Bin(10, 0.5)\), find \(P(X=5)\).
dbinom(x = 5, size = 10, prob = 0.5)
#> [1] 0.2460938
7 qnorm()
- Let \(Z\) follows a standard normal distribution. Then the 0.975−quantile is \(Z_{0.975} ≈ 1.96\). It means the probability of sampling a value less than or equal to 1.96 is 0.975 or \(97.5%\)
qnorm(p = 0.975, mean = 0, sd = 1)
#> [1] 1.959964