# Run the model with parameter of 3 groups
runkmCluster(data[,2:5], 3)
# Add results to original data
clusters <- as.data.frame(runkmCluster(data[,2:5], 3)$cluster)
clusters$state <- data$State
data <- merge(data, clusters, by.x = "State", by.y = "state")
colnames(data) <- c("State", "Total Population",
"Median Household Income",
"Average Home Value",
"Bachelors Degrees",
"Cluster")
## K-means clustering with 3 clusters of sizes 18, 29, 4
##
## Cluster means:
## Total.Population Median.Household.Income Avg.Home.Value Bachelors.Degree
## 1 710222.2 62909.61 217288.9 5600408
## 2 202896.6 58961.83 216424.1 1562462
## 3 1855000.0 62347.00 284150.0 18081828
##
## Clustering vector:
## [1] 2 2 1 2 3 1 2 2 2 3 1 2 2 1 1 2 2 2 2 2 1 1 1 1 2 1 2 2 2 2 1 2 3 1 2
## [36] 1 2 2 1 2 2 2 1 3 2 2 1 2 1 2 1
##
## Within cluster sum of squares by cluster:
## [1] 5.059003e+13 2.687410e+13 9.924929e+13
## (between_SS / total_SS = 85.2 %)
##
## Available components:
##
## [1] "cluster" "centers" "totss" "withinss"
## [5] "tot.withinss" "betweenss" "size" "iter"
## [9] "ifault"