Showing posts with label analysis. Show all posts
Showing posts with label analysis. Show all posts

Monday, 21 July 2014

COMPREHENSIVE VIEW ON CRAN PACKAGES- Robust Analysis of Data In eXtreme (RADIX)





RADIX101 has been designed with an intention to ease the search of suitable packages for specific Data Analysis in R. As RADIX is well known for its Source/Origin of Something, here it refers to a systematic beginning of Robust Data Analysis towards Extreme Statistical Computing in R.



Purpose of RADIX101:
Ø  Systematic Approach to any Data Analytics using R.
Ø  Comprehensive categorization of numerous CRAN Packages into Data Specific Desired Outcomes.
Ø  Platform on Wide range of specialised statistical packages under various Data Analytic stages.

  

RADIX101 categorises CRAN packages into 3 main stages:-
1.      Pre Modelling stage:- Various Analysis performed on Raw Data prior suitable models are applied. This stage comprises of :-
•   Data Visualisation – Different kinds of visualisation plots to understand the Data spread.
•   Data Statistics – Statistical Exploration of Raw Data.
•   Data Transformation- Suitable Transformation on Raw Data prior to models.
•   Outlier Detection – Analysis of Outliers in the Raw Data.
•   Feature Selection- Prior Model Selection of important Independent/Predictor variables.
•   Dimension Reduction- Reduction of Rows (Input Fields) and Columns (Independent Variables) by combination or merging using suitable algorithms.

2.              Modelling stage:- Various analytical models to study the Data to observe and predict suitable insights and outcomes.

•   Continuous Regression- Basic and Advanced models on Regression of Continuous Data.
•   Ordinal Regression- Basic and Advanced models on Regression of Ordinal Data.
•   Classification- Basic and Advanced models on Classification predictions.
•   Clustering- Basic and Advanced algorithms for suitable Clustering methods.
•   Time Series- Time Series Analysis includes AR, MA, ARMA, ARIMA, SARIMA.
•   Survival – Survival Analysis includes Cos Proportion Hazard models, Kaplan Meir Curves, Risk Prediction models etc.
•   Association/Conjoint- To study and analyse the Transaction Patterns.
•   Probabilistic Choice – Analysis of choices using Probabilistic/Discrete choice theory.
•   Fraud Analytics- To study the frauds using Benfords algorithm.
•   Miscellaneous Models- Models for Text Mining, Customer Relationship Management, Boosting, Bagging etc.


3.      Post-Modelling Stage:- Various Evaluation packages for validation of models along with ROC analysis.
•   General Model Validation- To study model accuracy for model comparisons and diagnostics
•   Regression Validation- Validation of Regression Models.
•   Classification Validation- Validation of Classification Models.
•   Clustering Validation- Validation of Clustering Models.
•   ROC analysis- Receiver Operating Characteristic (ROC) curves for complete empirical descriptions.



P.S. 
 R is an Open Source Software Programming Language used for Statistical Computing, Data Analysis and Graphical Visualisations. A numerous packages have been created which allow specialised statistical techniques to be performed on various data sets with different business objectives. There are more than 5,800 additional packages (a core set of packages are already pre-installed) and 120,000 functions (as on June 2014) available at Comprehensive R Archive Network (CRAN).These Packages provide wide range of statistical techniques like Regression Analysis, Classification Analysis, Clustering Analysis etc. using a varied number of models such as Neural Networks, Bayesian Techniques, Nearest Neighbour etc. with some specialised in certain domains like HealthCare, Bio-Sciences, Genetic Studies etc. Graphical User Interfaces of R software like Rattle, Deducer, RStudio, RGUI etc. are also widely used with specific design outcomes.








Monday, 12 May 2014

Principal Component Analysis (PCA) and Factor Analysis in R



