Skip to content

Repository files navigation

R_data_analysis_project

Global information


Authors :


- Aniss Ilyes BENTEBIB (1st year of Master degree [computer science] related to Natural Language Processing).
- Camille-Amaury JUGE (1st year of Master degree [computer science] related to Data Science).
- This package is under the license : CC-BY-4.0, free to use except for commercial.

Objectives :


- see the file nammed "Exercise_Lise_Bellanger_Univ_Nantes.pdf".
- This project is related to the 1st year master Degree "Analyse de de données" held by Lise BELLANGER-HUSI in 2019.
- see the final report of this project nammed "JUGE_BENTEBIB_ADD_Project.pdf".

Language, Tools used :


- R
- Anaconda 3 Environment
- Trello and Git
- Rstudio

Files :


- rapport : "/Rapport/Rapport ADD.pdf"
- annexes : "/Rapport/Annexes.pdf"
- Dataset : "/dataset.csv"
- tested function : "/R/test/"
- function : "/R/"

Documentation


Installation and beginning



After the package is installed, just use it as a normal library in R.

library(nantes_statistical_analysis)

Principal component analysis



The pca function have three main categories :
- The base objects (matrix and calculation to prepare our pca)
- The pca's axes, factors, projection of variables and individuals and more ...
- The pca's interpreted datas (inertia, cumulative percentages, correlation)


#By Default
nantes_pca <- pca(M, center=TRUE, scale=TRUE, bias=FALSE, Q=diag(dim(M)[2]), D=(1/dim(M)[1])*diag(dim(M)[1]), axisMethod="kaiser", axisNumber=-1)


#Examples

#Centered pca
nantes_pca <- pca(M, scale=FALSE)
#biased-Centered pca
nantes_pca <- pca(M, scale=FALSE, bias=TRUE)
#Centered-reduced pca, if scale is true, whatever center is specified it will be also true
# note that bias is useless in a scale pca
nantes_pca <- pca(M, scale=TRUE)
# specified axis number, won't take in consideration the determination algorithm for the number of axis
nantes_pca <- pca(M, scale=TRUE, axisNumber=2)
# auto determine the number of axis by the kaiser or elbow method
nantes_pca <- pca(M, scale=TRUE, axisMethod="kaiser")
nantes_pca <- pca(M, scale=TRUE, axisMethod="elbow")
# for a totally different pca, just set all to FALSE, and you can change the individuals metric matrix D, and variables metric matrix Q

This will return a list of pca components including :
# Base components
nantes_pca$base_component$base #the main matrix
nantes_pca$base_component$rownames #the dataframe's rownames or a auto rownames
nantes_pca$base_component$colnames #the dataframe's colnames or a auto colnames
nantes_pca$base_component$G #the gravity center of your matrix
nantes_pca$base_component$M_centered #the centered matrix
nantes_pca$base_component$S #the covariance matrix (if bias=TRUE S main diagonal is the biaised variance 1/n)
# Depending on the type of PCA you aim to do, the following objects won't appear
# --> "scale=TRUE" will enable thos following objects to appear
nantes_pca$base_component$D1_div_sd #the matrix which contains on its diagonal the (1/standard deviation) of the column 
nantes_pca$base_component$M_scale #the centered and reduced matrix
nantes_pca$base_component$R #the correlation matrix


# PCA components
nantes_pca$PCA_component$Si #the incidence matrix
nantes_pca$PCA_component$values #eigen values of the incidence matrix
nantes_pca$PCA_component$vectors #eigen vectors of the incidence matrix
nantes_pca$PCA_component$rank #the rank of the Si matrix
nantes_pca$PCA_component$Fi #Individuals matrix projection
nantes_pca$PCA_component$Gi #Variables matrix projection


