package mycor

knitr::opts_chunk$set(collapse=TRUE, comment=NA)


For correlation analysis : cor, cor.test and lm

When I began to study R software, I was really impressed with the function cor. As you knows, the 'cor' function returns all r values of every possible pairs of matrix or data.frame provided that it consists of numeric data only. For example, cor(mtcars[1:5]) acts just as expected.


But cor(iris) returns error because the data.frame consist of both numeric and factor variable.


If you wanted to get p values as well as r values, you should use cor.test instead of cor. But cor.test can deal with only one pair of numeric vectors of the same length, neither a matrix nor a data.frame. Furthermore, if you wanted to get the slope and intercept of simple linear regression line of xyplot, you had to perform lm test for every pairs of numeric variables of the data.frame.

Solution ; Do not repeat yourself !!

My idea is that a single function deals with data.frame of mixed numeric, logical and factor variables, select numeric variables, perform cor.test and lm to get r,p,slope and intercept of every pairs of the variables for exploratory analysis. It can save my time and effort.

Function "mycor" and Class "mycor"

Use of mycor function is simple. Just call mycor with a data.frame. For example, just call cor(iris). Unlikely cor, it does not result in an error.


The mycor function reurns an object of class "mycor". This can be saved for print, summarize and plot. A S3 method for class formula can be used to function mycor. Function print.mycor shows the r values and th p values similar to the function cor. A mycor class object can be summarized with summary function, summary().

out=mycor(iris,alternative="greater", method="kendall",digits=2)

The mycor function uses cor.test internally, so you can use all options of cor.test - namely alternatives, method, conf.level,... .

Plot "mycor" object

Probably most valuable function is plot. It is not a new function. It uses internally one of two popular function : graphics::pairs() and lattice::parallelplot(). In fact, plot.mycor function have four types of plot : Three variants of pairs and parallelplot. Call function plot with no option makes pairs().


But if you specify the groups, you can get more pretty plot. You can use extra arguments which can used in pairs() or parallelplot().

plot(out,groups=species,main="Test of mycor::plot")

With type=2 option, you can get histogram at diagonal panel.


With type=3 option, you can get correlation plot at upper panels.


With type=4 option, you can get parallelplot.


Try the mycor package in your browser

Any scripts or data that you put into this service are public.

mycor documentation built on May 2, 2019, 2:12 p.m.