Week 4 assignment - Machine Learning for Data
LIBNAME mydata "/courses/d1406ae5ba27fe300 " access=readonly;
************************************************************************************************************** DATA MANAGEMENT **************************************************************************************************************; data clust; set mydata.gapminder; run; * create a unique identifier to merge cluster assignment variable with the main data set;
idnum=_n_; keep idnum incomeperperson alcconsumption armforcesrate hivrate internetuserate urbanrate employrate suicideper100th lifeexpectancy femaleemployrate;
run;
ods graphics on;
* Split data randomly into test and training data; proc surveyselect data=clust out=traintest seed = 145 samprate=0.8 method=srs outall; run;
data clus_train; set traintest; if selected=1; run; data clus_test; set traintest; if selected=0; run; * standardize the clustering variables to have a mean of 0 and standard deviation of 1;
proc standard data=clus_train out=clustvar mean=0 std=1;
var incomeperperson alcconsumption hivrate internetuserate urbanrate employrate suicideper100th lifeexpectancy femaleemployrate; run; %macro kmean(K);
proc fastclus data=clustvar out=outdata&K. outstat=cluststat&K. maxclusters= &K. maxiter=30000;
var incomeperperson alcconsumption hivrate internetuserate urbanrate employrate suicideper100th lifeexpectancy femaleemployrate; run;
%mend;
%kmean(1); %kmean(2); %kmean(3); %kmean(4); %kmean(5); %kmean(6); %kmean(7); %kmean(8); %kmean(9); * extract r-square values from each cluster solution and then merge them to plot elbow curve; data clus1; set cluststat1; nclust=1;
if _type_='RSQ';
keep nclust over_all; run;
data clus2; set cluststat2; nclust=2;
if _type_='RSQ';
keep nclust over_all; run;
data clus3; set cluststat3; nclust=3;
if _type_='RSQ';
keep nclust over_all; run;
data clus4; set cluststat4; nclust=4;
if _type_='RSQ';
keep nclust over_all; run; data clus5; set cluststat5; nclust=5;
if _type_='RSQ';
keep nclust over_all; run; data clus6; set cluststat6; nclust=6;
if _type_='RSQ';
keep nclust over_all; run; data clus7; set cluststat7; nclust=7;
if _type_='RSQ';
keep nclust over_all; run; data clus8; set cluststat8; nclust=8;
if _type_='RSQ';
keep nclust over_all; run; data clus9; set cluststat9; nclust=9;
if _type_='RSQ';
keep nclust over_all; run;
data clusrsquare; set clus1 clus2 clus3 clus4 clus5 clus6 clus7 clus8 clus9; run;
* plot elbow curve using r-square values; symbol1 color=blue interpol=join; proc gplot data=clusrsquare; plot over_all*nclust; run;
A k-means cluster analysis was conducted to identify underlying subgroups of people's income based on their similarity of responses on 9 variables that represent characteristics that could have an impact on people's daily life in different countries. Clustering variables included quantitative variables measuring alcohol consumption, a scales measuring HIV rate, internet use rate, life expectancy, urban rate, employ rate, people who suicide and female employ rate. All clustering variables were standardized to have a mean of 0 and the standard deviation of 1.
Data were randomly split into a training set that included 80% of the observations (N= 171) and a test set that included 20% of the observations.A series of k-means cluster analyses were conducted on the training data specifying k=1-9 clusters, using Euclidean distance. The variance in the clustering variables that was accounted for by the clusters (r-square) was plotted for each of the nine cluster solutions in an elbow curve to provide guidance for choosing the number of clusters to interpret.
In the figure below, the plot shows the increase in the proportion advantage in the clustering variables, explained by each of the cluster solutions. The two cluster solution accounts for about 28% or 29% of the variance. The R-square value increases as more clusters are specified












