Advertisement
Guest User

Untitled

a guest
Oct 21st, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. library(data.table)
  2. library(ggplot2)
  3.  
  4. Demographics <- fread("demographics.csv")
  5.  
  6. #Cluster states on demographic
  7. StatePredictors <- Demographics[,-(1:2),with=FALSE]
  8. length(names(Demographics))
  9. CityPredictorsIncome <- Demographics[,c((28-10):26)]
  10. CityPredictorsPopulation <- Demographics[,c(4:16)]
  11. #Normalise to proportions in each age group
  12. CityPredictorsPopulation <- CityPredictorsPopulation/Demographics$population
  13. PCAPopulation <- prcomp(CityPredictorsPopulation,center = T)
  14. PCAIncome <- prcomp(CityPredictorsIncome,center = T,scale. = T)
  15.  
  16. #Proportion of variance explained by components
  17. plot(PCAPopulation$sdev/sum(PCAPopulation$sdev))
  18. PCAPopulation$rotation[,1] #First component has >45 year olds
  19. PCAPopulation$rotation[,2] #Second component has <45
  20. plot(PCAPopulation$x[,1],PCAPopulation$x[,2])
  21. PCAKMeansClust <- kmeans(PCAPopulation$x[,1:2],4)
  22. plot(PCAPopulation$x[,1],PCAPopulation$x[,2])
  23. points(PCAKMeansClust$centers,col="red")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement