Phi coefficient matrix (package psych)

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Phi coefficient matrix (package psych)

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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Phi coefficient matrix (package psych)

Jim Lemon-4
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 <[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Phi coefficient matrix (package psych)

Kumar Mainali
Hi Jim,

That solves my problem. Than you.

-- Kumar


Postdoctoral Associate
Fagan Lab, Department of Biology
University of Maryland

On Tue, Apr 21, 2015 at 5:31 PM, Jim Lemon <[hidden email]> wrote:

> 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 <[hidden email]>
> 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]]
> >
> > ______________________________________________
> > [hidden email] 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Phi coefficient matrix (package psych)

William Revelle
In reply to this post by Kumar Mainali

>
> Kumar and Jim,
>  The phi coefficient is identical to the Pearson coefficient in the case of a 2 x 2 data set.
>
> As it says in the help file for phi:
>
> "Since the phi coefficient is just a Pearson correlation applied to dichotomous data, to find a matrix of phis from a data set involves just finding the correlations using cor or lowerCor or corr.test.”
>
> So,  you can do a cor(df)  and you do not need the phi(df)
>
> If you want the tetrachoric (which estimates what a Pearson would be if you had not artificially dichotomized the data) try tetrachoric(df)
>
> Bill
>


>
>
>
>> On Apr 21, 2015, at 3:34 PM, Kumar Mainali <[hidden email]> 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]]
>>
>> ______________________________________________
>> [hidden email] 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.
>
> William Revelle           http://personality-project.org/revelle.html
> Professor           http://personality-project.org
> Department of Psychology   http://www.wcas.northwestern.edu/psych/
> Northwestern University   http://www.northwestern.edu/
> Use R for psychology             http://personality-project.org/r
> It is 3 minutes to midnight   http://www.thebulletin.org
>
>
>
>
>
>

William Revelle           http://personality-project.org/revelle.html
Professor           http://personality-project.org
Department of Psychology   http://www.wcas.northwestern.edu/psych/
Northwestern University   http://www.northwestern.edu/
Use R for psychology             http://personality-project.org/r
It is 3 minutes to midnight   http://www.thebulletin.org

______________________________________________
[hidden email] 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.