Fabio Sanchez
2008-May-27 08:52 UTC
[R] Write function multiple tests and write summary table
Dear R community,
I am not experienced in writing functions and need your help to understand
the strategy to face the following problem:
I have a group of independent numerical variables, let's say
a<-c(1,3,2,6,9,2,2,3,4,1)
b<-c(2,3,3,4,5,6,2,1,1,6)
c<-c(0,2,4,4,7,6,7,1,2,2)
d<-c(0,0,0,1,0,2,1,3,1,2)
e<-c(9,2,3,1,1,1,0,2,5,6)
and a grouping variable,
group<-c("case","case","case",","case",","case","control","control","control","control","control")
I want to apply a test for every variable independently and store the
summary result in a table
for example
mod1<-aov(a~group)
mod2<-aov(b~group)
.
.
.
generate summary table
Any suggestions?
Thank you
Fabio
--
Fabio Sánchez, MD, MSc, PhD
Unit of Dermatology and Venereology
Department of Medicine
Karolinska Institute
SE-17176 Karolinska University Hospital
Phone: +46 8 51772158
Fax: +46 8 51773620
[[alternative HTML version deleted]]
Petr PIKAL
2008-May-27 09:49 UTC
[R] Odp: Write function multiple tests and write summary table
Petr Pikal petr.pikal at precheza.cz 724008364, 581252140, 581252257 r-help-bounces at r-project.org napsal dne 27.05.2008 10:52:18:> Dear R community, > > I am not experienced in writing functions and need your help tounderstand> the strategy to face the following problem: > > I have a group of independent numerical variables, let's say > > a<-c(1,3,2,6,9,2,2,3,4,1) > b<-c(2,3,3,4,5,6,2,1,1,6) > c<-c(0,2,4,4,7,6,7,1,2,2) > d<-c(0,0,0,1,0,2,1,3,1,2) > e<-c(9,2,3,1,1,1,0,2,5,6) > > and a grouping variable, > > group<-c >("case","case","case",","case",","case","control","control","control","control","control")> > I want to apply a test for every variable independently and store the > summary result in a table > > for example > > mod1<-aov(a~group) > mod2<-aov(b~group) > . > . > . > generate summary table > > > Any suggestions?Use lists. lll<-list(a,b,c,d,e) group<-factor(group) lapply(lll, function(x) summary(aov(x~group))) [[1]] Df Sum Sq Mean Sq F value Pr(>F) group 1 8.1 8.1 1.35 0.2788 Residuals 8 48.0 6.0 [[2]] Df Sum Sq Mean Sq F value Pr(>F) group 1 0.1 0.1 0.025 0.8783 Residuals 8 32.0 4.0 [[3]] Df Sum Sq Mean Sq F value Pr(>F) group 1 0.10 0.10 0.0142 0.9081 Residuals 8 56.40 7.05 [[4]] Df Sum Sq Mean Sq F value Pr(>F) group 1 6.40 6.40 14.222 0.005456 ** Residuals 8 3.60 0.45 --- Signif. codes: 0 ?***? 0.001 ?**? 0.01 ?*? 0.05 ?.? 0.1 ? ? 1 Further improvement will be necessary because it is not clear what the output shall look like. Regards. Petr> Thank you > > Fabio > > > > -- > Fabio S?nchez, MD, MSc, PhD > Unit of Dermatology and Venereology > Department of Medicine > Karolinska Institute > SE-17176 Karolinska University Hospital > Phone: +46 8 51772158 > Fax: +46 8 51773620 > > [[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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
ctu at bigred.unl.edu
2008-May-27 10:39 UTC
[R] Write function multiple tests and write summary table
Hi Fabio,
Before using R, there is one thing you need to know "Don't use letter
c to assign a name. I use f instead of your letter c for third dataset.
This is my code (For better result, I use lm to replace aov)
DS<-matrix(data=c(a<-c(1,3,2,6,9,2,2,3,4,1),b<-c(2,3,3,4,5,6,2,1,1,6),f<-c(0,2,4,4,7,6,7,1,2,2),d<-c(0,0,0,1,0,2,1,3,1,2),e<-c(9,2,3,1,1,1,0,2,5,6)),
nrow=10,ncol=5)
group<-c("case", "case", "case",
"case", "case", "control", "control",
"control", "control", "control")
group<-factor(group)
est<-matrix(data=NA,nrow=2,ncol=5,
dimnames=list(estimate=c("intercept","factor"),dataset=c("a","b","f","d","e")))
SE<-matrix(data=NA,nrow=2,ncol=5,dimnames=list(STD=c("intercept","factor"),dataset=c("a","b","f","d","e")))
t.stat<-matrix(data=NA,nrow=2,ncol=5,dimnames=list(T.stat=c("intercept","factor"),dataset=c("a","b","f","d","e")))
p.value<-matrix(data=NA,nrow=2,ncol=5,dimnames=list(p.vale=c("intercept","factor"),dataset=c("a","b","f","d","e")))
for(i in 1:5)
{
results<-lm(DS[,i]~group)
est[,i]<-summary(results)$coefficient[,1]
SE[,i]<-summary(results)$coefficient[,2]
t.stat[,i]<-summary(results)$coefficient[,3]
p.value[,i]<-summary(results)$coefficient[,4]
}
Data.a<-matrix(data=c(est[,1],SE[,1],t.stat[,1],p.value[,1]),nrow=2,ncol=4)
Data.b<-matrix(data=c(est[,2],SE[,2],t.stat[,2],p.value[,2]),nrow=2,ncol=4)
Data.f<-matrix(data=c(est[,3],SE[,3],t.stat[,3],p.value[,3]),nrow=2,ncol=4)
Data.d<-matrix(data=c(est[,4],SE[,4],t.stat[,4],p.value[,4]),nrow=2,ncol=4)
Data.e<-matrix(data=c(est[,5],SE[,5],t.stat[,5],p.value[,5]),nrow=2,ncol=4)
summary.table<-rbind(Data.a,Data.b,Data.f,Data.d,Data.e)
Summary.Table<-matrix(data=summary.table,nrow=10,ncol=4,dimnames=list(DATA=c("intercept.a","factor.a","intercept.b","factor.b",
"intercept.f","factor.f","intercept.d","factor.d",
"intercept.e","factor.e"),TEST=c("Estimate","S.E","t.stat","p.value")))
Summary.Table
This is what you need~
Chunhao Tu
Quoting Fabio Sanchez <Fabio.Sanchez at ki.se>:
> Dear R community,
>
> I am not experienced in writing functions and need your help to understand
> the strategy to face the following problem:
>
> I have a group of independent numerical variables, let's say
>
> a<-c(1,3,2,6,9,2,2,3,4,1)
> b<-c(2,3,3,4,5,6,2,1,1,6)
> c<-c(0,2,4,4,7,6,7,1,2,2)
> d<-c(0,0,0,1,0,2,1,3,1,2)
> e<-c(9,2,3,1,1,1,0,2,5,6)
>
> and a grouping variable,
>
>
group<-c("case","case","case",","case",","case","control","control","control","control","control")
>
> I want to apply a test for every variable independently and store the
> summary result in a table
>
> for example
>
> mod1<-aov(a~group)
> mod2<-aov(b~group)
> .
> .
> .
> generate summary table
>
>
> Any suggestions?
>
> Thank you
>
> Fabio
>
>
>
> --
> Fabio S?nchez, MD, MSc, PhD
> Unit of Dermatology and Venereology
> Department of Medicine
> Karolinska Institute
> SE-17176 Karolinska University Hospital
> Phone: +46 8 51772158
> Fax: +46 8 51773620
>
> [[alternative HTML version deleted]]
>
>