In: Computer Science
Codes for this R-Project please!
In this projects we will draw plots for different discrete
probability distributions that we have studied in class.
# 1. Binomial distribution.
We will plot the density function for the binomial distribution
Bin(n, p).
Note:
1) The values for this random variable are 0, 1, 2, ..., n.
2) The density plot will have a bar of height P(X=k), at the point
'k' on the x-axis.
3) In the plot include a vertical line at the expected value of
Bin(n,p).
Write a function plot_binom, that takes input values: n and p, and returns the density plot of Bin(n,p).
```{r}
# plot_binom <- function(n,p){
# x <- 0:n
# bin <- dbinom( _____ ) # input appropriate parameters
# mu_X <- ____ #input appropriate value for expected value
# plot(x, bin, type = "h", lwd = 5,
# main = paste("Binom density: n = ", n ,"and p = ", p),
# xlab = "x", ylab = "P(X=x)")
# abline(v = mu_X, col = 'red', lwd = 4)
# }
```
Fix n = 40. Compute plots for the following values of p: 0.05, 0.1,
0.4, 0.6, 0.9, 0.95.
Use the command "par(mfrow=c(3,2))" to have all the plots on the
same frame.
```{r}
# n <- 40
# prob_vals <- c(_______) #input appropriate values for
probability
# par(mfrow=c(3,2))
# for (p in prob_vals){
# plot_binom(____) #input the appropriate values
#}
```
Write at least two observations that you can note from these plots. Consider skewness and symmetry.
Work Screenshot :
Plotting Screen shot:
Code
plot_binom <- function(n,p){
x <- 0:n
bin <- dbinom(x,n,p )
mu_X <- 15
plot(x, bin, type = "h", lwd = 5,
main = paste("Binom density: n = ", n ,"and p = ", p),
xlab = "x", ylab = "P(X=x)")
abline(v = mu_X, col = 'red', lwd = 4)
}
n <- 40
prob_vals <- c(0.05, 0.1, 0.4, 0.6, 0.9, 0.95)
par(mfrow=c(3,2))
for (p in prob_vals){
plot_binom(n,p)
}
For this code I have set expected value to be 30
After plotting the graphs , It is observed that
1. The distribution is symmetric around the mean value, when p value is nearer to 0.5.
2. When p > 0.5, the distribution is skewed to the left.
3. When p < 0.5, the distribution is skewed to the right.
I hope , I have completed your assignment and also provided the observations from the plotting in terms of symmetry and skewness.
If further customization needed , please specify.?
Thank You?