[R] T-test & for loop

David Winsemius dwinsemius at comcast.net
Thu May 6 22:59:45 CEST 2010


On May 6, 2010, at 4:56 PM, Joris Meys wrote:

> You should at least cheat right:
> mean( replicate( 10^5, t.test(rnorm(10, .1), a='g')$p. < .05))
>
> ;-)

Yes indeed, and even "better" :

mean( replicate(1e4, t.test(rnorm(10, .1), a='g')$p. < .05))

>
> @Greg : neat!
>
>
> On Thu, May 6, 2010 at 10:54 PM, David Winsemius <dwinsemius at comcast.net 
> > wrote:
>
> On May 6, 2010, at 3:51 PM, Greg Snow wrote:
>
> Golf entry:
>
> mean( replicate( 10000, t.test(rnorm(10, 0.1, 1),  
> alternative='greater', mu=0, conf.level=0.95)$p.value < 0.05))
>
> Or
>
> mean( replicate( 10000, t.test(rnorm(10, .1), a='g')$p.value < .05))
>
> or even
>
> mean( replicate( 10000, t.test(rnorm(10, .1), a='g')$p. < .05))
>
> Having failed with my efforts with:
>  summary(lm(rnorm(10,.1)~1))[[4]][4] < .05  ....
>
> I'm going to cheat:
> mean( replicate( 10^3, t.test(rnorm(10, .1), a='g')$p. < .05))
>
> -- 
> David
> -- 
> Gregory (Greg) L. Snow Ph.D.
> Statistical Data Center
> Intermountain Healthcare
> greg.snow at imail.org
> 801.408.8111
>
>
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Joris Meys
> Sent: Thursday, May 06, 2010 9:57 AM
> To: Dimitri Liakhovitski
> Cc: r-help at r-project.org; level
> Subject: Re: [R] T-test & for loop
>
> Hehe,
>
> those homeworks. If you can explain this code to the professor, you
> surely
> passed.
>
> n <-10000
> obs.rej.rate <- sum(sapply(1:n,function(x){t.test(rnorm(10, 0.1,
> 1),alternative="greater",mu=0,conf.level=0.95)$p.value <0.05}))/n
> obs.rej.rate
>
> This could actually be another round of R-golf. Anybody up to it?
>
>
>
> On Thu, May 6, 2010 at 3:03 AM, Dimitri Liakhovitski
> <ld7631 at gmail.com>wrote:
>
> Sounds like homework (you are not supposed to post homework-related
> questions here - read the guidelines). But anyway:
>
> nr.of.rejections=0
> for(i in 1:10000){
>   x=rnorm(10, 0.1, 1)
>   result<-
> t.test(x,alternative="greater",mu=0,conf.level=0.95)$p.value
>  if(result<0.05) nr.of.rejections = nr.of.rejections+1
> }
> print(paste("Observed number of rejections of Ho:", nr.of.rejections,
> sep="
> "))
> observed.rejection.rate = nr.of.rejections/10000
> print(paste("Actual rejection rate of Ho:", observed.rejection.rate,
> sep="
> "))
>
> However, it's not Type 1 error rate. It's the observed rejection
> rate. Type
> 1 error is probability of rejecting Ho when it is actually TRUE. And
> in
> your
> case (x = rnorm(10,0.1,1) your null hypothesis (that mean = 0) is NOT
> true
> because,as you know, in the population the mean is 0.1.
>
> Dimitri
>
>
> On Wed, May 5, 2010 at 2:31 PM, level <lev_lambert at hotmail.co.uk>
> wrote:
>
>
> I have been set a question which i understand statistically but my
> inability
> with R is preventing me from finishing it..
>
> My question is that we to calculate the frequency of Type 1 errors
>
> starting with  x = rnorm(10, 0.1, 1)
>
> then doing a t-test seeing whether you reject the null hypothesis
> (Ho
> mu
> =
> 0) alternative is mu > 0
>
> Then i am supposed to use a for loop to do this procedure 10 000
> times
> keeping track whether the null has been rejected or not.  If anyone
> can
> help
> with me it would be great help
>
> cheers
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/T-test-for-loop-
> tp2131458p2131458.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>
>
>
>
> --
> Dimitri Liakhovitski
> Ninah.com
> Dimitri.Liakhovitski at ninah.com
>
>      [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>
>
>
>
> --
> Joris Meys
> Statistical Consultant
>
> Ghent University
> Faculty of Bioscience Engineering
> Department of Applied mathematics, biometrics and process control
>
> Coupure Links 653
> B-9000 Gent
>
> tel : +32 9 264 59 87
> Joris.Meys at Ugent.be
> -------------------------------
> Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>
> ______________________________________________
> 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.
>
> David Winsemius, MD
> West Hartford, CT
>
>
>
>
> -- 
> Joris Meys
> Statistical Consultant
>
> Ghent University
> Faculty of Bioscience Engineering
> Department of Applied mathematics, biometrics and process control
>
> Coupure Links 653
> B-9000 Gent
>
> tel : +32 9 264 59 87
> Joris.Meys at Ugent.be
> -------------------------------
> Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list