[R] function help

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Fri Oct 17 20:58:34 CEST 2008


take a look at ?mapply(); for instance, you can use something like this 
(untested):

dat <- Sample(...)
mapply(Power, dat$Gmean, dat$Gsd, MoreArgs = list(alfa = 0.05, m1 = 57, 
s1 = 33, n1 = 200, n2 = 100))


I hope it helps.

Best,
Dimitris

Alex99 wrote:
> Hi everyone,
> I have dataset which I make a sample of it couple of times and each time I
> get the mean and standard deviation of each row for each sample. I have a
> function for that, which takes the name of the file and number of times to
> sample and then returns the mean and standard deviation for each row in each
> sample.
> 
> Sample=function(name, n){
> res=replicate(n,name[,sample(colnames(name),n)],simplify=FALSE)
> 
> Means=do.call(rbind,lapply(res,function(x) rowMeans(x)))
> rownames(Means)=paste('sample',1:n,sep="")
> Gmean=colMeans(Means)
> 
> STDs=do.call(rbind,lapply(res,function(x) rowSds(x)))
> rownames(STDs)=paste('sample',1:5,sep="")
> Gsd=sd(STDs)
> 
> return(Gmean,Gsd)}
> 
> I then need to use the mean and standard deviation from each row to
> calculate the Power for 2 sample means. here is the function I use to do it:
> 
> Power=function(alfa,m1,m2,s1,s2,n1,n2){
>  up=abs(m1-m2)
>  down=sqrt(((s1^2)/(n1))+((s2^2)/(n2)))
>  z=(-1)*(qnorm(1-(alfa/2)))
>  fi=z+(up/down)
>  pow=pnorm(fi)
>  return(pow)}
> 
> then I need to call the Power function:
> 
> Power(.05,57,mi,33,si,200,n)
> 
> the alfa,m1,s1 and n1 values are constant but the m2,s2 and n2 values are
> changing.
> n2 is the n used in sample function(which I could input manually)
> m2 should be the grand mean for each row 
> s2 is the grand standard deviation for each row
> 
> example:this is a sample output for "sample" function:
> $Gmean
>   s1   s2   s3   s4 
> 0.08 0.20 0.12 0.20 
> 
> $Gsd
>        s1        s2        s3        s4 
> 0.2449490 0.2156741 0.2449490 0.2156741 
>  my problem is how can I put the corresponding means and standard devotions
> together  in the power function?I used a for loop, but the problem is
> getting the s1...s4 from Gmean and Gsd
> 
> Thanks for your help
> 
>  

-- 
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014



More information about the R-help mailing list