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 -- View this message in context: http://www.nabble.com/function-help-tp20035949p20035949.html Sent from the R help mailing list archive at Nabble.com.
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
Dear Alex, Try this: # Data name=read.table(textConnection(" X8 X9 X10 X102 X110 X177 X283 X284 X286 X292 X297 X306 X308 X314 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 1 0 0 0 0 1 0 0 0 0"),header=TRUE) closeAllConnections() rownames(name)=paste('s',1:4,sep="") # Sample # Function to estimate both the big mean and big standard deviation # name is the data set # n is the number of samples 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) apply(x,1,sd))) rownames(STDs)=rownames(Means) Gsd=sd(STDs) list(Gmean=Gmean,Gsd=Gsd) } # Power # mypars is a vector of length three which components # are n1, s1 and n1 # m2, s2 and n2 may change # alfa is "fixed" Power=function(mypars,m2,s2,n2,alfa=0.05){ m1=mypars[1] s1=mypars[2] n1=mypars[3] 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) } # powers # n is the number of samples # DATA is the ORIGINAL data set powers=function(n,m2,n2,s2,DATA){ outp=Sample(name,n) mymeans=outp$Gmean mysds=outp$Gsd res=t(rbind(mymeans,mysds,n=n)) apply(res,1,Power,m2=m2,n2=n2,s2=s2) } # ten samples and values for m2, n2 and s2 powers(n=10,m2=0.12,n2=10,s2=0.15) HTH, Jorge On Fri, Oct 17, 2008 at 11:50 AM, Alex99 <loyola9988@yahoo.com> 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 > > > -- > View this message in context: > http://www.nabble.com/function-help-tp20035949p20035949.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]