[R] Arrays and functions

From: Louisa Hay <louisahay_at_msn.com>
Date: Sun, 13 Apr 2008 09:43:33 +0100

Hi, I' am doing a stats project using R to work out the size of a t-test and wilcoxon test depending on the distribution and sample size. I just can't get it to work - I want to put my results from the function size() into an array.At the moment I keep getting the error message:Error in res[distribution, test, samplesize] <- results : subscript out of boundsCan anyone tell me where I'm going wrong, please?!size.power.test<-function(){k<-1000distributions<-c("Normal","Uniform")tests<-c("t","Wilcoxon")samplesizes<-c(10,30,40)res<-array(0,c(length(distributions),length(tests),length(samplesizes)))dimnames(res)<-list(distributions,tests,samplesizes)for(distribution in distributions){for(test in tests){for(samplesize in samplesizes){for(i in 1:k){results<-size()res[distribution,test,samplesize]<-results}output<-size.power.testreturn(output)}}}}size<-function(k=1000,H0=0,mu.true=0,sigma.true=1,alpha=0.05,x1=-sqrt(3),y1=sqrt(3)){distributions<-c("Normal","Uniform")tests<-c("t","Wil!  coxon")samplesizes<-c(10,30,40)reject1<-numeric(k)for(distribution in distributions){for(test in tests){for(samplesize in samplesizes){for(i in 1:k){if(distribution=="Normal"){#randomly generate data from normal distribution with true meandat<-rnorm(n=samplesize,mean=mu.true,sd=sigma.true)}else{#randomly generate data from a uniform distributiondat<-runif(samplesize,min=x1,max=y1)}if(test=="t"){#perform a t-test on sampleres<-t.test(dat,alternative="two.sided",mu=H0)#record if p-value is less than alphareject1[i]<-(res$p.value<=alpha)}else{#perform a wilcoxon-test on sampleres<-wilcox.test(dat,alternative="two.sided",mu=H0)#record if p-value is less than alphareject1[i]<-(res$p.value<=alpha)}}output<-list(sum(reject1)/k)return(output)}}}}

A prize an hour, 24 hours a day. Try Big Snap now!

[[elided Hotmail spam]]

        [[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. Received on Sun 13 Apr 2008 - 08:45:36 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 13 Apr 2008 - 10:30:27 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive