diff --git a/scripts/02_R_distr.R b/scripts/02_R_distr.R new file mode 100644 index 0000000000000000000000000000000000000000..6d619a64ffd06b532f04571c144ac513c7ba131e --- /dev/null +++ b/scripts/02_R_distr.R @@ -0,0 +1,70 @@ +#======================================================================= +# Distribuição amostral da amplitude. + +n <- 5 # Tamanho da amostra. +sigma <- 1 # Desvio-padrão populacional + +w <- replicate(1000, { + x <- rnorm(n = n, m = 0, sd = sigma) + r <- diff(range(x))/sigma + r/sigma +} + ) + +plot(density(w)) +rug(w) + +plot(ecdf(w)) +rug(w) + +mean(w) +sd(w) + +curve(pnorm(x, mean = mean(w), sd = sd(w)), add = TRUE, col = 2) + +#----------------------------------------------------------------------- +# Fazer para diferentes valores de n. + +nvals <- c(2, 3, 4, 5, 7, 10, 15) + +ww <- lapply(nvals, + function(n) { + replicate(1000, { + x <- rnorm(n = n, m = 0, sd = sigma) + r <- diff(range(x))/sigma + r/sigma + }) + }) +str(ww) + +t(sapply(ww, length)) + +da <- data.frame(n = ordered(rep(nvals, sapply(ww, length))), + w = do.call(c, ww)) + +aggregate(w ~ n, data = da, FUN = function(x) c(mean(x), sd(x))) + +library(latticeExtra) + +densityplot(~w | n, data = da, n = 202, as.table = TRUE, + auto.key = list(corner = c(0.9, 0.1)), + panel = function(x, ...) { + panel.densityplot(x = x, ...) + panel.mathdensity(dmath = dnorm, col = "black", + args = list(mean = mean(x), + sd = sd(x))) + }) + +ecdfplot(~w | n, data = da, as.table = TRUE, + auto.key = list(corner = c(0.9, 0.1)), + panel = function(x, subscripts, ...) { + panel.ecdfplot(x, subscripts = subscripts, ...) + m <- mean(x) + s <- sd(x) + col <- "gray50" + panel.curve(pnorm(x, mean = m, sd = s), col = col) + panel.abline(v = m, lty = 2, col = col) + }) + +ecdfplot(~w, groups = n, data = da, + auto.key = list(corner = c(0.9, 0.1)))