diff --git a/Documentation/Live Training Seminar Materials - Logistic Regression.pdf b/Documentation/Live Training Seminar Materials - Logistic Regression.pdf new file mode 100644 index 0000000..37eb2a4 Binary files /dev/null and b/Documentation/Live Training Seminar Materials - Logistic Regression.pdf differ diff --git a/LogisticRegression.aprx b/LogisticRegression.aprx new file mode 100644 index 0000000..8ee3afe Binary files /dev/null and b/LogisticRegression.aprx differ diff --git a/R-ArcGIS Bridge Live Training Seminar.tbx b/R-ArcGIS Bridge Live Training Seminar.tbx new file mode 100644 index 0000000..5b979f7 Binary files /dev/null and b/R-ArcGIS Bridge Live Training Seminar.tbx differ diff --git a/Seagrass.gdb/a00000001.TablesByName.atx b/Seagrass.gdb/a00000001.TablesByName.atx new file mode 100644 index 0000000..41db6d1 Binary files /dev/null and b/Seagrass.gdb/a00000001.TablesByName.atx differ diff --git a/Seagrass.gdb/a00000001.gdbindexes b/Seagrass.gdb/a00000001.gdbindexes new file mode 100644 index 0000000..b02aa75 Binary files /dev/null and b/Seagrass.gdb/a00000001.gdbindexes differ diff --git a/Seagrass.gdb/a00000001.gdbtable b/Seagrass.gdb/a00000001.gdbtable new file mode 100644 index 0000000..ede59b0 Binary files /dev/null and b/Seagrass.gdb/a00000001.gdbtable differ diff --git a/Seagrass.gdb/a00000001.gdbtablx b/Seagrass.gdb/a00000001.gdbtablx new file mode 100644 index 0000000..3d6f920 Binary files /dev/null and b/Seagrass.gdb/a00000001.gdbtablx differ diff --git a/Seagrass.gdb/a00000002.gdbtable b/Seagrass.gdb/a00000002.gdbtable new file mode 100644 index 0000000..a0af90e Binary files /dev/null and b/Seagrass.gdb/a00000002.gdbtable differ diff --git a/Seagrass.gdb/a00000002.gdbtablx b/Seagrass.gdb/a00000002.gdbtablx new file mode 100644 index 0000000..7c12c56 Binary files /dev/null and b/Seagrass.gdb/a00000002.gdbtablx differ diff --git a/Seagrass.gdb/a00000003.gdbindexes b/Seagrass.gdb/a00000003.gdbindexes new file mode 100644 index 0000000..58df68d Binary files /dev/null and b/Seagrass.gdb/a00000003.gdbindexes differ diff --git a/Seagrass.gdb/a00000003.gdbtable b/Seagrass.gdb/a00000003.gdbtable new file mode 100644 index 0000000..4a552d3 Binary files /dev/null and b/Seagrass.gdb/a00000003.gdbtable differ diff --git a/Seagrass.gdb/a00000003.gdbtablx b/Seagrass.gdb/a00000003.gdbtablx new file mode 100644 index 0000000..2f80ed4 Binary files /dev/null and b/Seagrass.gdb/a00000003.gdbtablx differ diff --git a/Seagrass.gdb/a00000004.CatItemsByPhysicalName.atx b/Seagrass.gdb/a00000004.CatItemsByPhysicalName.atx new file mode 100644 index 0000000..df552c7 Binary files /dev/null and b/Seagrass.gdb/a00000004.CatItemsByPhysicalName.atx differ diff --git a/Seagrass.gdb/a00000004.CatItemsByType.atx b/Seagrass.gdb/a00000004.CatItemsByType.atx new file mode 100644 index 0000000..44905b1 Binary files /dev/null and b/Seagrass.gdb/a00000004.CatItemsByType.atx differ diff --git a/Seagrass.gdb/a00000004.FDO_UUID.atx b/Seagrass.gdb/a00000004.FDO_UUID.atx new file mode 100644 index 0000000..8667d5d Binary files /dev/null and b/Seagrass.gdb/a00000004.FDO_UUID.atx differ diff --git a/Seagrass.gdb/a00000004.gdbindexes b/Seagrass.gdb/a00000004.gdbindexes new file mode 100644 index 0000000..a4f334d Binary files /dev/null and b/Seagrass.gdb/a00000004.gdbindexes differ diff --git a/Seagrass.gdb/a00000004.gdbtable b/Seagrass.gdb/a00000004.gdbtable new file mode 100644 index 0000000..565d799 Binary files /dev/null and b/Seagrass.gdb/a00000004.gdbtable differ diff --git a/Seagrass.gdb/a00000004.gdbtablx b/Seagrass.gdb/a00000004.gdbtablx new file mode 100644 index 0000000..48521ed Binary files /dev/null and b/Seagrass.gdb/a00000004.gdbtablx differ diff --git a/Seagrass.gdb/a00000004.spx b/Seagrass.gdb/a00000004.spx new file mode 100644 index 0000000..6d236bd Binary files /dev/null and b/Seagrass.gdb/a00000004.spx differ diff --git a/Seagrass.gdb/a00000005.CatItemTypesByName.atx b/Seagrass.gdb/a00000005.CatItemTypesByName.atx new file mode 100644 index 0000000..dd8d428 Binary files /dev/null and b/Seagrass.gdb/a00000005.CatItemTypesByName.atx differ diff --git a/Seagrass.gdb/a00000005.CatItemTypesByParentTypeID.atx b/Seagrass.gdb/a00000005.CatItemTypesByParentTypeID.atx new file mode 100644 index 0000000..e653f4a Binary files /dev/null and b/Seagrass.gdb/a00000005.CatItemTypesByParentTypeID.atx differ diff --git a/Seagrass.gdb/a00000005.CatItemTypesByUUID.atx b/Seagrass.gdb/a00000005.CatItemTypesByUUID.atx new file mode 100644 index 0000000..2d41c1d Binary files /dev/null and b/Seagrass.gdb/a00000005.CatItemTypesByUUID.atx differ diff --git a/Seagrass.gdb/a00000005.gdbindexes b/Seagrass.gdb/a00000005.gdbindexes new file mode 100644 index 0000000..bc88709 Binary files /dev/null and b/Seagrass.gdb/a00000005.gdbindexes differ diff --git a/Seagrass.gdb/a00000005.gdbtable b/Seagrass.gdb/a00000005.gdbtable new file mode 100644 index 0000000..d14ebf5 Binary files /dev/null and b/Seagrass.gdb/a00000005.gdbtable differ diff --git a/Seagrass.gdb/a00000005.gdbtablx b/Seagrass.gdb/a00000005.gdbtablx new file mode 100644 index 0000000..4f47897 Binary files /dev/null and b/Seagrass.gdb/a00000005.gdbtablx differ diff --git a/Seagrass.gdb/a00000006.CatRelsByDestinationID.atx b/Seagrass.gdb/a00000006.CatRelsByDestinationID.atx new file mode 100644 index 0000000..c0004e5 Binary files /dev/null and b/Seagrass.gdb/a00000006.CatRelsByDestinationID.atx differ diff --git a/Seagrass.gdb/a00000006.CatRelsByOriginID.atx b/Seagrass.gdb/a00000006.CatRelsByOriginID.atx new file mode 100644 index 0000000..9fa27dd Binary files /dev/null and b/Seagrass.gdb/a00000006.CatRelsByOriginID.atx differ diff --git a/Seagrass.gdb/a00000006.CatRelsByType.atx b/Seagrass.gdb/a00000006.CatRelsByType.atx new file mode 100644 index 0000000..5143e30 Binary files /dev/null and b/Seagrass.gdb/a00000006.CatRelsByType.atx differ diff --git a/Seagrass.gdb/a00000006.FDO_UUID.atx b/Seagrass.gdb/a00000006.FDO_UUID.atx new file mode 100644 index 0000000..3f6a24c Binary files /dev/null and b/Seagrass.gdb/a00000006.FDO_UUID.atx differ diff --git a/Seagrass.gdb/a00000006.gdbindexes b/Seagrass.gdb/a00000006.gdbindexes new file mode 100644 index 0000000..c608a88 Binary files /dev/null and b/Seagrass.gdb/a00000006.gdbindexes differ diff --git a/Seagrass.gdb/a00000006.gdbtable b/Seagrass.gdb/a00000006.gdbtable new file mode 100644 index 0000000..7a750d0 Binary files /dev/null and b/Seagrass.gdb/a00000006.gdbtable differ diff --git a/Seagrass.gdb/a00000006.gdbtablx b/Seagrass.gdb/a00000006.gdbtablx new file mode 100644 index 0000000..431307d Binary files /dev/null and b/Seagrass.gdb/a00000006.gdbtablx differ diff --git a/Seagrass.gdb/a00000007.CatRelTypesByBackwardLabel.atx b/Seagrass.gdb/a00000007.CatRelTypesByBackwardLabel.atx new file mode 100644 index 0000000..668f82b Binary files /dev/null and b/Seagrass.gdb/a00000007.CatRelTypesByBackwardLabel.atx differ diff --git a/Seagrass.gdb/a00000007.CatRelTypesByDestItemTypeID.atx b/Seagrass.gdb/a00000007.CatRelTypesByDestItemTypeID.atx new file mode 100644 index 0000000..b832954 Binary files /dev/null and b/Seagrass.gdb/a00000007.CatRelTypesByDestItemTypeID.atx differ diff --git a/Seagrass.gdb/a00000007.CatRelTypesByForwardLabel.atx b/Seagrass.gdb/a00000007.CatRelTypesByForwardLabel.atx new file mode 100644 index 0000000..4895142 Binary files /dev/null and b/Seagrass.gdb/a00000007.CatRelTypesByForwardLabel.atx differ diff --git a/Seagrass.gdb/a00000007.CatRelTypesByName.atx b/Seagrass.gdb/a00000007.CatRelTypesByName.atx new file mode 100644 index 0000000..eb16a9e Binary files /dev/null and b/Seagrass.gdb/a00000007.CatRelTypesByName.atx differ diff --git a/Seagrass.gdb/a00000007.CatRelTypesByOriginItemTypeID.atx b/Seagrass.gdb/a00000007.CatRelTypesByOriginItemTypeID.atx new file mode 100644 index 0000000..cc8d8c4 Binary files /dev/null and b/Seagrass.gdb/a00000007.CatRelTypesByOriginItemTypeID.atx differ diff --git a/Seagrass.gdb/a00000007.CatRelTypesByUUID.atx b/Seagrass.gdb/a00000007.CatRelTypesByUUID.atx new file mode 100644 index 0000000..7c0c3dc Binary files /dev/null and b/Seagrass.gdb/a00000007.CatRelTypesByUUID.atx differ diff --git a/Seagrass.gdb/a00000007.gdbindexes b/Seagrass.gdb/a00000007.gdbindexes new file mode 100644 index 0000000..2a98c93 Binary files /dev/null and b/Seagrass.gdb/a00000007.gdbindexes differ diff --git a/Seagrass.gdb/a00000007.gdbtable b/Seagrass.gdb/a00000007.gdbtable new file mode 100644 index 0000000..7a8d61c Binary files /dev/null and b/Seagrass.gdb/a00000007.gdbtable differ diff --git a/Seagrass.gdb/a00000007.gdbtablx b/Seagrass.gdb/a00000007.gdbtablx new file mode 100644 index 0000000..342963b Binary files /dev/null and b/Seagrass.gdb/a00000007.gdbtablx differ diff --git a/Seagrass.gdb/a00000009.gdbindexes b/Seagrass.gdb/a00000009.gdbindexes new file mode 100644 index 0000000..c9d0caa Binary files /dev/null and b/Seagrass.gdb/a00000009.gdbindexes differ diff --git a/Seagrass.gdb/a00000009.gdbtable b/Seagrass.gdb/a00000009.gdbtable new file mode 100644 index 0000000..18b30f1 Binary files /dev/null and b/Seagrass.gdb/a00000009.gdbtable differ diff --git a/Seagrass.gdb/a00000009.gdbtablx b/Seagrass.gdb/a00000009.gdbtablx new file mode 100644 index 0000000..418e7e9 Binary files /dev/null and b/Seagrass.gdb/a00000009.gdbtablx differ diff --git a/Seagrass.gdb/a00000009.spx b/Seagrass.gdb/a00000009.spx new file mode 100644 index 0000000..d7320a4 Binary files /dev/null and b/Seagrass.gdb/a00000009.spx differ diff --git a/Seagrass.gdb/gdb b/Seagrass.gdb/gdb new file mode 100644 index 0000000..a786e12 Binary files /dev/null and b/Seagrass.gdb/gdb differ diff --git a/Seagrass.gdb/timestamps b/Seagrass.gdb/timestamps new file mode 100644 index 0000000..b11cfe9 Binary files /dev/null and b/Seagrass.gdb/timestamps differ diff --git a/scripts/Logistic_Regression_Wrappings.R b/scripts/Logistic_Regression_Wrappings.R new file mode 100644 index 0000000..4851c1d --- /dev/null +++ b/scripts/Logistic_Regression_Wrappings.R @@ -0,0 +1,144 @@ +#Live Training Seminar: Go Deeper with Data Analytics Using ArcGIS Pro and R +#Logistic Regression R Script Wrappings +tool_exec<- function(in_params, out_params){ + + ##################################################################################################### + ### Check/Load Required Packages + ##################################################################################################### + arc.progress_label("Loading packages...") + arc.progress_pos(20) + + if(!requireNamespace("MKmisc", quietly = TRUE)) + install.packages("MKmisc", quiet = TRUE) + if(!requireNamespace("ROCR", quietly = TRUE)) + install.packages("ROCR", quiet = TRUE) + if(!requireNamespace("survey", quietly = TRUE)) + install.packages("survey", quiet = TRUE) + if(!requireNamespace("pROC", quietly = TRUE)) + install.packages("pROC", quiet = TRUE) + if(!requireNamespace("ROCR", quietly = TRUE)) + install.packages("ROCR", quiet = TRUE) + if(!requireNamespace("caret", quietly = TRUE)) + install.packages("caret", quiet = TRUE) + + require(MKmisc) + require(ROCR) + require(survey) + require(pROC) + require(ROCR) + require(caret) + + ##################################################################################################### + ### Define input/output parameters + ##################################################################################################### + input_data <- in_params[[1]] + train_percentage_size <- (in_params[[2]])/100 + dependent_variable <- in_params[[3]] + independent_variables <- in_params[[4]] + + output_prediction_data <- out_params[[1]] + + ##################################################################################################### + ### Load Data and Create Dataframe R Object + ##################################################################################################### + arc.progress_label("Loading data...") + arc.progress_pos(40) + + d <- arc.open(input_data) + fields_list <- append(c(dependent_variable), independent_variables) + d_df_full <- arc.select(d) + d_df <- arc.select(d, fields = fields_list) + + ##################################################################################################### + ### Create Training and Testing Datasets + ##################################################################################################### + arc.progress_label("Creating training and testing datasets...") + arc.progress_pos(60) + smp_size <- floor(train_percentage_size * nrow(d_df)) + + set.seed(1234) + train_ind <- sample(seq_len(nrow(d_df)), size = smp_size) + + d_df_train <- d_df[train_ind, ] + d_df_test <- d_df[-train_ind, ] + + ##################################################################################################### + ### Fit Logistic Regression Model + ##################################################################################################### + arc.progress_label("Creating training and testing datasets...") + arc.progress_pos(80) + + response <- d_df_train[, 1] + predictors <- d_df_train[, -1] + + d_df_train.log <- glm(response ~ ., family = binomial(link = 'logit'), data = predictors) + + d_df_full$Seagrass_Prediction <- predict(d_df_train.log, newdata = d_df_full, type = "response") + + ##################################################################################################### + ### Run Diagnostics on Logistic Regression Model + ##################################################################################################### + arc.progress_label("Running diagnostics on fitted model...") + arc.progress_pos(80) + + #Summary of model fit + cat(paste0("\n", "............................................", "\n")) + cat(paste0("\n", "............................................", "\n")) + cat(paste0("\n")) + cat(paste0("\n", "Summary of Fitted Logistic Regression Model", "\n")) + cat(paste0("\n", "............................................", "\n")) + cat(paste0("\n")) + print(summary(d_df_train.log)) + + #Hosmer-Lemeshow Test + cat(paste0("\n", "............................................", "\n")) + cat(paste0("\n", "............................................", "\n")) + cat(paste0("\n")) + cat(paste0("\n", "Hosmer-Lemeshow Goodness of Fit Test Results", "\n")) + cat(paste0("\n", "............................................", "\n")) + cat(paste0("\n")) + HL <- HLgof.test(fit = fitted(d_df_train.log), obs = d_df_train$Seagrass) + print(HL) + + #Prediction Accuracy + d_df_test.log.pred <- predict(d_df_train.log, newdata = d_df_test, type = 'response') + d_df_test.log.pred <- ifelse(d_df_test.log.pred > 0.5, 1, 0) + misClassificError <- mean(d_df_test.log.pred != d_df_test$Seagrass) + cat(paste0("\n", "............................................", "\n")) + cat(paste0("\n", "............................................", "\n")) + cat(paste0("\n")) + cat(paste0("\n", "Prediction Accuracy for Test Dataset", "\n")) + cat(paste0("\n", "............................................", "\n")) + cat(paste0("\n")) + print(paste('Accuracy Percentage:', round((1-misClassificError)*100, 2))) + cat(paste0("\n")) + + #ROC Curve + d_df_test.log.pred <- predict(d_df_train.log, newdata = d_df_test, type = 'response') + pred <- prediction(d_df_test.log.pred, d_df_test$Seagrass) + perf <- performance(pred, measure = "tpr", x.measure = "fpr") + plot(perf) + auc <- performance(pred, measure = "auc") + auc <- auc@y.values[[1]] + cat(paste0("\n", "............................................", "\n")) + cat(paste0("\n", "............................................", "\n")) + cat(paste0("\n")) + cat(paste0("\n", "Area Under the ROC Curve", "\n")) + cat(paste0("\n", "............................................", "\n")) + cat(paste0("\n")) + print(paste('Area:', auc)) + cat(paste0("\n")) + cat(paste0("\n", "............................................", "\n")) + cat(paste0("\n")) + + ##################################################################################################### + ### Write Output + ##################################################################################################### + arc.progress_label("Writing output...") + arc.progress_pos(80) + + if(!is.null(output_prediction_data) && output_prediction_data != "NA") + arc.write(output_prediction_data, d_df_full, shape_info = arc.shapeinfo(d)) + + arc.progress_pos(100) +}