5
$\begingroup$

I'm relatively new to R and am working with a very large dataset that has a mix of numerical scores (for instance, household income) as well as text values (i.e. race). I was planning on using PCA to analyze this dataset but it only works on numerical data. Is there any good alternative?

$\endgroup$
1

2 Answers 2

5
$\begingroup$

Check out the dudi.mix() function in the ade4 package: Ordination of Tables mixing quantitative variables and factors. Example:

library(ade4)
scatter.dudi(dudi.mix(iris,scannf=FALSE))

There are a couple other packages that do mixed correspondence analysis.

You can go ahead and fully dummy code your categorical variables too. It's not as theoretically sound, but it does get the job done.

$\endgroup$
1
  • $\begingroup$ I did forget to mention the other option, which is to approach it ass backwards. Convert your continuous variables into less than full rank b-spline basis (that means encode the intercept; also means the resulting basis functions will sum to one) and perform Multiple Correspondence Analysis. That would even handle non-linearity. $\endgroup$ Commented Aug 3, 2012 at 19:26
2
$\begingroup$

Make variables

Make your text values nominal variables using factor or ordinal variables using ordered e.g. if you have a data frame called mydata then

mydata <- transform(race=factor(race))

will convert a textual race variable to a nominal one. See ?transform for details.

This way R will show you labels but construct the appropriate dummy variables in the background for you when you fit any models. You can change the baseline category in the default contrast coding using relevel.

If you used read.csv to get your data in, this may have already happened without your requesting it. You can check the classes of each variable using

lapply(mydata, class)

Things that are factors list their levels. Indeed half the battle with R is to persuade not to do these sorts of 'handy' variable conversions.

Scale variables?

If you are thinking of PCA then you are, I suppose, trying to scale this data somehow. If this is the case you might use a factor analysis approach that can deal with mixed variable types. One such method is MCMCmixfactanal in the MCMCpack R package.

On the other hand you might ask yourself what space you would get out of a big scaling model that contains so much disparate stuff. Perhaps a wiser plan would be to keep the nominal variables as things to condition on / stratify with, in the context of fitting a regular multivariate model.

If you'd like to elaborate on the purpose of your PCA plan we might be able to provide some more alternatives. What information are you trying to get out of the data by scaling?

$\endgroup$
1
  • $\begingroup$ Thanks, I didn't know of MCMCmixfactanal. $\endgroup$
    – chl
    Commented Jul 7, 2012 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.