Up

Estimation of Polychoric/Tetrachoric Correlation Coefficients

Bayesian approach with a grid method

Yasuharu Okamoto, 2018

 

A correlation coefficient of latent variables for categorical variables is called a polychoric correlation coefficient, or in the case of binary variables tetrachoric correlation coefficient. Python scripts for estimation of these correlation coefficients are developed. Two sets of the scripts are prepared, one for two categorical variables and the other for more than two categorical variables.

 

Basic Model

It is assumed that values of two categorical variables are determined by those of latent variables with normal distributions. This type of a model is called Thurstonefs law of categorical judgment (Torgerson, 1958), Thurstonefs method of successive intervals (Guilford, 1954), or ordinal probit regression (Kruschke, 2011).

Consider two categorical variables  and , which have  and  number of categorical values, respectively. Denote the categorical values of  and  by integers from  to  and from 1 to , respectively.

We assume that latent variables  and  determine the categorical values as follows:

 if

 if

where s and s are category boundaries such that

and

Hence, we have

, 

 is the cumulative standard normal distribution function.

We assume that  has a two-variate normal distribution, that is given by this

where

,   

We have

where

Hece, we have

From this, we have

When we have  pairs  of  and , for which there are  pairs of , we have

Hence, we have a posterior distribution

where  is a prior distribution. As a prior distribution, we employ a uniform distribution over a finite region, which is large enough.

To estimate parameter values, we take the following two steps. The two step method is discussed by D. A. Alwin (gMargins of Errorh, 2007, p. 45).

Step 1

Estimate values of s and s by equation (1).

Step 2

              For the values estimated in Step 1, the posterior distribution of  is given by equation (2). The estimation is done by a grid method. That is,

on the interval , 1999 points are set, where successive points are separated by distance 0.001. This is coded by the following script

    grid_x = []

    for i in range(-999, 1000):

        grid_x.append(i / 1000)

These grid points are grouped into 4 sets, each of which is denoted by [p_start, p_end] as shown by this script

    grid_p = []

    for i in range(p_start, p_end + 1):

        if (i == p_start) or (i % 50 == 0):

            msg = '{0}/<{1} - {2}>'.format(i, p_start, p_end)

            print(msg)

            cnvs.create_rectangle(0, 0, 500, 150, outline = '#000055', fill = '#000055')

            cnvs.create_text(250, 75, text = msg, font = ('', 15), fill = '#FFFFFF')

            cnvs.update()

        rho = grid_x[i]

        v =  LogL(Nij, Kx, Ky, Cx, Cy, rho)

        grid_p.append(v)

For each group, calculation is done independently and simultaneously using multiprocessing. Each of multiprocessing presents a form, which shows the current state of the calculation by the function create_rectangle of the class Canvas.

Two sets of scripts are developed, one for two categorical variables and the other for more than two categorical variables.

 

Two categorical variables

The Python script files and a sample data file are archived in a zip file PolychoricGrid.zip , which can be downloaded by clicking on the file name PolychoricGrid.zip. The main script file is Main.py. Run Main.py, then the input data file name is asked and an output file name is required to be set (Figure 1).

Figure 1

 

The format of an input data file is as shown in Figure 2.

In the next line after the first line with a slash / at the head, the numbers of categories for the variables are written.

In the next line after the second line with a slash / at the head, names (labels) of the variables are written in the order where the name for the case ID variable is put first, and the names of categorical variables are written.

After the third line with a slash / at the head, categorical responses are written, one case data in one line, in the order where the case ID is written first, then the two categorical responses are put.

Values of case ID is strings, and categorical responses are denoted by integers.

Figure 2

 

After the last data, a line with a slash at the head is put (Figure 3).

Figure 3

 

Enter the name for the output file, then calculation starts (Figure 4).

Figure 4

 

During calculation by the grid method, forms of executions of multiprocessing are displayed showing the current states.

When the calculation ends, the posterior distribution of the correlation coefficient  is displayed with MAP estimate and 95% confidence interval (CI) (Figure 5).

Figure 5

 

Click the icon X at the upper right corner of the form, then the form is closed and the program ends.

After the program ends, open the output file. The content is as like in Figure 6.

Figure 6

 

Values of category boundaries are written as elements of lists Cx and Cy. Estimation of the posterior distribution of  is shown by the MAP (maximum a posteriori) estimate, the 95% CI (confidence interval), the mean, and the sd (standard deviation).

 

More than two categorical variables

In the case of M categorical variables, , c, , calculation of probabilities for M joint distribution of categorical variables needs too much time. Hence, estimation of polychoric (or tetrachoric) correlation coefficients is done for each pair of variables, separately. For each pair, the above method for two categorical variables is applied.

The script files and sample data files are archived in the zip file PolychoricMVGrid.zip , which can be downloaded by clicking the name PolychoricMVGrid.zip .

Run the main script file Main.py, then names for the input data file and the output file are asked. Name of the output file is any text file name.

Figure 7

 

The format of the input data file is as like in Figure 8.

Figure 8

 

In the next line after the first line with a slash / at the head, number of categorical variables is written. In Figure 8, 4 is written.

In the next line after the second line with a slash / at the head, numbers of categories of variables are written. In Figure 8, 3, 3, 5, and 5 are written.

In the next line after the third line with a slash / at the head, the name of case ID variable and the names of categorical variables are written.

After the fourth line with a slash / at the head, case ID string and categorical responses of each case are written. Data for each case are written in a line.

After the last data, a line with a slash / at the head is put to indicate the end of data (Figure 9).

Figure 9

 

After the name of the output file is entered, calculation starts. Current states of multiprocessing are displayed by forms presented (Figure 10).

Figure 10

 

Variables are denoted by sequential order from Var-0 to Var-(M-1). Calculation starts for Var-0 and Var-(M-1), and ends for Var-(M-2) and Var-(M-1) (Figure 11).

 ends, the program

Figure 11

 

After the program completes the calculation, open the output file, which is as like in Figure 12.

Figure 12

 

Estimates of the correlation coefficients are written in a matrix form as MAP estimates of the posterior distributions.

 

Up