Message-ID: <CA+8X3fWWM0tbAVjm4MBd7KHcOg4LN_qzpJSyeHC7KbBFZRDEwg@mail.gmail.com>
Date: 2015-04-21T21:31:33Z
From: Jim Lemon
Subject: Phi coefficient matrix (package psych)
In-Reply-To: <CABK368i4w4fv1BQu=eetMN94OA-r=VK7a=UPQ1F7te6o1JBU3A@mail.gmail.com>
HI Kumar,
A simple way is:
phimat<-function(x) {
xcol<-dim(x)[2]
newx<-matrix(NA,nrow=xcol,ncol=xcol)
for(i in 1:xcol) {
for(j in 1:xcol) newx[i,j]<-phi(table(x[,i],x[,j]))
}
rownames(newx)<-colnames(newx)<-colnames(x)
return(newx)
}
phimat(df)
Jim
On Wed, Apr 22, 2015 at 6:34 AM, Kumar Mainali <kpmainali at gmail.com> wrote:
> I want to calculate phi coefficient for every pair of the columns. Is there
> a way to generate a matrix like a correlation matrix? I know cor function
> in the case below gives same answer as phi coefficient.
>
> x <- sample(c(0,1), 10, replace=TRUE)
> y <- sample(c(0,1), 10, replace=TRUE)
> z <- sample(c(0,1), 10, replace=TRUE)
> df <- data.frame(x,y,z)
> cor(df)
> library(psych)
> phi(df)
>
> Thank you,
> Kumar Mainali
> Postdoctoral Associate
> Department of Biology
> University of Maryland
> ?
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.