R Code  for Principal Component Analysis (PCA) and Factor Analysis (FA)
Data Set:- Bank Marketing https://www.blogger.com/blogger.g?blogID=4359514443959080595#editor/target=post;postID=5426750358330739357
Source of Data Set:-  UCI Repository (http://archive.ics.uci.edu/ml/datasets/Bank+Marketing)
The Code includes the following:-
1. Data Exploration - Missing Values, Outliers
2. Data Visualisation
3.  Correlation Matrix
4. Package "FactoMiner"
5. Topics Covered:- PCA, Correspondence analysis(CA), Dual Multiple Factor Analysis (DMFA), Factor Analysis for mixed data(FAMD), Generalised Procrustes Analysis (GPA), Hierarchical Tree
6. Detailed Plots


## The data has been imported using Import Dataset option in R Environment
## The data set can be obtained from http://archive.ics.uci.edu/ml/datasets/Bank+Marketing
## DATASET UNDERSTANDING
head(bank_full) ## Displays first 6 rows for each variable
str(bank_full) ## Describes each variables
summary(bank_full) ## Provides basic statistical information of each variable

## DATA EXPLORATION - Check for Missing Data
## Option 1
is.na(bank_full) ## Displays True for a missing value
## Since it is a large dataset, graphical display of missing values will prove to be easier
##Option 2
require(Amelia)
missmap(bank_full,main="Missing Data - Bank Subscription", col=c("red","grey"),legend=FALSE)

## No red colour stripes are visible. hence no missing values.
##Option 3
summary(bank_full) ## displays missing values if any under every variable

## DATA VISUALISATION
## Use Box plots (Only for continuous variables)- To Check Ouliers
boxplot(bank_full$age~bank_full$subscribed, main=" AGE",ylab="age of customers",xlab="Subscribed")
boxplot(bank_full$balance~bank_full$subscribed, main=" BALANCE",ylab="Balance of customers",xlab="Subscribed")
boxplot(bank_full$lastday~bank_full$subscribed, main=" LAST DAY",ylab="Last day of contact",xlab="Subscribed")
boxplot(bank_full$lastduration~bank_full$subscribed, main="LAST DURATION",ylab="Last duration of contact",xlab="Subscribed")
boxplot(bank_full$numcontacts~bank_full$subscribed, main="NUM CONTACTS",ylab="number of contacts",xlab="Subscribed")
boxplot(bank_full$pdays~bank_full$subscribed, main=" Previous DAYS",ylab="Previous days of contact",xlab="Subscribed")
boxplot(bank_full$pcontacts~bank_full$subscribed, main=" Previous Contacts",ylab="Previous Contacts with customers",xlab="Subscribed")

## Though some outliers are observed in Previous contacts, NumContacts and LastDuration, they have not bee removed keeping their significance into consideration
## Use Histograms (For both continuous and categorical variables)
## These histograms provide details abpout Skewness, Normal Distribution etc
## Function to create histograms for continuous variables with normal curve
bank_Conthist<-function(VarName,NumBreaks,xlab,main,lengthxfit) ## xlab and main should be mentioned under quotes as they are characters
{
  hist(VarName,breaks=NumBreaks,col="yellow",xlab=xlab,main=main)
  xfit<-seq(min(VarName),max(VarName),length=lengthxfit)
  yfit<-dnorm(xfit,mean=mean(VarName),sd=sd(VarName))
  yfit<-yfit*diff(h$mids[1:2])*length(VarName)
  lines(xfit,yfit,col="red",lwd=3)
}
bank_Conthist(bank_full$age,10,"age of customers","AGE",30)
bank_Conthist(bank_full$balance,50,"Balance of customers","Balance",100)

## Balance is more skewed towards to Negative or Zero
bank_Conthist(bank_full$lastday,5,"Last Day of contact","LAst Day",10)
bank_Conthist(bank_full$lastduration,100,"LastDuration of COntact","Last Duration",10)

## Last Duration is more skewed towards 0 to 100 secs.
bank_Conthist(bank_full$numcontacts,30,"Number of Contacts","NUmContacts",20)
## NUmContacts are more skewed towards 1
bank_Conthist(bank_full$pdays,30,"Previous Days of contacts","PDays",20)
## Many were not contacted previously
bank_Conthist(bank_full$pcontacts,20,"Previous Contacts","PContacts",10)
## Since many were not contacted previously, therefore Pcontacts is 0

## Barplots for Categorical Variables
barplot(table(bank_full$job),col="red",main="JOB")
barplot(table(bank_full$marital),col="green",main="Marital")
barplot(table(bank_full$education),col="red",main="Education")
barplot(table(bank_full$creditdefault),col="red",main="Credit Default")

## Since Credit Default is highly skewed towards NO, this shall be removed from further analysis
bank_full[5]<-NULL
str(bank_full)
barplot(table(bank_full$housingloan),col="red",main="Housing Loan")
barplot(table(bank_full$personalloan),col="blue",main="Personal Loan")
barplot(table(bank_full$lastcommtype),col="red",main="Last communication type")
barplot(table(bank_full$lastmonth),col="violet",main="Last Month")
barplot(table(bank_full$poutcome),col="magenta",main="Previous Outcome")

## Correlation Matrix among input (or independent) continuous variables
bank_full.cont<-data.frame(bank_full$age,bank_full$balance,bank_full$lastday,bank_full$lastduration,bank_full$numcontacts,bank_full$pdays,bank_full$pcontacts)
str(bank_full.cont)
cor(bank_full.cont)

## It can be observed that No two variables are highly correlated


## First, Lets convert factors having character levels to numeric levels
str(bank_full)
bank_full_transform<-bank_full
bank_full_transform$marital=factor(bank_full_transform$marital,levels=c("single","married","divorced"),labels=c(1,2,3))
bank_full_transform$job=factor(bank_full_transform$job,levels=c("admin.","blue-collar","entrepreneur","housemaid","management","retired","self-employed","services","student","technician","unemployed","unknown"),labels=c(1,2,3,4,5,6,7,8,9,10,11,12))
bank_full_transform$education=factor(bank_full_transform$education,levels=c("primary","secondary","tertiary","unknown"),labels=c(1,2,3,4))
bank_full_transform$housingloan=factor(bank_full_transform$housingloan,levels=c("no","yes"),labels=c(1,2))
bank_full_transform$personalloan=factor(bank_full_transform$personalloan,levels=c("no","yes"),labels=c(1,2))

bank_full_transform$lastcommtype=factor(bank_full_transform$lastcommtype,levels=c("cellular","telephone","unknown"),labels=c(1,2,3))
bank_full_transform$poutcome=factor(bank_full_transform$poutcome,levels=c("failure","other","success","unknown"),labels=c(1,2,3,4))
bank_full_transform$lastmonth=factor(bank_full_transform$lastmonth,levels=c("apr","aug","dec","feb","jan","jul","jun","mar","may","nov","oct","sep"),labels=c(1,2,3,4,5,6,7,8,9,10,11,12))
str(bank_full_transform)

## Now convert these numerical factors into numeric
bank_full_transform$job<-as.numeric(as.character(bank_full_transform$job))
bank_full_transform$marital<-as.numeric(as.character(bank_full_transform$marital))
bank_full_transform$education<-as.numeric(as.character(bank_full_transform$education))
bank_full_transform$personalloan<-as.numeric(as.character(bank_full_transform$personalloan))
bank_full_transform$housingloan<-as.numeric(as.character(bank_full_transform$housingloan))
bank_full_transform$lastcommtype<-as.numeric(as.character(bank_full_transform$lastcommtype))
bank_full_transform$lastmonth<-as.numeric(as.character(bank_full_transform$lastmonth))
bank_full_transform$poutcome<-as.numeric(as.character(bank_full_transform$poutcome))
bank_full_transform$age<-as.numeric(as.character(bank_full_transform$age))
bank_full_transform$balance<-as.numeric(as.character(bank_full_transform$balance))
bank_full_transform$lastday<-as.numeric(as.character(bank_full_transform$lastday))

bank_full_transform$lastduration<-as.numeric(as.character(bank_full_transform$lastduration))
bank_full_transform$numcontacts<-as.numeric(as.character(bank_full_transform$numcontacts))
bank_full_transform$pdays<-as.numeric(as.character(bank_full_transform$pdays))
bank_full_transform$pcontacts<-as.numeric(as.character(bank_full_transform$pcontacts))
str(bank_full_transform)

##In bank_full_transform; the subscribed table is not quantitative
bank_full_transformfull<-bank_full_transform
bank_full_transformfull$subscribed<-factor(bank_full_transformfull$subscribed,levels=c("no","yes"),labels=c(1,2))
bank_full_transformfull$subscribed<-as.numeric(as.character(bank_full_transformfull$subscribed))
all(is.na(bank_full_transformfull))
write.table(bank_full_transformfull,"x1.csv",sep=",")
str(bank_full_transformfull)

## Now all the variables are either intergers or numeric except SUBSCRIBED (which is factor)



## Multivariate Exploratory Data Analysis using FACTOMINER Package
install.packages("FactoMineR")
library(FactoMineR)

## Two-Way Analysis of Variance with the contrasts sum (The sum of coefficients is Zero)
## T test and F test
AovSum(subscribed~.,data=bank_full)
## Two-Way Anova with interaction with Age
AovSum(subscribed~.:age,data=bank_full)
##RV Coefficient:- Squared Pearson Correlation Coefficient. It measures the closeness of 2 sets of points in a matrix.
X<-bank_full[,1:8]
Y<-bank_full[,9:14]

coeffRV(X,Y)
## Correlation of One Categorical or Continuous variable with other Categorical or quantitative variables
## Lets check with respect to Age variable (whose num.var=1, since it is first variable column)
condes(bank_full[1:14],num.var=1,proba=0.05)


 ## 1. Correspondence Analysis(CA) including supplementary row and (or)column points
## Applicable only on Categorical Data in Numeric or Integer format
bank_CA<-CA(bank_full_transform,ncp=5,col.sup=10:13,row.sup=1:100,quanti.sup=1:4,quali.sup=15,graph=TRUE)
summary(bank_CA)

## Dimension Description : It points out the variables and categories which are more characteristic according to each dimensionobtained by factor analysis
dimdesc(bank_CA,axes=1:3,proba=0.05)
## Visualise the active elements
plot(bank_CA,invisible=c("row.sup","col.sup"))

##2. PRINCIPAL COMPONENT ANALYSIS (PCA)
## Initially estimate the number of components in PCA
estim_ncp(bank_full_transform[,1:15],ncp.min=0,ncp.max=NULL,scale=TRUE, method="GCV")
## Using the above estmate, use the value as ncp. Here we get ncp=1
## PCA analysis for the above data
bank_pca<-PCA(bank_full_transformfull,ncp=5,quanti.sup=NULL,quali.sup=15)
summary(bank_pca)

##bar plot with the eigen values
barplot(bank_pca$eig[,1],main="Eigen Values",names.arg=paste("Dim",1:nrow(bank_pca$eig),sep=""))
##Print the results of PCA
print(bank_pca)
bank_pca$var$coord

## Lets observe the PCA interpretation using third and fourth dimension as your axes
plot(bank_pca,choix="var",axes=c(3,4),lim.cos2.var=0)
## Construction of Confidence Ellipses around the barycentres of all categorical variables
a<-cbind.data.frame(bank_full_transform[,16],bank_pca$ind$coord)
b<-coord.ellipse(a,bary=TRUE)

##habillage=15 indicates that the individuals are coloured according to 15th variable
plot(bank_pca,habillage=15,ellipse=b)
plotellipses(bank_pca)

## Dimension Description : It points out the variables and categories which are more characteristic according to each dimensionobtained by factor analysis
dimdesc(bank_pca,axes=1:3,proba=0.05)

## Graph of variable after factor analysis
graph.var(bank_pca,axes=c(1,2),xlim="Comp-1",ylim="Comp-2",title="Graph of Variables after PCA")
## Hierarchical clustering on Principal Components
HCPC(bank_pca,nb.clust=-1)





## 3. Dual Multiple Factor Analysis (DMFA) with supplementary individuals, supplementary quantitative variables and supplementary categorical variables
bank_dmfa<-DMFA(bank_full_transform,num.fact=16,scale.unit=TRUE,ncp=3,quanti.sup=NULL,quali.sup=16,graph=TRUE,axes=c(1,2))
summary(bank_dmfa)

## Construction of Confidence Ellipses around the barycentres of all categorical variables
a<-cbind.data.frame(bank_full_transform[,16],bank_dmfa$ind$coord)
b<-coord.ellipse(a,bary=TRUE)
plot.PCA(bank_dmfa,habillage=16,ellipse=b)
plotellipses(bank_dmfa)

## Dimension Description : It points out the variables and categories which are more characteristic according to each dimensionobtained by factor analysis
dimdesc(bank_dmfa,axes=1:3,proba=0.05)




## 4. FACTOR ANALYSIS FOR MIXED DATA (Continuous and Catgeorical Data)
bank_famd<-FAMD(bank_full_transform[,1:15],ncp=5,graph=TRUE,axes=c(1,2))
summary(bak_famd)

## Dimension Description : It points out the variables and categories which are more characteristic according to each dimensionobtained by factor analysis
dimdesc(bank_famd,axes=1:3,proba=0.05)


## 5. Generalised PROCRUSTES ANALYSIS(GPA)
## It take sinto account all the missing values and Applicable only for quantitative (continuous) data sets
bank_full_transform_cont<-data.frame(bank_full_transform$age,bank_full_transform$balance,bank_full_transform$lastday,bank_full_transform$lastduration,bank_full_transform$numcontacts,bank_full_transform$pdays,bank_full_transform$pcontacts)
bank_gpa<-GPA(bank_f
ull_transform_cont,group=c(1,2,3))

## Construction of Partial Points only for some indivuals
plotGPApartial(bank_gpa)
## Dimension Description : It points out the variables and categories which are more characteristic according to each dimensionobtained by factor analysis
dimdesc(bank_famd,axes=1:3,proba=0.05)


## 6. HIERARCHICAL TREE FROM A DATA SET
## Usefull when all the variables play a significant role
## initially the data is partitioned using k means clustering (here kk=10 clusters made)
##If nb.clust= 0 (the tree is cut at the level the user clicks on); -1 (the tree is automatically cut at the suggested level)
HCPC(bank_full_transform[,1:15],kk=10,nb.clust=-1)