This package was motivated by correlation issues in real datasets, in particular industrial datasets.
The main idea stands in explicit modeling of the correlations between covariates by a structure of sub-regressions (so it can model complex links, not only correlations between two variables), that simply is a system of linear regressions between the covariates. It points out redundant covariates that can be deleted in a pre-selection step to improve matrix conditioning without significant loss of information and with strong explicative potential because this pre-selection is explained by the structure of sub-regressions, itself easy to interpret. An algorithm to find the sub-regressions structure inherent to the dataset is provided, based on a full generative model and using Monte-Carlo Markov Chain (MCMC) method. This pre-treatment does not depend on a response variable and thus can be used in a more general way with any correlated datasets.
In a second part, a plug-in estimator is defined to get back the redundant covariates sequentially. Then all the covariates are used but the sequential approach acts as a protection against correlations.
This package also contains some functions to make statistics easier (see BoxPlot
and recursive_tree
or matplot_zone
).
In this vignette we explain the main method in CorReg that leads to the correg
function. This function allows to make linear regression using sub-regressions structure and/or many variable selection methods (including lasso, ridge, clere, stepwise,…)
We first generate (for this tutorial) a dataset with strong correlations between the covariates. The mixture_generator
function gives such a dataset and also a validation sample built with the same parameters. Both contains a response variable generated on some covariates (not all) by linear regression. So we have sub-regressions that make some variables redundent when we know the other variables. Such correlations make the variance of the regression estimators explode. Moreover, dimension is unnecessarily high and interpretation of regression coefficients is dangerous.
require(CorReg)
## Loading required package: CorReg
#dataset generation
base=mixture_generator(n=15,p=10,ratio=0.4,tp1=1,tp2=1,tp3=1,positive=0.5,
R2Y=0.8,R2=0.9,scale=TRUE,max_compl=3,lambda=1)
X_appr=base$X_appr #learning sample
Y_appr=base$Y_appr #response variable for the learning sample
Y_test=base$Y_test #responsee variable for the validation sample
X_test=base$X_test #validation sample
TrueZ=base$Z#True generative structure (binary adjacency matrix)
We also get an adjacency matrix Z that describes the Directed Acyclic Graph of the links between the covariates. See https://en.wikipedia.org/wiki/Adjacency_matrix for more informations on adjacency matrices.
TrueZ
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 0 0 0 0 0 0 0 0 0 0
## [2,] 1 0 0 0 0 0 0 1 0 1
## [3,] 0 0 0 0 0 0 0 0 0 0
## [4,] 1 0 1 0 0 0 0 1 0 0
## [5,] 1 0 1 0 0 0 0 1 0 0
## [6,] 0 0 1 0 0 0 0 0 0 1
## [7,] 0 0 0 0 0 0 0 0 0 1
## [8,] 0 0 0 0 0 0 0 0 0 0
## [9,] 0 0 0 0 0 0 0 0 0 0
## [10,] 0 0 0 0 0 0 0 0 0 0
#TrueZ[i,j]==1 means that X[,j] linearly depends on X[,i]
We will try to find this structure (in real life we don’t know the true structure).
To find the correlation structure we need a global density model that will serve as a null hypothesis. Each variable can be independent (following a certain density we have to estimate) or can be lineary dependent on other covariates. The density_estimation
function will provide this null hypothesis for each variable. We use Gaussian Mixture to fit a large scope of density in real life.
#density estimation for the MCMC (with Gaussian Mixtures)
density=density_estimation(X=X_appr,nbclustmax=10,detailed=TRUE,package = "Rmixmod")
Bic_null_vect=density$BIC_vect# vector of the BIC found (1 value per covariate)
Each null hypothesis is associated to a BIC criterion. Complexity is the one of each mixture model.
We use a specific MCMC algorithm (http://www.theses.fr/2015LIL10060 and http://www.theses.fr/2015LIL10060/document/ or https://hal.archives-ouvertes.fr/hal-01099133/) to find the Adjacency matrix that describes the correlation structure. We use the Bayesian Information Criterion (BIC) to compare null hypothesis and complex correlations structures with many sub-regressions between covariates.
#MCMC to find the structure
res=structureFinder(X=X_appr,verbose=0,reject=0,Maxiter=1500,
nbini=30,candidates=-1,Bic_null_vect=Bic_null_vect,star=TRUE,p1max=15,clean=TRUE)
hatZ=res$Z_opt #found structure (adjacency matrix)
hatBic=res$bic_opt #associated BIC
As we work with a dataset we have generated, we know the true structure so we can compare the result with the true model. In terms of BIC:
#BIC comparison between true and found structure
bicopt_vect=BicZ(X=X_appr,Z=hatZ,Bic_null_vect=Bic_null_vect)
bicopt_True=BicZ(X=X_appr,Z=TrueZ,Bic_null_vect=Bic_null_vect)
sum(bicopt_vect)
## [1] 246.7436
sum(bicopt_True)
## [1] 266.503
And in terms of structure :
#Structure comparison
compZ=compare_struct(trueZ=TrueZ,Zalgo=hatZ)#qualitative comparison
compZ
## $ratio_true1
## [1] 0.08333333
##
## $ratio_true0
## [1] 0.8636364
##
## $true1
## [1] 1
##
## $false1
## [1] 12
##
## $false0
## [1] 11
##
## $deltadr
## [1] -2
##
## $true_left
## [1] 1
##
## $false_left
## [1] 5
Then we have a look on the sub-regressions themselves. Each item of the list represents a subregression.
First element is the R-square.Second element is the variable that is regressed by others (if no names provided it gives the position of the variable in the dataset).
Then comes the list of the explicative variables in the subgression and the associated coefficients (in the first column). The readZ
function allows to list them by relevance.
#interpretation of found and true structure ordered by increasing R2
readZ(Z=hatZ,crit="R2",X=X_appr,output="all",order=1)# <NA>line : name of subregressed covariate
## [[1]]
## coefs var
## 1 0.0373825935369688 R2
## 2 <NA> 9
## 3 -0.377130976690436 intercept
## 4 0.450584282946823 6
##
## [[2]]
## coefs var
## 1 0.708414811517319 R2
## 2 <NA> 4
## 3 -1.91729035914169 intercept
## 4 2.40975499650083 8
##
## [[3]]
## coefs var
## 1 0.81771649406235 R2
## 2 <NA> 5
## 3 -1.07727349807673 intercept
## 4 1.07014269379456 1
##
## [[4]]
## coefs var
## 1 0.87568544200595 R2
## 2 <NA> 7
## 3 2.34525072338656 intercept
## 4 0.470341192816987 1
## 5 0.575505410187899 6
## 6 -0.879867591711528 8
## 7 2.11783937286341 10
##
## [[5]]
## coefs var
## 1 0.87714187217588 R2
## 2 <NA> 3
## 3 1.70251248310966 intercept
## 4 -0.223882637426385 1
## 5 0.287083616238587 6
## 6 -0.60588355839347 8
## 7 0.180024268283803 10
##
## [[6]]
## coefs var
## 1 0.912295874749831 R2
## 2 <NA> 2
## 3 -1.21164195380908 intercept
## 4 -0.86466568601952 1
## 5 1.81464335248824 8
readZ(Z=TrueZ,crit="R2",X=X_appr,output="all",order=1)# <NA>line : name of subregressed covariate
## [[1]]
## coefs var
## 1 0.786895812190988 R2
## 2 <NA> 10
## 3 -0.869722329666876 intercept
## 4 0.203889840163848 2
## 5 -0.225790366294011 6
## 6 0.40217790702684 7
##
## [[2]]
## coefs var
## 1 0.866992030606987 R2
## 2 <NA> 8
## 3 0.941792693823175 intercept
## 4 0.214614944081325 2
## 5 0.204802435978037 4
## 6 0.119984501894889 5
##
## [[3]]
## coefs var
## 1 0.875536584941473 R2
## 2 <NA> 1
## 3 0.76001676358717 intercept
## 4 -0.378273339597977 2
## 5 0.221315320105724 4
## 6 0.496988130943177 5
##
## [[4]]
## coefs var
## 1 0.909960195736572 R2
## 2 <NA> 3
## 3 0.847437377347586 intercept
## 4 -0.269709789805811 4
## 5 -0.175420579010794 5
## 6 0.235175638485818 6
We can finally use the correg function to make linear regression using the sub-regression structure learnt above. We get three models :
#Regression coefficients estimation
select="NULL"#without variable selection (otherwise, choose "lar" for example to use lasso selection)
resY=correg(X=X_appr,Y=Y_appr,Z=hatZ,compl=TRUE,expl=TRUE,pred=TRUE,
select=select,K=10)
#MSE computation
MSE_complete=MSE_loc(Y=Y_test,X=X_test,A=resY$compl$A)#classical model on X
MSE_marginal=MSE_loc(Y=Y_test,X=X_test,A=resY$expl$A)#reduced model without correlations
MSE_plugin=MSE_loc(Y=Y_test,X=X_test,A=resY$pred$A)#plug-in model
MSE_true=MSE_loc(Y=Y_test,X=X_test,A=base$A)# True model
Then we can compare the MSE obtained for each model.
#MSE comparison
MSE=data.frame(MSE_complete,MSE_marginal,MSE_plugin,MSE_true)
MSE#estimated structure
## MSE_complete MSE_marginal MSE_plugin MSE_true
## 1 317.282 402.6804 167.1834 119.3847
barplot(as.matrix(MSE),main="MSE on validation dataset", sub=paste("select=",select),col="blue")
abline(h=MSE_complete,col="red")