<commit_before>---
layout: post
title: Interactions between Categorical Variables in Mixed Graphical Models
category: r
---

{:center: style="text-align: center"}
In a [previous post](http://jmbh.github.io/Estimation-of-mixed-graphical-models/) we recovered the conditional independence structure in a dataset of *mixed variables* describing different aspects of the life of individuals diagnosed with Autism Spectrum Disorder, using the [mgm package](https://cran.r-project.org/web/packages/mgm/index.html). While depicting the independence structure in multivariate data set gives a first overview of the relations between variables, in most applications we interested in the exact parameter estimates. For instance, for interactions between continuous variables, we would like to know the sign and the size of parameters - i.e., if the nodes in the graph are positively or negatively related, and how strong these associations are. In the case of interactions between categorical variables, we are interested in the signs and sizes of the set of parameters that describes the exact non-linear relationship between variables.

In this post, we take the analysis a step further and show how to use the output of the [mgm package](https://cran.r-project.org/web/packages/mgm/index.html) to take a closer look at the recovered dependencies. Specifically, we will recover the sign and weight of interaction parameter between continuous variables and zoom into interactions between categorical and continuous variables and between two categorical variables. Both the dataset and the code are available on [Github](https://github.com/jmbh/AutismData).

We start out with the conditional dependence graph estimated in the previous post, however, now with *variables grouped by their type*:

![center](http://jmbh.github.io/figs/2017-11-30-Closer-Look/Autism_VarTypes.jpg) 

We obtained this graph by fitting a mixed graphical model using the mgmfit() function as in the [previous post](http://jmbh.github.io/Estimation-of-mixed-graphical-models/):

{% highlight r %}

# load data; available on Github
datalist <- readRDS('autism_datalist.RDS')
data <- datalist$data
type <- datalist$type
lev <- datalist$lev

library(devtools)
install_github('jmbh/mgm') # we need version 1.1-6
library(mgm)

fit <- mgmfit(data, type, lev, lamda.sel = "EBIC", d = 2)

{% endhighlight %}


Display Edge Weights and Signs
------

We now also display the weights of the dependencies. In addition, for interactions between continuous (Gaussian, Poisson) variables, we are able determine the sign of the dependency, as it only depends on one parameter. The signs are saved in `fit$signs`. To make plotting easier, there is also a matrix `fit$edgecolor`, which gives colors to positive (green), negative (red) and undefined (grey) edge signs. 

Now, to plot the weighted adjacency matrix with signs (where defined), we give `fit$edgecolor` as input to the argument edge.color in [qgraph](https://cran.r-project.org/web/packages/qgraph/index.html) and plot the weighted adjacency matrix `fit$wadj` instead of the unweighted adjacency matrix `fit$adj`:


{% highlight r %}

library(devtools)
install_github('SachaEpskamp/qgraph') # we need version 1.3.3
library(qgraph)

# define variable types
groups_typeV <- list("Gaussian"=which(datalist$type=='g'), 
                     "Poisson"=which(datalist$type=='p'),
                     "Categorical"=which(datalist$type=='c'))

# pick some nice colors
group_col <- c("#72CF53", "#53B0CF", "#ED3939")

jpeg("Autism_VarTypes.jpg", height=2*900, width=2*1300, unit='px')
qgraph(wgraph, 
       vsize=3.5, 
       esize=5, 
       layout=Q0$layout, # gives us the same layout as above
       edge.color = fit$edgecolor, 
       color=group_col,
       border.width=1.5,<commit_msg>add missing code in mixed cat post<commit_after>---
layout: post
title: Interactions between Categorical Variables in Mixed Graphical Models
category: r
---

{:center: style="text-align: center"}
In a [previous post](http://jmbh.github.io/Estimation-of-mixed-graphical-models/) we recovered the conditional independence structure in a dataset of *mixed variables* describing different aspects of the life of individuals diagnosed with Autism Spectrum Disorder, using the [mgm package](https://cran.r-project.org/web/packages/mgm/index.html). While depicting the independence structure in multivariate data set gives a first overview of the relations between variables, in most applications we interested in the exact parameter estimates. For instance, for interactions between continuous variables, we would like to know the sign and the size of parameters - i.e., if the nodes in the graph are positively or negatively related, and how strong these associations are. In the case of interactions between categorical variables, we are interested in the signs and sizes of the set of parameters that describes the exact non-linear relationship between variables.

In this post, we take the analysis a step further and show how to use the output of the [mgm package](https://cran.r-project.org/web/packages/mgm/index.html) to take a closer look at the recovered dependencies. Specifically, we will recover the sign and weight of interaction parameter between continuous variables and zoom into interactions between categorical and continuous variables and between two categorical variables. Both the dataset and the code are available on [Github](https://github.com/jmbh/AutismData).

We start out with the conditional dependence graph estimated in the previous post, however, now with *variables grouped by their type*:

![center](http://jmbh.github.io/figs/2017-11-30-Closer-Look/Autism_VarTypes.jpg) 

We obtained this graph by fitting a mixed graphical model using the mgmfit() function as in the [previous post](http://jmbh.github.io/Estimation-of-mixed-graphical-models/):

{% highlight r %}

# load data; available on Github
datalist <- readRDS('autism_datalist.RDS')
data <- datalist$data
type <- datalist$type
lev <- datalist$lev

library(devtools)
install_github('jmbh/mgm') # we need version 1.1-6
library(mgm)

fit <- mgmfit(data, type, lev, lambda.sel = "EBIC", d = 2)

{% endhighlight %}


Display Edge Weights and Signs
------

We now also display the weights of the dependencies. In addition, for interactions between continuous (Gaussian, Poisson) variables, we are able determine the sign of the dependency, as it only depends on one parameter. The signs are saved in `fit$signs`. To make plotting easier, there is also a matrix `fit$edgecolor`, which gives colors to positive (green), negative (red) and undefined (grey) edge signs. 

Now, to plot the weighted adjacency matrix with signs (where defined), we give `fit$edgecolor` as input to the argument edge.color in [qgraph](https://cran.r-project.org/web/packages/qgraph/index.html) and plot the weighted adjacency matrix `fit$wadj` instead of the unweighted adjacency matrix `fit$adj`:


{% highlight r %}

library(devtools)
install_github('SachaEpskamp/qgraph') # we need version 1.3.3
library(qgraph)

# define variable types
groups_typeV <- list("Gaussian"=which(datalist$type=='g'), 
                     "Poisson"=which(datalist$type=='p'),
                     "Categorical"=which(datalist$type=='c'))

# pick some nice colors
group_col <- c("#72CF53", "#53B0CF", "#ED3939")

jpeg("Autism_VarTypes.jpg", height=2*900, width=2*1300, unit='px')
qgraph(fit$wadj, # weighted adjacency matrix in model fit object 
       vsize=3.5, 
       esize=5, 
       layout= 'spring', # to get the exact same layout as above take it from qgraph object of the earlier post
       edge.color = fit$edgecolor, 
       color=group_col,
       border.width=1.5,
{'fraction_non_alphanumeric': 0.09270063529106704, 'fraction_numerical': 0.010501750291715286, 'mean_word_length': 4.549640287769784, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 3, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7664761', 'n_tokens_mistral': 2227, 'n_tokens_neox': 2061, 'n_words': 885}