In R Language generate 100 binomial random numbers with para
In R Language, generate 100 binomial random numbers with parameters n = 4 and p = 0:7. Please set the
seed as set.seed(480).
(a) Draw a barplot of the sample.
(b) Calculate the mean, standard deviation and variance of the sample.
(c) What proportion of the sample is below 2?
(d) Compare (b) and (c) with the theoretical values that you learned in elementary statistics.
Solution
set.seed(480)
bsamples <- rbinom(100, 4, 0.7)
barplot(bsamples, main=\"Binomial Distrbution 100 Samples, n = 4, p = 0.7\",ylab=\"Outcome\", col = rainbow(20), xlab=\"Trial\")
mn <- mean(bsamples)
ssd <- sd(bsamples)
svar<- var(bsamples)
print(paste(\"Mean =\",mn))
print(paste(\"Standard Deviation =\",ssd))
print(paste(\"Variance =\",svar))
prop2 <- length(bsamples[bsamples < 2])/length(bsamples)
print(paste(\"Proprtion of Sample Below 2 = \", prop2))
### Probability of getting less than 2 on every trial is, p2
p2 <- 4*(.3**3)*.7 + (.3**4)
pratio <- (prop2-p2)/ssd
print(paste(\"Deviation from theoretical value = \", pratio))
