From f080968222e5d35959800f9390f4e0e848fb34e5 Mon Sep 17 00:00:00 2001 From: Walmes Zeviani <walmes@ufpr.br> Date: Mon, 4 Apr 2016 20:37:41 -0300 Subject: [PATCH] =?UTF-8?q?Adiciona=20script=20de=20distribui=C3=A7=C3=A3o?= =?UTF-8?q?=20amostral.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/02_R_distr.R | 70 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 scripts/02_R_distr.R diff --git a/scripts/02_R_distr.R b/scripts/02_R_distr.R new file mode 100644 index 0000000..6d619a6 --- /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))) -- GitLab