Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R programming MCA() in FactoMineR error message

Tags:

r

r-package

I was using the MCA() function from FactoMineR package in R to do the multiple correspondence analysis on a set of around 160 variables with around 2000 observations. Around 150 of the variables are continuous, so I first used the cut() function to convert those continuous variables to categorical variables and then used MCA() function.

My code is very simple like this:

library(FactoMineR)

data<-read.csv('demographics.csv')

for (i in 9:length(data)){

   temp<-unlist(data[i],use.names=FALSE)

   data[i]<-cut(temp,breaks=5,labels=c('A','B','C','D','E'))
}

MC<-MCA(data,ncp=10,graph=TRUE)

After I run the code, I got the following error message.

Error in dimnames(res) <- list(attributes(tab)$row.names, listModa) : length of 'dimnames' [2] not equal to array extent

I am wondering why this error occurs and how to fix it. There is no missing data in my table and all of the variables are categorical.

If anyone has encountered similar problems and would love to help, I would really appreciate it. Thanks a lot.

like image 796
Jingjing Ni Avatar asked Nov 03 '15 20:11

Jingjing Ni


People also ask

What is MCA in R?

The Multiple correspondence analysis (MCA) is an extension of the simple correspondence analysis (chapter @ref(correspondence-analysis)) for summarizing and visualizing a data table containing more than two categorical variables.

What is MCA in data?

In statistics, multiple correspondence analysis (MCA) is a data analysis technique for nominal categorical data, used to detect and represent underlying structures in a data set. It does this by representing data as points in a low-dimensional Euclidean space.

What is the difference between PCA and MCA?

Put in very simple terms, Multiple Correspondence Analysis (MCA) is to qualitative data, as Principal Component Analysis (PCA) is to quantitative data.


1 Answers

I have had this error before because the function requires the variables to be factors (and the data I was passing it wasn't fully converted into factors). Unlike a lot of other R functions, this one does not convert the data for you even if all columns are categorical.

I'm not quite sure what your data is, but it is likely that one or more columns is not as a factor variable. If your columns 1 to 8 are already factors then it may be in the read.csv call; string variables will automatically be converted into a factors when you read them in from the csv, but numeric ones will not.

like image 100
KRS Avatar answered Sep 29 '22 02:09

KRS