4) Ranking Correlation by Absolute Strength.

This Blog entry is from the Linear Regression section in Learn R.

In the previous Blog entry a correlation matrix was created and the first column was transposed into a matrix by the name PearsonCorrelation.  The PearsonCorrelation matrix has the strength of relationship between each of the independent variables and the dependent variables.

The first task is to order the variables by their strength of their ABSOLUTE correlation, as both -1 and +1 are equally interesting extremes.  The abs() function in R makes this transformation effortless:

PearsonDependentAbs <- abs(PearsonDependent)

Run the line of script to console:

It can be seen from the environment pane window that a new matrix has been created:

In this instance, any negative number has been turned into a positive number, as observed by a single click in the environment pane:

The task remains to order the matrix by highest value to the lowest value.  This can be achieved with a simple click on the column in the matrix viewer (click once for ascending, again for descending):

While there are methods to order a matrix in R, they are extremely convoluted and the arrange() function as presented in dplyr does not work as the matrix is not a data frame (although it could be converted to a data frame by just passing the matrix to the data.frame() function).

In view of this process being exploratory and not necessarily needing to be recreated, the manual ordering in the view pane is adequate.

3) Create a Correlation Matrix using Spearman and Pearson.

This Blog entry is from the Linear Regression section in Learn R.

Correlation is a measure of relationship and direction of that relationship.  It is a single value that ranges from -1 to +1, which would signal the direction and strength of a relationship.  Both -1 and +1 are, in their extremes, equally interesting.  A correlation matrix takes all the variables together and produces the correlation value, the strength of their relationship in one director of another, between each variable.

The matrix will be the foundation for many of the techniques used in the following Blog entry. In R the cor() function is used to produce correlation matrices upon data frames.  To create a Pearson correlation matrix:

Pearson = cor(FDX,use="complete",method="pearson")

It can be seen that the cor() function takes the FDX data frame as its source.  The method argument specifies which type of correlation calculation to perform, an alternative would be "spearman".

Lastly "use" argument tells the cor() function how to deal with missing or bad data,  whereby the default is to throw an error,  hence it is a good idea to specify "complete" when working with very large datasets else it is likely the entire matrix would be returned as "NA".

Run the line of script to console:

It can be seen that a matrix by the name of Pearson has been created and is available in the environment pane:

Clicking on the entry in the environment pane would expand a view panel and display a more visually satisfying correlation matrix:

As the Pearson correlation is a matrix object, it can be interacted with via subscripting.  While the correlation matrix is extremely useful for identifying collinearity, at this stage the main point of interest is the relationships to the dependent variable only.

To return just the Dependent column:

PearsonDependent <- Pearson[,"Dependent",drop="false"]

In this example the matrix is being subset to bring back all rows by leaving the first argument blank,  while specifying only the "Dependent" column.  By default subsetting will return the simplest structure and it cannot be assumed that it will be the same structure as the oroginal matrix,  hence the drop="false" argument is used to ensure that the structure is the same (this is to say a matrix of rows and colums).

Run the line of script to console:

It can be seen that a new matrix has been created in the environment pane:

Clicking on the new matrix titled PearsonDependent will expand into the script window:

It can be seen that only the first column has been returned making the matrix less foreboding to work with in subsequent Blog entries.