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?
Check out the
dudi.mix() function in the
ade4 package: Ordination of Tables mixing quantitative variables and factors. Example:
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.
Make your text values nominal variables using
factor or ordinal variables using
ordered e.g. if you have a data frame called
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
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
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.
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?