# Generate viable sampling distributions of discrete data in R

I'm trying to simulate 2 X 2 data that would yield a relatively strong negative correlation.

I'm using the R library GenOrd and the ordsample() function as follows below. The ordsample() function requires that you specify (a) the sample size you'd like to simulate, (b) the marginal probabilities as a list that is the length of the number of variables being simulated (in this case 2), and (c) the target correlation matrix (i.e., called sigma in the GenOrd package).

library(GenOrd)

# Specify sample size N
N <- 40

# Marginal distribution for two variables
marginal <- list(c(.5), c(.5))

# Correlation (Pearson) matrix as target for simulated relationship between variables
Sigma <- matrix(c(1.0, -.71, -.71, 1.0), 2, 2, byrow=TRUE)

# Generate a sample of the categorical variables with specified parameters
m <- ordsample(N, marginal, Sigma)


However, I'm getting the following error whenever I input a correlation larger than -.70.

Error in contord(list(marginal[[q]], marginal[[r]]), matrix(c(1,
Sigma[q,  :
Correlation matrix not valid!


I'm clearly specifying something untenable somewhere - but I don't know what it is. Specifying any value between -.70 and +1.00 works fine such that it generates two variables with the correlation requested, given sampling error.

It's just values below -.70 that crash the script.

I'm thinking I'm misunderstanding the specification of the marginal distribution, but am confused because it works for values that are not less than -.70.

Here is the help info for the marginal argument in the ordsample() function (R documentation):

a list of k elements, where k is the number of variables. The i-th element of marginal is the vector of the cumulative probabilities defining the marginal distribution of the i-th component of the multivariate variable. If the i-th component can take k_i values, the i-th element of marginal will contain k_i-1 probabilities (the k_i-th is obviously 1 and shall not be included).

Help appreciated.

• Could you tell us more specifically what ordSample is trying to do? What do your "specified parameters" mean? – whuber Apr 16 '18 at 19:56
• Thanks for your attention, I've added more to the original question. – Peter Miksza Apr 16 '18 at 20:09
• Thank you, but then what does Sigma represent? Evidently there is a mathematical constraint relating the marginal probabilities to whatever the numbers in Sigma mean. (Specifically, out of the many statistics that can be derived from a $2\times 2$ table that have been called "correlation," which one is it?) Note that ordsample is not a part of base R--it's in somebody's package--so explaining what it's doing will make your question accessible to a much larger number of qualified readers. – whuber Apr 16 '18 at 20:12
• It's just the label for the argument requesting a correlation matrix, I've added that to the question. The default is Pearson's correlation, which can be changed to Spearman. Neither options appears to work. – Peter Miksza Apr 16 '18 at 20:14
• I think I might be missing something basic here: what does a Pearson correlation mean for a $2\times 2$ table of "categorical variables"? You must be interpreting it in some special way, such as assigning numerical values to the rows and columns and interpreting the cell entries as counts of data. (Which numbers you use don't matter except insofar as they determine the sign of the correlation.) I'm having trouble with that interpretation--perhaps the same trouble you are--because for the marginal distributions you supply, a perfect negative correlation is feasible. – whuber Apr 16 '18 at 22:23

I'm not exactly sure "why", however, I found no problems simulating 2 X 2 data that would yield a relatively strong negative correlation using the generate.binary() function from the MultiOrd package.

For example, the following code will work for the complete range of correlation inputs. The documentation for the generate.binary() function indicates that the matrix specified is interpreted as a tetrachoric correlation matrix.

library(MultiOrd)

# Specify sample size N
N <- 40

# Marginal distribution for two variables as a vector for MultiOrd rather than a list
marginal <- c(.5, .5)

# Correlation (tetrachoric) matrix as target for simulated relationship between variables
Sigma <- matrix(c(1.0, -.71, -.71, 1.0), 2, 2, byrow=TRUE)

# Generate a sample of the categorical variables with specified parameters
m <- generate.binary(40, marginal, Sigma)