# Density of N(100,10) curve(dnorm(x, mean = 100, sd = 10), xlim = c(70, 130), ylab = "density") abline(v = 100, lty = "dashed", col = "red") abline(v = c(80, 120), lty = "dashed", col = "blue") # Density of normal with different locations curve(dnorm(x, mean = -5, sd = 1), xlim = c(-10, 15), ylab = "density") curve(dnorm(x, mean = 0, sd = 1), add = TRUE, col = "darkgreen") curve(dnorm(x, mean = 5, sd = 1), add = TRUE, col = "gold") curve(dnorm(x, mean = 10, sd = 1), add = TRUE, col = "darkred") # Density of normal with different scale curve(dnorm(x, mean = 0, sd = 0.5), xlim = c(-10, 10), ylab = "density") curve(dnorm(x, mean = 0, sd = 1), add = TRUE, col = "darkgreen") curve(dnorm(x, mean = 0, sd = 2), add = TRUE, col = "gold") curve(dnorm(x, mean = 0, sd = 5), add = TRUE, col = "darkred") # Density of normal with different location and scale curve(dnorm(x, mean = 0, sd = 1), xlim = c(-10, 10), ylab = "density") curve(dnorm(x, mean = 0, sd = 2), add = TRUE, col = "darkgreen") curve(dnorm(x, mean = 5, sd = 1), add = TRUE, col = "gold") curve(dnorm(x, mean = 5, sd = 2), add = TRUE, col = "darkred") # CDF curve(pnorm(x, mean = 0, sd = 1), xlim = c(-10, 10), ylab = "CDF") curve(pnorm(x, mean = 0, sd = 2), add = TRUE, col = "darkgreen") curve(pnorm(x, mean = 5, sd = 1), add = TRUE, col = "gold") curve(pnorm(x, mean = 5, sd = 2), add = TRUE, col = "darkred") # Density and value of the density in a point curve(dnorm(x, mean = 100, sd = 10), xlim = c(80,120), ylab = "density") dnorm(110, mean = 100, sd = 10) y <- dnorm(110, mean = 100, sd = 10) segments(x0 = 110, y0 = 0, x1 = 110, y1 = y, col = "red", lwd = 2) segments(x0 = 80, y0 = y, x1 = 110, y1 = y, col = "red", lwd = 2) points(110, y, col = "red", pch = 19) # CDF and value of the CDF in a point curve(pnorm(x, mean = 100, sd = 10), xlim = c(80,120), ylab = "CDF") pnorm(110, mean = 100, sd = 10) y <- pnorm(110, mean = 100, sd = 10) segments(x0 = 110, y0 = 0, x1 = 110, y1 = y, col = "red", lwd = 2) segments(x0 = 80, y0 = y, x1 = 110, y1 = y, col = "red", lwd = 2) points(110, y, col = "red", pch = 19) # Quantiles curve(pnorm(x, mean = 100, sd = 10), xlim = c(80,120), ylab = "CDF") qnorm(c(0.1,0.5,0.9), mean = 100, sd = 10) x <- qnorm(c(0.1,0.5,0.9), mean = 100, sd = 10) segments(x0 = c(0,0,0), y0 = c(0.1, 0.5, 0.9), x1 = x, y1 = c(0.1, 0.5, 0.9), col = "red", lwd = 2) segments(x0 = x, y0 = 0, x1 = x, y1 = c(0.1, 0.5, 0.9), col = "red", lwd = 2) # Generation of numbers from the normal distribution set.seed(14) sim <- rnorm(1000, mean = 100, sd = 10) hist(sim, breaks = 40, freq = FALSE) curve(dnorm(x, mean = 100, sd = 10), add = TRUE) # Exercise: To do togheter # 1)Let us to generate $B=1000$ samples of size $n=10$ # from a $N(\mu=5, \sigma^2 = 4)$ and fix $\alpha=0.05$ # Compute the CI for each replication # Count how many intervals includes the true parameter mu <- 5; sigma <- 2; B <- 1000; n <- 10; alpha <- 0.05 CI <- matrix(0, B, 2) l <- rep(0, B) set.seed(23) q0975 <- qt(1 - alpha/2, n - 1) for (i in 1 : B){ x <- rnorm(n, mu, sigma) CI[i, ] <- mean(x) + c(-q0975, q0975) * sd(x)/sqrt(n) l[i] <- (mu > CI[i,1] & mu < CI[i,2]) } #Empirical coverage probability mean(l) # Plot of the (first 100) CIs plot(1, xlim = c(0,10), ylim = c(0,11), type = "n", xlab = expression(mu), ylab = "", yaxt = "n", main = paste("100 IC for the mean (unknown variance)"), cex.main = 1.2) abline(v = mu) d <- 0 for (i in 1 : 100){ d <- d + 0.1 lines(seq(CI[i, 1], CI[i, 2], length = 100), rep(d, 100), col = (l[i] + 1)) } # number of intervals (out the 100) including the true parameter value sum(l[1 : 100]) # Example of Hypothesis test y <- c(497.0621, 497.7690, 497.2631, 498.6688, 494.6227, 496.2185, 498.2635, 502.7199, 497.4339, 494.2883) t_obs <- (mean(y) - 500)/(sd(y)/sqrt(length(y))) curve(dt(x, 9), xlim = c(-5, 5), ylim = c(0, 0.4), main = "Rejection region", col = "blue", lwd = 2, xlab = "", ylab = expression(t[9]), yaxs="i") cord.x0 <- c(-5, seq(-5, qt(0.025, 9), 0.01), qt(0.025, 9)) cord.y0 <- c(0, dt(seq(-5,qt(0.025, 9), 0.01), 9), 0) cord.x1 <- c(qt(0.975, 9),seq(qt(0.975, 9), 5, 0.01), 5) cord.y1 <- c(0, dt(seq(qt(0.975, 9), 5, 0.01), 9), 0) polygon(cord.x0, cord.y0, col = "darkred", border = NA ) polygon(cord.x1, cord.y1, col = "darkred", border = NA ) abline(v = t_obs, lty = 2, lwd = 2, col = "red") text(0, 0.2, paste("Accept", expression(H0))) text(c(-2.7,2.7), 0.08, paste("Reject", expression(H0)), col = "darkred") text(as.double(t_obs) - 0.15, 0.02, "t", col = "red", cex = 1.2)