[R] How to reconstitute a Product-Outcome table?
Gabor Grothendieck
ggrothendieck at gmail.com
Mon Nov 5 10:01:59 CET 2007
This does not answer your question but here is a shorter
version of your code:
xx <- xtabs(Count ~ Outcome + Product, x)
yy <- sum(xx) * xx / outer(rowSums(xx), colSums(xx))
subset(as.data.frame(yy), Freq != 0)
On Nov 5, 2007 3:18 AM, francogrex <francogrex at mail.com> wrote:
>
> Hi, my question is technical but maybe a little complicated to express it
> well. I have written this small code to calculate a relative risk (RR) of a
> Product-Outcome pairs that I am given:
> example dataset called test.txt in tab-delimited format:
> -------------------------------------------------
> Product Outcome Count
> P.A O.A 1
> P.A O.D 16
> P.B O.A 7
> P.B O.C 23
> P.C O.B 6
> P.C O.B 25
> P.D O.A 3
> P.D O.O 1
> P.E O.O 31
> P.F O.D 2
> P.G O.C 1
> P.G O.D 1
>
> ## The code:
> x=read.table(file="test.txt",header=TRUE)
> yy=reshape(x,direction="wide",idvar="Product",timevar="Outcome")
> dat <- sapply(yy, function(x) {x[is.na(x)] <- 0; x})
> dat=subset(dat,select=-Product)
> ni=apply(dat,1,sum)
> nj=as.vector(apply(dat,2,sum))
> nij=as.vector(x$Count)
> ntot=sum(nij)
> pi=ni/ntot
> pj=nj/ntot
> pij=nij/ntot
> mat=cbind(Product=as.vector(x$Product),Outcome=as.vector(x$Outcome))
> mat=as.data.frame(mat);mat$pij=pij
> zaz=reshape(mat,direction="wide",idvar="Outcome",timevar="Product")
> smat<- as.data.frame(sapply(zaz, function(x)
> {x[is.na(x)] <- 0; x}) );
> smat=subset(smat,select=-Outcome)
> smat$pj=pj; smat[length(nj)+1,1:length(ni)]=pi;
> result <- matrix(NA,nrow=length(nj),ncol=length(ni))
> for(i in 1:length(ni)){
> for(j in 1:length(nj)){
> result[j,i]=smat[j,i]/(smat[length(nj)+1,i]*smat[j,length(ni)+1])}
> }
> colnames(result)<-as.vector(unique(x$Product))
> rownames(result)<-as.vector(unique(x$Outcome))
> result1=ftable(result)
> result1=as.data.frame(result1);result1=subset(result1,Freq>0)
> names(result1)=c("Outcome","Product", "RR")
> result1
>
> ##OUTPUT:
> Outcome Product RR
> 1 O.A P.A 0.6256684
> 2 O.D P.A 5.7956656
> 6 O.A P.B 2.4818182
> 8 O.C P.B 3.7375000
> 14 O.B P.C 19.5000000
> 16 O.A P.D 7.9772727
> 20 O.O P.D 0.9140625
> 25 O.O P.E 3.6562500
> 27 O.D P.F 6.1578947
> 32 O.D P.G 3.0789474
> 33 O.C P.G 2.4375000
> -------------------------------------------
>
> My question now is: Suppose I have only some values of Count and RR:
> [example like 2 vectors generated randomly:Count=c(1, 16, 7, 23, 6, 25, 3,
> 1, 31, 2, 1, 1) and RR=c(0.625668449197861, 5.79566563467492,
> 2.48181818181818, 3.7375, 19.5, 7.97727272727273, 0.9140625, 3.65625,
> 6.1578947368421, 3.07894736842105, 2.4375)]
> Is there a way to reconstitute the count table as the one called test.txt in
> the beginning. I mean to reconstitute Product-Outcome-Count table from just
> the two vectors of Count and RR?
> Thanks.
> --
> View this message in context: http://www.nabble.com/How-to-reconstitute-a-Product-Outcome-table--tf4750151.html#a13582738
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>
More information about the R-help
mailing list