{"cells":[
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "##PCA with Expedia destinations\n##load destinations.cvs data\ndest <- read.csv(\"../input/destinations.csv\", header = T, sep=\",\", stringsAsFactors = F)\nhead(dest, 10)\napply(dest, 2, mean)\napply(dest, 2, var)\ndim(dest)\n\nrequire(leaps)\n\n##I took little random sample for easy emplementation\nset.seed(12345678)\nmysam1  <- dest[sample(1:nrow(dest), 50, replace= FALSE),]\nsummary(mysam1)\n## 1 forward\nforward = regsubsets(d1~., mysam1, method =\"forward\")\nsummary(forward)\n## its get me d35, d52,d57, d62, d89, d119, d145 forsubset to use for PCA\nmysam2 <- mysam1[, c(36,53,58, 63, 90, 120, 146)]\nmysam2\nsummary(mysam2)\npredict(pca_dest1, \n        newdata=tail(dest, 10))\nlibrary(devtools)\ninstall_github(\"ggbiplot\", \"vqv\")\ninstall.packages(\"vqv-ggbiplot-2623d7c.tar.gz\", repos=NULL, type=\"source\")\nlibrary(ggbiplot)\ninstall_github(\"ggbiplot\", \"vqv\")\ng <- ggbiplot(pca_dest1, obs.scale = 1, var.scale = 1, \n               ellipse = TRUE, \n              circle = TRUE)\ng <- g + scale_color_discrete(name = '')\ng <- g + theme(legend.direction = 'horizontal', \n               legend.position = 'top')\nprint(g)\n\n## PCA with forward\npcaex1.out=prcomp(mysam2, scale=TRUE)\npcaex1.out\nhist(pcaex1.out$rotation)\n\npca_dest1 <- princomp(mysam2, cor=TRUE)\nsummary(pca_dest1)\nloadings(pca_dest1) # pc loadings \nplot(pca_dest1,type=\"lines\") # scree plot \npca_dest1$scores # the principal components\nbiplot(pca_dest1, cex = .5)\n\n## 2 backward\nbackward = regsubsets( d1~., mysam1,nbest = 1, nvmax = 8, method =\"backward\")\nsummary(backward)\n\n## backward selection gave me d12, d13,d18, d22, d24, d33, d36  forsubset to use for PCA\nmysam3 <- mysam1[, c(13, 14, 19, 23, 25, 34, 37)]\nmysam3\nsummary(mysam3)\n## PCA\n\npcaex2.out=prcomp(mysam3, scale=TRUE)\npcaex2.out\nhist(pcaex2.out$rotation)\n\npca_dest2 <- princomp(mysam3, cor=TRUE)\nsummary(pca_dest2)\nloadings(pca_dest2) # pc loadings \nplot(pca_dest2,type=\"lines\") # scree plot \npca_dest2$scores # the principal components\nbiplot(pca_dest2, cex = .5)\n\n## All big data set PCA\npca_dest <- princomp(dest, cor=TRUE)\npca_dest00 <- princomp(dest, cor=TRUE, scores = T)\nsummary(pca_dest)\nloadings(pca_dest)             # pc loadings \npca_dest$sd^2                  # component variances\nscreeplot(pca_dest)               # if you would like a scree plot\nplot(pca_dest,type=\"lines\")    # scree plot in lines\npca_dest$scores             # the principal components\n##biplot(pca_dest)  is to big to emplement but possible\nbiplot(pca_dest$sdev, pca_dest$scale)\nscreeplot(pca_dest)\n \npca.m4 <- princomp(~d98,\n                   cor=TRUE, data=dest, scores = TRUE)\nunclass(loadings(pca.m3))  # component loadings\nunclass(loadings(pca.m4))\npca.m3$sd^2  # component variances\nsummary(pca.m3) # proportions of variance\nscreeplot(pca.m3) # if you would like a scree plot\n\n##extract the component scores\nfirst.component.scores <- pca_dest$scores[,1]\nsummary(first.component.scores)\nlength(first.component.scores)\n\nstr(pca_dest)  ## show the structure\n\nsecond.component.scores <- pca_dest$scores[,2]\nsummary(second.component.scores)\nlength(second.component.scores)\n\n# Create a correlation matrix (also called a matrix of association) object \n# of the items for PCA.\nlibrary(psych)\ncor.matrix.1 <- cor(dest)\ncor.matrix.1\n\n# Script for PCA with VARIMAX rotation (an orthogonal rotation strategy).\n\npca.2 <- principal(r = cor.matrix.1, nfactors = 150, residuals = FALSE, rotate = \"varimax\")\npca.2\npca.varimax <- principal(r = cor.matrix.1, nfactors = 10, residuals = FALSE, rotate = \"varimax\")\npca.varimax  ## for 10 n factors\n\nlibrary(GPArotation)\n# Script for PCA with Direct Oblimin rotation (an oblique rotation strategy).\n\npca.3 <- principal(r = cor.matrix.1, nfactors = 10, residuals = FALSE, rotate = \"oblimin\")\npca.3\n\n##Cor matix with forward dataset\ncor.matrix.2 <- cor(mysam1[,c(\"d35\", \"d52\",\"d57\", \"d62\", \"d89\", \"d119\", \"d145\")], method = \"spearman\")\ncor.matrix.2\n# Predict PCs and ggbiplot\npredict(pca_dest, \n        newdata=tail(dest, 10))\nlibrary(devtools)\ninstall_github(\"ggbiplot\", \"vqv\")\ninstall.packages(\"vqv-ggbiplot-2623d7c.tar.gz\", repos=NULL, type=\"source\")\n\nlibrary(ggbiplot)\ninstall_github(\"ggbiplot\", \"vqv\")\ngd <- ggbiplot(pca_dest, obs.scale = 1, var.scale = 1, \n               ellipse = TRUE, \n              circle = TRUE)\ngd <- gd + scale_color_discrete(name = '')\ngd <- gd + theme(legend.direction = 'horizontal', \n               legend.position = 'top')\nprint(gd)\n"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": ""
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": ""
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": ""
 }
],"metadata":{"kernelspec":{"display_name":"R","language":"R","name":"ir"}}, "nbformat": 4, "nbformat_minor": 0}