# Generate correlated continuous and categorical data in R [duplicate]

Task at hand: generate a simulated dataset containing both continuous and categorical variables, given a pre-defined correlation matrix.

What has been done: this post covers how to generate correlated continuous variables. This post elaborates the generation of correlated categorical variables. Both quite comprehensible.

The question: it is unclear how to generate both continuous and categorical variables at the same time, all correlated between each other. For example, let's say we need to have a dataset with five variables, three continuous variables, and two categorical variables. The first categorical could refer to three income categories (therefore being ordinal), and the second one could be gender (therefore being nominal). The three continuous variables could be, say, scores on three psychological tests. All of them should be correlated to a given degree.

It would be possible to create a dataset with five correlated continuous variables, and then bin two of those into categorical, hopefully retaining some degree of the original relationship. Yet I am not sure how to control the degree of correlation in this case. The data simulation would be in R. Any ideas?

EDIT 1: To be clear, when it concerns categorical data, by correlation I mean statistical association, however it is measured (I have no preference there). Not the Pearson correlation.

• If you want to be able to specify correlation between a continuous and an ordinal variable, can you define what you mean by correlation in that case? If one variable is ordinal, presumably you can't mean a Pearson correlation, because "Low/Middle/High" doesn't take numerical values -- and if you assign it some, it's no longer ordinal (you've made it interval by doing so). In that case you'd be answering the rather different question of generating correlated discrete and continuous interval variables. – Glen_b -Reinstate Monica Dec 21 '14 at 20:04
• I hoped this would be clear from the example I have given, namely income categories and gender. Obviously, it would not be Pearson correlation that includes the categorical variables. By correlation I mean an association, whereas the exact measure of association would be dependent on the types of variables involved, e.g. point-biserial for the dichotomous and continuous, rank-biserial for the ordinal and dichotomous. Other measures of association might be used, I have no preference. I hope the issue is clear now? – Maxim.K Dec 21 '14 at 20:38
• Ah. No, it was not obvious that you meant those measures of association (a least not to me, perhaps it was obvious to someone else); they're hardly the only possible choices. You might clarify your question by being explicit about those. – Glen_b -Reinstate Monica Dec 21 '14 at 20:41
• If you're going to generate a categorical variable, I think you'll also need to specify the (population?) distribution of the categorical variable. – Jeremy Miles Dec 22 '14 at 17:43
• @JeremyMiles I imagine so. This is described in the procedure regarding the generation of categorical data I've linked to. – Maxim.K Dec 22 '14 at 18:04