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]]
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 > # 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 at gmail.com > > [[alternative HTML version deleted]] > >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
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
I am a new user of R package and I needed a help regarding plotting confidence limits for FFT ( chisquare based) and spectrum using the monte carlo simulations of background red noise . How do I do it in R can any one help in this issue? sudheer -- ********************************************************************************** Dr. Sudheer Joseph Scientist Indian National Centre for Ocean Information Services (INCOIS) Ocean Valley, Post Box No# 21, IDA Jeedimetla P.O. Hyderabad, Ranga Reddy District - 500 055 Andhra Pradesh, India. TEl:+91-40-23044600(R),Tel:+91-9440832534(Mobile) Tel:+91-40-23886047(O),Fax:+91-40-23895011(O) E-mail:sjo@rediffmail.com <E-mail%3Asjo@rediffmail.com>; sudheer.joseph@yahoo.com; sjo@incois.gov.in. Web- http://oppamthadathil.tripod.com --------------* --------------- "The ultimate measure of a man is not where he stands in moments of comfort and convenience, but where he stands at times of challenge and controversy." Martin Luther King, Jr. [[alternative HTML version deleted]]