# Re: [R] Write function multiple tests and write summary table

From: <ctu_at_bigred.unl.edu>
Date: Tue, 27 May 2008 05:39:56 -0500

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]]
>
>

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 Tue 27 May 2008 - 11:05:26 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 Tue 27 May 2008 - 11:30:48 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.