The hardware and bandwidth for this mirror is donated by dogado GmbH, the Webhosting and Full Service-Cloud Provider. Check out our Wordpress Tutorial.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]dogado.de.

A short introduction to cross-network analysis with xnet

Meys Joris

2020-02-03

Concepts and terms used in the package.

Notation and naming of networks in the package

Networks exist in all forms and shapes. xnet is a simple, but powerful package to predict edges in networks in a supervised fashion. For example:

The two sets can contain the same types nodes (e.g. protein interaction networks) or different nodes (e.g. goods bought by clients in a recommender system). When the two sets are the same, we call this a homogeneous network. A network between two different sets of nodes is called a heterogeneous network.

The interactions are presented in a adjacency matrix, noted Y. The rows of Y represent one set of nodes, the columns the second. Interactions can be measured on a continuous scale, indicating how strong each interaction is. Often the adjacency matrix only contains a few values: 1 for interaction, 0 for no interaction and possibly -1 for an inverse interaction.

Two-step kernel ridge regression ( function tskrr() ) predicts the values in the adjacency matrix based on similarities within the node sets, calculated by using some form of a kernel function. This function takes two nodes as input, and outputs a measure of similarity with specific mathematical properties. The resulting kernel matrix has to be positive definite for the method to work. In the package, these matrices are noted K for the rows and - if applicable - G for the columns of Y.

We refer to the kernlab for a collection of different kernel functions.

Data in the package

For the illustrations, we use two different datasets.

Homogeneous networks

The example dataset proteinInteraction originates from a publication by Yamanishi et al (2004). It contains data on interaction between a subset of 769 proteins, and consists of two objects:

  • the adjacency matrix proteinInteraction where 1 indicates an interaction between proteins
  • the kernel matrix Kmat_y2h_sc describing the similarity between the proteins.

Heterogeneous networks

The dataset drugtarget serves as an example of a heterogeneous network and comes from a publication of Yamanishi et al (2008). In order to get a correct kernel matrix, we recalculated the kernel matrices as explained in the vignette Preparation of the example data.

The dataset exists of three objects

  • the adjacency matrix drugTargetInteraction
  • the kernel matrix for the targets targetSim
  • the kernel matrix for the drugs drugSim

The adjacency matrix indicates which protein targets interact with which drugs, and the purpose is to predict new drug-target interactions.

Fitting a two-step kernel ridge regression

Heterogeneous network

To fit a two-step kernel ridge regression, you use the function tskrr(). This function needs to get some tuning parameter(s) lambda. You can choose to set 1 lambda for tuning K and G using the same lambda value, or you can specify a different lambda for K and G.

Homogeneous network

For homogeneous networks you use the same function, but you don’t specify the G matrix. You also need only a single lambda:

Extracting parameters from a trained model.

The model output itself tells you only little, apart from the dimensions, the lambdas used and the labels found in the data. That information can be extracted using a number of convenient functions.

You can also use the functions rownames() and colnames() to extract the labels.

Information on the fit of the model

The functions fitted() and predict() can be used to extract the fitted values. The latter also allows you to specify new kernel matrices to predict for new nodes in the network. To obtain the residuals, you can use the function residuals(). This is shown further in the document.

Performing leave-one-out cross-validation

Settings for LOO

The most significant contribution of this package, are the various shortcuts for leave-one-out cross-validation (LOO-CV) described in the paper by Stock et al, 2018. Generally LOO-CV removes a value, refits the model and predicts the removed value based on this refit model. In this package you do this using the function loo(). The paper describes a number of different settings, which can be passed to the argument exclusion:

For some networks, only information of interactions is available, so a 0 does not necessarily indicate “no interaction”. It just indicates “no knowledge” for an interaction. In those cases it makes more sense to calculate the LOO values by replacing the interaction by 0 instead of removing it. This can be done by setting replaceby0 = TRUE.

In both cases the result is a matrix with the LOO values.

Use LOO in other functions

There are several functions that allow to use the LOO values instead of predictions for model tuning and validation. For example, you can calculate residuals based on LOO values directly using the function residuals():

Every other function that can use LOO values instead of predictions will have the same two arguments exclusion and replaceby0.

Looking at model output

The function provides a plot() function for looking at the model output. This function can show you the fitted values, LOO values or the residuals. It also lets you construct dendrograms based on distances computed using the K and G matrices, so you have both the predictions and the similarity information on the nodes in one plot.

plot(drugmodel, main = "Drug Target interaction")

To plot LOO values, you set the argument which. As the protein model is pretty extensive, we can remove the dendrogram and select a number of proteins we want to inspect closer.

plot(proteinmodel, dendro = "none", main = "Protein interaction - LOO",
     which = "loo", exclusion = "both",
     rows = rownames(proteinmodel)[10:20],
     cols = colnames(proteinmodel)[30:35])

If the colors don’t suit you, you can set both the breaks used for the color code and the color code itself.

plot(drugmodel, which = "residuals",
     col = rainbow(20),
     breaks = seq(-1,1,by=0.1))

Tuning a model to find the best lambda.

In most cases you don’t know how to set the lambda values for optimal predictions. In order to find the best lambda values, the function tune() allows you to do a grid search. This grid search can be done in a number of ways:

Tuning minimizes a loss function. Two loss functions are provided, i.e. one based on mean squared error (loss_mse) and one based on the area under the curve (loss_auc). But you can provide your own loss function too, if needed.