# PCA components
nantes_pca$PCA_interpretation$qlt_Fi #the relative contribution (qlt) of individuals
nantes_pca$PCA_interpretation$ctr_Fi #the absolute contribution (ctr) of individuals
nantes_pca$PCA_interpretation$qlt_Gi #the relative contribution (qlt) of variables
nantes_pca$PCA_interpretation$ctr_Gi #the absolute contribution (ctr) of variables

Kmeans



The categorical algorithm for classification K-means :
- Returns the cluster as a unique vector with every individuals as an indice, and his value his cluster number
- Returns the clusters' centers point
- Implements multiple methods (distance, initialization)


#By Default
nantes_kmeans <- kmeansfunction(M, k=-1, d="euclidian", initialization="random", precision=0.001, logs=FALSE, seed = -1, kMax = -1)

This will return a list compunded of clusters' centers point and cluster's membership of individuals :
# returns
nantes_pca$centers #the clusters' center points as a matrix
nantes_pca$clusters #cluster's membership of individuals as a vector
nantes_pca$k #the choosen k cluster (automatic or not depending if k specified or not)


#Examples

# initialization algorithm :
# random will take "k" points randomly in all individuals
nantes_kmeans <- kmeansfunction(M, initialization="random")
# we advize you to choose this one rather than the last one, see why here : https://en.wikipedia.org/wiki/K-means%2B%2B
# choose the first point randomly and then compute a probability for every other point regarding to the euclidian distance
nantes_kmeans <- kmeansfunction(M, initialization="kmeans++")

# distance choosen :
# basic euclidian distance between center and a point
nantes_kmeans <- kmeansfunction(M, d="euclidian")
# Distance of mahalanobis, which is sometimes more efficient because it doesn't take an account of outliers
nantes_kmeans <- kmeansfunction(M, d="mahalanobis")

# precision parameters
# precision limit between two iterations , note that if they are more than 1000 iterations it will stops even precision is not respected
nantes_kmeans <- kmeansfunction(M, precision=0.001)

# logs parameters
# if you want to print all steps (clusters and center points) else FALSE
nantes_kmeans <- kmeansfunction(M, logs=TRUE)

# k parameters
# the cluster number, note that if you let it to -1, it will automatically do 1:k clusters and choose the best one
nantes_kmeans <- kmeansfunction(M, k=-1) # k will be choosen within the algorithm
nantes_kmeans <- kmeansfunction(M, k=2) # k is specified, this will only create two clusters

# kMax parameters
# from 2 to min(number of indivduals, kMax) iteration to determines the best cluster number
nantes_kmeans <- kmeansfunction(M, kMax=-1) # k is not specified, will do from 2:10 clusters
nantes_kmeans <- kmeansfunction(M, kMax=100) # specified kMax, will do from 2:100 clusters

# seed parameters
# the seed affected to probability, enables to find the same initial clusters with the random method
nantes_kmeans <- kmeansfunction(M, seed=-1) # not specified
nantes_kmeans <- kmeansfunction(M, seed=100) # specified seed

Eigen reproduced function



We decided to work additionally on implementing our own Eigen function :
- returns the same parameters than R's eigen function
- Use the QR algorithm, with the method of HouseHolders, which is a numerically stable algorithm rather than Givens ones.
- Is really efficient on small sized and symmetric matrix, else vectors suffers of round problems (we didn't find the solution to improve it)

#By Default
nantes_eigen <- eigenfunction(M)

plotAndExplain



Function which print and plot all the data of you pca/kmeans/one or two dimensions analysis :

#To explain PCA
explainPCA(pca_result, individualRate=1/n, variableRate=1/p)
#To explain Kmeans, M represents the projection of variables/individuals, kmeans the return of the kmeans function apply to initial datas
explainKmeans(M, kmeans) 
#To explain Dataset monodimensional
explainDataset(M, colnames, bidimensionnal=FALSE)
#To explain Dataset bidimensional
explainDataset(M, colnames, bidimensionnal=TRUE)

About

first year Master degree project, in data analysis and statistics.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages