0
$\begingroup$

I have a data set with several dichotomous, continuous, and ordinal variables. As part of exploratory data analysis I find the correlation matrix a very useful tool to see what might have a relationship with what else. I can do that for continuous and ordinal variables in one go with Spearman correlation. As the dichotomous variables code "yes/no" cases, I can create an association matrix with Cramer's V or corrected Cramer's V values but how could I quantitatively see the strength of relationships between dichotomous-continuous variables or between nominal non-dichotomous and continuous variables or between ordinal and nominal ?

I know I could dived the data set by dichotomous or nominal variables and do an ANOVA or Kruskal-Wallis test for the continuous variables and show the results on boxplots. However, I do not know how I could see the relationships of all variables with each other.

Let me show with an example what I mean. Let's have a table with the columns: gender (0/1), weight (continuous), age (continuous), smoker (0/1), health_issues (ordinal from 0 to 5).

|gender|weight|age|smoker|health_issues|
|0     |57    |46 |1     |2            |
|1     |46    |23 |0     |0            |
|0     |84    |40 |0     |2            |

How can we say how strong relationship smoking, weight, age and gender have with the ordinal variable health issues? How can we see what relationships the other variables have with each other Let's assume the continuous variables are not normally distributed.

$\endgroup$
2
  • $\begingroup$ Why don't you post what you found as another answer to this question? It's OK to answer your own question, and posting what you found as a formal answer will make it easier for others to benefit from your work on this. $\endgroup$
    – EdM
    Commented Nov 5 at 14:48
  • $\begingroup$ Thanks, I did not know that. I will post it as an answer. $\endgroup$
    – Tamas
    Commented Nov 5 at 19:22

2 Answers 2

1
$\begingroup$

This answer to "Correlation coefficient between a (non-dichotomous) nominal variable and a numeric (interval) or an ordinal variable" discusses a few choices that you can make for quantifying such associations.

Visualization for multiple variables of different types is nicely handled by what's called a "pairs plot." This is a matrix of plots, with each plot a visual display of the association between a pairwise combination of variables. The visual displays are important, as they can illustrate data patterns beyond the linear associations of correlation measures.

What's often done is to have, say, plots in a lower diagonal of the plot matrix with correlations (or other numerical association measure) in the corresponding upper diagonal. Software can allow you to define the particular numerical association measure that you want.

The standard pairs() function in base R can produce such plots. The ggpairs() function of the GGally package provides a lot of flexibility, with many examples on this web page

$\endgroup$
0
$\begingroup$

Answer: The post from EdM helped but the real tangible answer I found here: https://gtk.uni-miskolc.hu/files/10560/vegyes+kapcsolat.pdf It is a slide set in statistics exactly about this question but in Hungarian.

The point is that we can compute the standard deviation for the whole continuous column (e.g. weight in the example), let's call this total std, and we can group the elements of this column by the nominal variable e.g. gender. Then we can compute the standard deviation within these groups, let's call this within group std. Then we can compute the standard deviation between the group means, which is called between group standard deviation. The solution come from the fact that squared total std = squared within groups std + squared between groups std.

The strength of the relationship can be expressed by between group std/total std. It is designated by H. The closer it is to 1 the stronger it is.

For how much percent the nominal variable is accountable of the total variance, i.e. e.g. how much percent the gender explain in the variance of the variable weight, it is H square.

I have written it in python and I thought I would share it as this question comes from time to time and the answers were not really tangible.

2 remarks to the code snippet: (1) I have written it in a loop, so you can add the nominal variables to the cols list and it will iterate over these; the continuous variable name you can assign to the col variable; (2) if you compute standard deviation with the python function, you need to use here ddof=0 otherwise not the population standard deviation will be computed but the sample which differs in the denominator (n-1).

import pandas as pd
import numpy as np


col = 'weight'
cols = ['gender']
for i in cols:
  # Grouping by question
  grouped_data = df_weigth_non_na.groupby(i)[col]

  # Withing Group Means
  group_means = grouped_data.mean()

  # Overall mean
  overall_mean = df_weigth_non_na[col].mean()

  # SST (Total Sum of Squares)
  sst = np.sum((df_weigth_non_na[col] - overall_mean) ** 2)
  stddev_total = np.sqrt(sst/ len(df_weigth_non_na[col]))
  print(f'stddev_total {stddev_total}')

  # SSB (Between-group Sum of Squares) weighted be group size
  ssb = np.sum(grouped_data.size() * (group_means - overall_mean) ** 2)
  stddev_between = np.sqrt(ssb / grouped_data.size().sum())
  print(f'stddev_between {stddev_between}\n')

  
  group_stds = grouped_data.std(ddof=0)

  # Print the standard deviations
  print("Standard Deviations for Each Group:")
  print(group_stds)
  print("Standard Deviations within Groups:")
  stddev_within2 = 0
  for group, stds in group_stds.items():
    stddev_within2 += (stds**2) * grouped_data.size()[group]

  stddev_within = np.sqrt(stddev_within2 / grouped_data.size().sum())
  print(f'Std within groups: {stddev_within} \n')

  # variance ratio
  h_squared = ssb / sst
  h = stddev_between / stddev_total
  print(f'h_squared {h_squared}')
  print(f'h {h}')

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.