# For W 11/8

# K-means Clustering

# First, we create the subset of variables for structure analysis

ANES2012ft1<-subset(ANES2012,select=ftgr_xfund:ftgr_feminists)

ANES2012ft2<-subset(ANES2012,select=ftgr_liberals:ftgr_working)

ANES2012ft3<-subset(ANES2012,select=ftgr_gay)

ANES2012ft4<-subset(ANES2012,select=ftgr_rich:ftgr_tea)

ANES2012ft<-cbind(ANES2012ft1,ANES2012ft2,ANES2012ft3,ANES2012ft4)

# Next, we compute the correlation matrix of those variables. This is the input

# for cluster analysis.

ANES2012cor<-cor(ANES2012ft,use="complete.obs",method="pearson")

kmeans(ANES2012cor,centers=2)

# Now, like with our other methods of latent structure analysis,

# we start looking for an optimal clustering solution. We begin

# with two clusters and start adding clusters until we get a good

# solution, with low within groups variation and good between

# groups variation, and, most importantly, interpretable

# clusters, based on membership. We increment the value of centers by 1

# and compare the results. Repeat until you find the optimal solution.

kmeans(ANES2012cor,centers=3)

# We want clusters that are not too big and not too small. We keep incrementing

# the number of clusters until we go one step too far, then back up to the previous

# solution.