{"cells":[{"metadata":{"_uuid":"32e477d6-d419-4d35-9815-ad7ffef582d5","_cell_guid":"64ab0a8d-e5e1-46cb-9f20-37c0b37f4a57","trusted":true},"cell_type":"code","source":"# Install packages (may take a few minutes)\n\ninstall.packages(\"psych\")\ninstall.packages(\"GPArotation\")\ninstall.packages(\"fastDummies\")\ninstall.packages(\"factoextra\")\ninstall.packages(\"dplyr\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4e60326d-52c8-49ae-8f93-5cf1e89eb39f","_cell_guid":"9bd82d5c-b4a4-4cf0-b876-140dc3fa7f4f","trusted":true},"cell_type":"code","source":"# Load libraries\n\nlibrary(factoextra)\nlibrary(fastDummies)\nlibrary(psych)\nlibrary(GPArotation)\nlibrary(datasets)\nlibrary(readr)\nlibrary(data.table)\nlibrary(dplyr)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a5a5a8d8-bf96-4cae-a33d-bf278065c42c","_cell_guid":"261ba851-b187-4dec-9d87-8894bd83edd3","trusted":true},"cell_type":"code","source":"# Read the file (TYPE IN THE FILE PATH)\n\ndata <- read.csv('../input/titanic/train.csv')\n\nhead(data)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"95430f41-1044-48b7-957a-4cc7dab173e7","_cell_guid":"b43ae8c9-6fea-49b0-98c6-0cc31a590179","trusted":true},"cell_type":"code","source":"# Drop columns (TYPE IN THE COL NO.)\n\ndata <- data[-c(1,4,6,9,11)]\n\nhead(data)\n\nsummary(data)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a83eccd4-1bcc-47c1-910c-e6b58332709b","_cell_guid":"4f314646-ef3c-45f4-9250-63177623ebc1","trusted":true},"cell_type":"code","source":"# Omit rows with any missing values (if needed)\n\ndata <- na.omit(data)\n\nhead(data)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c01ac909-8335-499a-aebf-6207de78da8d","_cell_guid":"abc504aa-2a40-45ed-b97f-3fe04224ffc1","trusted":true},"cell_type":"code","source":"# Encode categorical variables (if needed)\n\ndata <- dummy_cols(data, remove_most_frequent_dummy = TRUE, remove_selected_columns = TRUE)\n\nhead(data)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f892057c-bc41-4fd1-8b97-1c063e00eacb","_cell_guid":"37d55bbf-fe0c-46e5-85ef-0fa9ff3e6a11","trusted":true},"cell_type":"code","source":"# Scale the data\n\ndata <- scale(data)\n\nhead(data)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"91bfa527-e838-4506-a156-3189d6e10768","_cell_guid":"d2ec2c0a-8eea-4994-b124-047fbf617987","trusted":true},"cell_type":"markdown","source":"# Data adequacy tests","execution_count":null},{"metadata":{"_uuid":"580ea3ec-8595-4eca-95aa-3728a5f1cce7","_cell_guid":"ec6ccbe7-8f97-4a1a-a1a7-8cf901bd08fc","trusted":true},"cell_type":"code","source":"# Kaiser-Meyer-Olkin Measure of Sampling Adequacy\n\nKMO(data)\n\n# Bartlett’s Test of Sphericity\n\ncortest.bartlett(data, n=NULL, diag=TRUE)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Parallel Analysis","execution_count":null},{"metadata":{"_uuid":"2024985f-5ddb-409f-b7e3-691404e3aa6d","_cell_guid":"fed49f67-4dde-4942-905a-d305f047c2e5","trusted":true},"cell_type":"code","source":"# Parallel Analysis (TYPE IN: factoring method)\n\nx <- fa.parallel(data, fm=\"pa\", fa=\"both\", n.iter=1)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f2de8904-717a-41b9-b663-f6f457910ac3","_cell_guid":"4388adcc-35c2-41f5-87bb-43c9e3a08166","trusted":true},"cell_type":"markdown","source":"# PCA and factor analysis","execution_count":null},{"metadata":{"_uuid":"7f09258a-e2ab-49b9-90b7-c3d9ed971b19","_cell_guid":"1d709226-0d1c-4756-ab20-5d6891304b78","trusted":true},"cell_type":"code","source":"# Pricipal Components Analysis (TYPE IN: rotation method)\n\nfit <- principal(data, x$ncomp, rotate=\"varimax\") \n\nprint(fit$loadings, cutoff=.3)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b56e8803-56b5-4dbd-8e1a-536d90ea46f9","_cell_guid":"ee38d37a-b0eb-45e0-a73c-923c8c9f2512","trusted":true},"cell_type":"code","source":"# To plot PCA\n\nfactor.plot(fit)\n\ncor.plot(fit)\n\nfa.diagram(fit)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"02c18c20-d8a3-4377-ae26-372c8e8928ca","_cell_guid":"5a6a1f4a-f4c1-4c9a-ad9f-0f47d2bb56b7","trusted":true},"cell_type":"code","source":"# Factor Analysis (TYPE IN: rotation method, factoring method)\n\nfit <- fa(data, x$nfact, rotate=\"promax\", fm=\"pa\")\n\nprint(fit$loadings, cutoff=.3)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ac948486-6bbb-4d7f-9591-4f366ec20e6d","_cell_guid":"944ccdfd-b14c-4058-9833-738b17a2f2fd","trusted":true},"cell_type":"code","source":"# To plot factor analysis\n\nfactor.plot(fit)\n\ncor.plot(fit)\n\nfa.diagram(fit)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Other helpful values\n\n# print(fit)\n\n# print(fit$values) # Eigen values\n\n# print(fit$communality) # Sum of squared factor loadings\n\n# print(fit$complexity) # Hoffman’s index of complexity\n\n# print(fit$Structure, cutoff=.3) # Structure matrix\n\n# print(fit$Phi) # Interfactor correlation matrix\n\n# print(fit$residual) # Residual matrix\n\n# print(fit$scores) # Factor scores\n\n# print(fit$weights) # Beta weights\n\n# print(fit$rot.mat) # Rotation matrix","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"* **Rotation options:**\n\nrotate=\"none\", \"varimax\", \"quartimax\", \"bentlerT\", \"equamax\", \"varimin\", \"geominT\" and \"bifactor\" are orthogonal rotations. \n\nrotate=\"Promax\", \"promax\", \"oblimin\", \"simplimax\", \"bentlerQ, \"geominQ\" and \"biquartimin\" and \"cluster\" are possible oblique transformations of the solution. \n\nThe default is to do a oblimin transformation, although versions prior to 2009 defaulted to varimax. \n\nSPSS seems to do a Kaiser normalization before doing Promax, this is done here by the call to \"promax\" which does the normalization before calling Promax in GPArotation.","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"* **Factoring methods:**\n\nfm=\"minres\" will do a minimum residual as will fm=\"uls\". Both of these use a first derivative. \n\nfm=\"ols\" differs very slightly from \"minres\" in that it minimizes the entire residual matrix using an OLS procedure but uses the empirical first derivative. This will be slower. \n\nfm=\"wls\" will do a weighted least squares (WLS) solution, fm=\"gls\" does a generalized weighted least squares (GLS), fm=\"pa\" will do the principal factor solution, fm=\"ml\" will do a maximum likelihood factor analysis. \n\nfm=\"minchi\" will minimize the sample size weighted chi square when treating pairwise correlations with different number of subjects per pair. \n\nfm =\"minrank\" will do a minimum rank factor analysis. \n\nfm = \"old.min\" will do minimal residual the way it was done prior to April, 2017 (see discussion below). \n\nfm=\"alpha\" will do alpha factor analysis as described in Kaiser and Coffey (1965). ","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"For more about rotation and factoring methods in psych package, \n\nplease go to: https://cran.r-project.org/web/packages/psych/psych.pdf","execution_count":null},{"metadata":{"_uuid":"7d5eda28-2f27-445b-b70d-a38f8bab72f7","_cell_guid":"329e662f-85a4-4824-a64e-bfda239a1916","trusted":true},"cell_type":"markdown","source":"# Begin cluster analysis (Extra)","execution_count":null},{"metadata":{"_uuid":"ca77bd8d-e906-42ee-ae30-5736dc49ac09","_cell_guid":"be1ebe6f-5514-4cfb-bb23-cebc5d553721","trusted":true},"cell_type":"code","source":"# Cluster Analysis (TYPE IN: clustering method, no. of clusters)\n\nfviz_nbclust(data, kmeans, method = \"wss\")\n\ncluster <- kmeans(data, 3, nstart = 24)\n\nfviz_cluster(cluster, data = data, ellipse.type = \"euclid\", star.plot = TRUE, repel = TRUE, ggtheme = theme_minimal())","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"name":"ir","display_name":"R","language":"R"},"language_info":{"name":"R","codemirror_mode":"r","pygments_lexer":"r","mimetype":"text/x-r-source","file_extension":".r","version":"3.6.3"}},"nbformat":4,"nbformat_minor":4}