Homogeneous networks

Homogeneous networks have a single lambda value, and should hence only search in a single dimension. The following code tests 20 lambda values between 0.001 and 10.

The returned object is a again a model object with the model fitted using the best lambda value. It also contains extra information on the settings of the tuning. You can extract the grid values as follows:

This returns a list with one or two elements, each element containing the grid values for the respective kernel matrix.

You can also create a plot to visually inspect the tuning:

This object is also a tskrr model, so all the functions used above can be used here as well. For example, we can use the same code as before to inspect the LOO values of this tuned model:

Heterogeneous networks

For heterogeneous networks, the tuning works the same way. Standard, the function tune() performs a two-dimensional grid search. To do a one-dimensional grid search (i.e. use the same lambda for K and G), you set the argument onedim = TRUE.

When performing a two-dimensional grid search, you can specify different limits and grid values or lambda values for both dimensions. You do this by passing a list with two elements for the respective arguments.

the plot_grid() function will give you a heatmap indicating where the optimal lambda values are found:

As before, you can use the function lambda() to get to the best lambda values.

A one-dimensional grid search give might yield quite different optimal lambda values. To get more information on the loss values, the function get_loss_values() can be used. This allows you to examine the actual improvement for every lambda value. The output is always a matrix, and in the case of a 1D search it’s a matrix with one column. Combining these values with the lambda grid, shows that the the difference between a lambda value of around 0.20 and around 0.34 is very small. This is also obvious from the grid plots shown above.

Predicting new values

In order to predict new values, you need information on the outcome of the kernel functions for the combination of the new values and those used to train the model. Depending on which information you have, you can do different predictions. To illustrate this, we split up the data for the drugsmodel.

idk_test <- c(5,10,15,20,25)
idg_test <- c(2,4,6,8,10)

drugInteraction_train <- drugTargetInteraction[-idk_test, -idg_test]
target_train <- targetSim[-idk_test, -idk_test]
drug_train <- drugSim[-idg_test, -idg_test]

target_test <- targetSim[idk_test, -idk_test]
drug_test <- drugSim[idg_test, -idg_test]

So the following drugs and targets are removed from the training data and will be used for predictions later:

rownames(target_test)
#> [1] "hsa2103" "hsa4306" "hsa5915" "hsa6256" "hsa9970"
colnames(drug_test)
#>  [1] "D00040" "D00067" "D00088" "D00105" "D00143" "D00182" "D00187" "D00188"
#>  [9] "D00211" "D00246" "D00279" "D00299" "D00312" "D00316" "D00327" "D00348"
#> [17] "D00443" "D00462" "D00506" "D00554" "D00565" "D00577" "D00585" "D00586"
#> [25] "D00596" "D00627" "D00690" "D00730" "D00898" "D00930" "D00950" "D00951"
#> [33] "D00954" "D00956" "D00961" "D00962" "D00965" "D01115" "D01132" "D01161"
#> [41] "D01217" "D01294" "D01387" "D01441" "D01689" "D02217" "D02367" "D04066"
#> [49] "D05341"

We can now train the data using tune() just like we would use tskrr()

trained <- tune(drugInteraction_train,
                k = target_train,
                g = drug_train,
                ngrid = 30)

Predict for new K-nodes

In order to predict the interaction between new targets and the drugs in the model, we need to pass the kernel values for the similarities between the new targets and the ones in the model. The predict() function will select the correct G matrix for calculating the predictions.

Predict for new G-nodes

If you want to predict for new drugs, you need the kernel values for the similarities between new drugs and the drugs trained in the model.

Predict for new K and G nodes

You can combine both kernel matrices used above to get predictions about the interaction between new drugs and new targets:

Impute new values based on a tskrr model

Sometimes you have missing values in a adjacency matrix. These missing values can be imputed based on a simple algorithm:

  1. replace the missing values by a start value
  2. fit a tskrr model with the added values
  3. replace the missing values with the predictions of that model
  4. repeat until the imputed values converge (i.e. the difference with the previous run falls below a tolerance value)

Apart from the usual arguments of tskrr, you can give additional parameters to the function impute_tskrr. The most important ones are

So let’s construct a dataset with missing values:

drugTargetMissing <- drugTargetInteraction
idmissing <- c(10,20,30,40,50,60)
drugTargetMissing[idmissing] <- NA

Now we can try to impute these values. The outcome is again a tskrr model.

imputed <- impute_tskrr(drugTargetMissing,
                        k = targetSim,
                        g = drugSim,
                        verbose = TRUE)
#> Nr. of iterations: 80 - Deviation:1.4900616992948e-08
plot(imputed, dendro = "none")

To extract information on the imputation, you have a few convenience functions to your disposal:

has_imputed_values(imputed)
#> [1] TRUE
which_imputed(imputed)
#> [1] 10 20 30 40 50 60

# Extract only the imputed values
id <- is_imputed(imputed)
predict(imputed)[id]
#> [1]  0.17454499  0.03111647 -0.01402729 -0.03812844  0.28626726  0.06590431

You can use this information to plot the imputed values in context:

rowid <- rowSums(id) > 0
colid <- colSums(id) > 0
plot(imputed, rows = rowid, cols = colid)

These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.
Health stats visible at Monitor.