Hi!

With the following script, I'm trying to make a demonstration of a
Confidence Interval, but I'm observing some differences on tails.

# Teste de média entre uma amostra e uma população normal
# Autor: Raphael de Freitas Saldanha
# Agosto de 2008

n    <- 200    # Sample size
xbar <- 100    # Sample mean
s    <- 2      # Sample SD
nc   <- 0.95   # Confidence level (95% -> 0.95)
rep  <- 1000   # Loops

#######################################################################

y <- NULL                # Vetor com as médias da amostra
for (i in 1:rep){        # Loop
x <- rnorm(n,xbar,s)     # Gere uma amostra normal n elementos, xbar média e
s desvio-padrão
x <- mean(x)             # Calcule a média (exata) dessa amostra
y <- c(y,x)              # Coloque essa média em um registro em y
}

y <- sort(y)             # Ordene todas as médias geradas

LI <- y[((1-nc)/2)*rep]         # Limite inferior,
LS <- y[rep-(((1-nc)/2)*rep)]   # Limite superior

### PARTE GRÁFICA ###

x <- mean(y)

xvals <- seq(-LI, LI, length.out=5000)
dvals <- dnorm(xvals,mean(y), sd(y))[1:5000]

xbvals <- seq(LS, LS*2, length.out=5000)
dbvals <- dnorm(xbvals,mean(y), sd(y))[1:5000]

ahist <- hist(y, freq=FALSE, col="lightblue", main="Intervalo de confiança")

polygon(c(xvals,LI,LI), c(dvals,dnorm(LI,mean(y), sd(y)),min(dvals)),
col="orange", lty=0)
polygon(c(LS,LS,xbvals), c(min(dbvals),dnorm(LS,mean(y), sd(y)),dbvals),
col="orange", lty=0)
curve(dnorm(x,mean(y), sd(y)),add=TRUE, lty=1, col="red",lwd=2)

### Intervalo de Confiança ###

LI # Limite inferior
LS # Limite superior

-- 
Atenciosamente,

Raphael Saldanha
saldanha.plangeo@gmail.com

	[[alternative HTML version deleted]]

