R by Example Chapter 1

Problem 1: Normal Percentiles

Here is the 25th, 50th, and 75th percentile for the standard normal curve

qnorm(c(0.25, 0.5, 0.75))
## [1] -0.6745  0.0000  0.6745

Problem 2: Chi-square density curve

curve(dchisq(x, 1), 0, 20, xlab = "x", ylab = "dchisq(x,1)")
title("Chi Square density")

plot of chunk unnamed-chunk-2

Problem 3: Gamma densities

curve(dgamma(x, 2, 1), 0, 20, xlab = "x", ylab = "dgamma(x,shape,rate=1)", col = "red")
curve(dgamma(x, 3, 1), 0, 20, add = TRUE, col = "blue")
title("gamma densities")
legend("topright", inset = 0.05, title = "shape", legend = c("2", "3"), lty = 1, 
    col = c("red", "blue"), bg = "wheat1")

plot of chunk unnamed-chunk-3

Problem 4: Binomial Probabilities \[ P(X=k)= {n \choose k} p^k(1-p)^{n-k} \]

p <- 1/6
n <- 12
bin <- function(n, p, k) choose(n, k) * p^k * (1 - p)^{
    n - k
}
x <- seq(0, n)
bin(n, p, x)
##  [1] 1.122e-01 2.692e-01 2.961e-01 1.974e-01 8.883e-02 2.842e-02 6.632e-03
##  [8] 1.137e-03 1.421e-04 1.263e-05 7.580e-07 2.756e-08 4.594e-10