Raphael Saldanha | 20 Aug 04:22

Confidence Interval

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

(Continue reading)

Prof Brian Ripley | 20 Aug 05:16
Favicon

Re: Confidence Interval

On Tue, 19 Aug 2008, Raphael Saldanha wrote:

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

You need to tell us what you are trying to do.  You seem to be computing a 
parametric bootstrap interval, but incorrectly (you need to reflect 
percentile intervals).  See Davison & Hinkley (1997) 'The Bootstrap and 
its Application' for more details.

In any case, your simulation is not repeatable (you set no seed), so we 
don't know what you saw and what 'differences' disturbed you.

Your calculation of quantiles is not quite correct: use quantile().
(The indices go from 1 to 1000, not 0 to 1000.)  E.g.

> quantile(1:1000, c(0.025, 0.975))
    2.5%   97.5%
  25.975 975.025

and not 25, 975.

As your results show, a histogram is not a good summary of the shape of a 
distribution on 1000 points.  We can do much better with an ecdf or a 
density estimate.

>
> # Teste de m?dia entre uma amostra e uma popula??o normal
(Continue reading)

Raphael Saldanha | 20 Aug 17:07

Re: Confidence Interval

Thanks Professor and Bernardo,

What I'm trying to do is this: I have a macro for Minitab. His author says
it's a "Monte Carlo simulation to estimate a confidence interval". But and
don't have Minitab, don't like to work with illegal licenses, and LOVE R.
So, I re-write the macro in a R script.

Bellow, is the original macro, for Minitab:

GMACRO

TesteMedia

do K1=1:1000
 random 200 C1;
 normal 100 2.
 mean C1 K2
 let C2(K1)=K2
enddo

sort C2 C3

let K3=C3(25)
let K4=C3(975)
print K3 K4

ENDMACRO

The macro's author think on this way: I have a y vector of sorted means with
1000 registers. So, my CI is between the 25º and 975º element.
(Continue reading)

Raphael Saldanha | 20 Aug 22:05

Re: Confidence Interval

Hi!

Here is a better code, using the percentil value instead the position, and
some corrections on the graph code.

The reason in the difference on the tails is elementar, but I don't had see
it before. The right and left limits are calculated by simulation, and his
differences from the mean are not exactly equal, so the areas under the
normal curve! I'm feeling like dummie now...

Thanks for the help!

Raphael Saldanha
BRAZIL

On Wed, Aug 20, 2008 at 12:07 PM, Raphael Saldanha <
saldanha.plangeo <at> gmail.com> wrote:

> Thanks Professor and Bernardo,
>
> What I'm trying to do is this: I have a macro for Minitab. His author says
> it's a "Monte Carlo simulation to estimate a confidence interval". But and
> don't have Minitab, don't like to work with illegal licenses, and LOVE R.
> So, I re-write the macro in a R script.
>
> Bellow, is the original macro, for Minitab:
>
> GMACRO
>
> TesteMedia
(Continue reading)

Bernardo Rangel Tura | 20 Aug 11:10
Favicon

Re: Confidence Interval

Em Ter, 2008-08-19 às 23:25 -0300, Raphael Saldanha escreveu:
> Hi!
> 
> With the following script, I'm trying to make a demonstration of a
> Confidence Interval, but I'm observing some differences on tails.

Raphael,

If you make demonstration of Confidence Interval why you don't use
"ci.examp" of "TeachingDemos" package?

It's a very good example

--

-- 
Bernardo Rangel Tura, M.D,MPH,Ph.D
National Institute of Cardiology
Brazil

______________________________________________
R-help <at> r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Gmane