diff --git a/.Rhistory b/.Rhistory index 6e3d130..0a29ded 100644 --- a/.Rhistory +++ b/.Rhistory @@ -1,70 +1,3 @@ -Sys.time() -py(model) -reticulate::py_call(model) -reticulate::py_call("model") -py_run_string("print(model)") -py_run_string("model") -py_run_string('print(model)') -?r_to_py -set.seed(89) -## Read data for analysis -cat_vars <- c("CC18_401","CC18_413d","educ","race","marstat","votereg","OScode","CompRating", -"region","pid7","immstat","employ","sexuality","trans","industryclass","pew_churatd") -bin_vars <- c("CC18_415a","CC18_417_a","gender") -cont_vars <- c("citylength_1","numchildren","birthyr") -data_0_test <- fread("data/cces_jss_format.csv", select = c(cat_vars, bin_vars, cont_vars)) -r_to_py(data_0_test) -py_run_string("print(data_0_test") -py_run_string("print(data_0_test)") -py_load_object(data_0_test) -py$data_0_test <- data_0_test -py_run_string("print(data_0_test") -py_run_string("print(data_0_test)") -cces <- read_csv("../linux/JSS/replication_materials/data/cces_jss_format.csv") -library(tidyverse) -cces <- read_csv("../linux/JSS/replication_materials/data/cces_jss_format.csv") -print(object.size(cces), "Mb") -print(object.size(cces),units = "Mb") -library(devtools) -check() -check_rhub() -build() -# Imputation workflow -library("rMIDAS") -install.packages("rMIDAS") -library(rMIDAS) -version(rMIDAS) -data(quakes) -force(quakes) -data(airquality) -x_conv <- convert(add_missingness(quakes, prop=0.1), -minmax_scale = TRUE) -x_train <- train(x_conv, -training_epochs = 20, -layer_structure = c(128,128), -input_drop = 0.75, -seed = 89) -force(airquality) -x_complete <- complete(x_train, m = 1) -?complete -complete2 <- function(mid_obj, -m=10L, -unscale = TRUE, -bin_label = TRUE, -cat_coalesce = TRUE, -fast = FALSE, -file = NULL, -file_root = NULL) { -if (!inherits(mid_obj,"midas_base.Midas")) { -stop("Trained midas object not supplied to 'mid_obj' argument") -} -if (is.null(options("python_initialised")$python_initialised)) { -python_init() -} -if (!("preproc" %in% names(mid_obj))) { -unscale = FALSE -bin_label = FALSE -cat_coalesce = FALSE } draws <- mid_obj$generate_samples(m = as.integer(m))$output_list if ((unscale || bin_label || cat_coalesce)) { @@ -510,3 +443,70 @@ library(devtools) install_github("MIDASverse/rMIDAS") library(rMIDAS) vignette(package = "rMIDAS") +library(devtools) +check() +release() +library(Amelia) +data(quakes) +quakes_missing <- add_missingess(quakes) +library(rMIDAS) +quakes_missing <- add_missingess(quakes) +quakes_missing <- add_missingess(quakes) +quakes_missing <- add_missingness(quakes) +quakes_missing <- add_missingness(quakes, 0.1) +amelia(quakes_missing, m = 1) +quakes_complete <- amelia(quakes_missing, m = 1) +quakes_complete$imputations[[1]] +quakes$cat <- sample(c("a","b","c"), length(quakes), replace = TRUE) +quakes +quakes_missing <- add_missingness(quakes, 0.1) +quakes_complete$imputations[[1]] +quakes_complete <- amelia(quakes_missing, m = 1) +quakes_complete <- amelia(quakes_missing, m = 1, noms = c("cat")) +quakes_complete$imputations$imp1 +library(Amelia) +data(quakes) +quakes +quakes$cat <- sample(c("a","b","c"), nrow(quakes), replace = TRUE) +quakes +quakes <- add_missingness(quakes, 0.3) +quakes +quakes$ord <- sample(c("1","2","3","4","5"), nrow(quakes), replace = TRUE) +quakes +data(quakes) +quakes$cat <- sample(c("a","b","c"), nrow(quakes), replace = TRUE) +quakes$ord <- sample(c("1","2","3","4","5"), nrow(quakes), replace = TRUE) +quakes +quakes <- add_missingness(quakes, 0.3) +quakes +quakes_complete <- amelia(quakes, m = 1, +noms = c("cat"), +ords = c("ord")) +quakes_complete <- amelia(quakes, m = 1, +noms = c("cat")) +data(quakes) +quakes$cat <- sample(c("a","b","c"), nrow(quakes), replace = TRUE) +quakes$ord <- as.factor(sample(c("1","2","3","4","5"), nrow(quakes), replace = TRUE)) +quakes +quakes <- add_missingness(quakes, 0.3) +quakes +quakes_complete <- amelia(quakes, m = 1, +noms = c("cat"), +ords = c("ord")) +levels(quakes$ord) +quakes$ord <- factor(sample(c("1","2","3","4","5"), nrow(quakes), replace = TRUE), +levels = c("1","2","3","4","5")) +levels(quakes$ord) +data(quakes) +quakes$cat <- sample(c("a","b","c"), nrow(quakes), replace = TRUE) +quakes$ord <- factor(sample(c("1","2","3","4","5"), nrow(quakes), replace = TRUE), +levels = c("1","2","3","4","5")) +quakes +quakes <- add_missingness(quakes, 0.3) +quakes +quakes +quakes_complete <- amelia(quakes, m = 1, +noms = c("cat"), +ords = c("ord")) +quakes_data <- quakes_complete$imputations$imp1 +quakes_data diff --git a/CRAN-SUBMISSION b/CRAN-SUBMISSION index bde3272..e627319 100644 --- a/CRAN-SUBMISSION +++ b/CRAN-SUBMISSION @@ -1,3 +1,3 @@ Version: 0.4.0 -Date: 2022-02-02 18:01:53 UTC -SHA: 6c541724f900f731cccf89e5d31a2f01c63582eb +Date: 2022-02-03 10:00:51 UTC +SHA: 5f3db7d61491ce824eda52b4080544898444d8db diff --git a/DESCRIPTION b/DESCRIPTION index 337ae13..2939243 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -30,6 +30,7 @@ Suggests: testthat, knitr, rmarkdown +SystemRequirements: Python (>= 2.7.0) VignetteBuilder: knitr License: Apache License (>= 2.0) URL: https://github.com/MIDASverse/rMIDAS diff --git a/R/load_utils.R b/R/load_utils.R index a19f240..3742793 100644 --- a/R/load_utils.R +++ b/R/load_utils.R @@ -9,3 +9,23 @@ skip_if_no_numpy <- function() { if (!have_numpy) testthat::skip("numpy not available for testing") } + +#' Check whether Python is capable of executing example code +#' +#' Checks if each Python dependency is available. +#' This function is called within some examples to ensure code executes properly. +#' @keywords setup +#' @return `NULL` +python_configured <- function() { + + py_dep <- c("matplotlib","numpy","pandas","tensorflow","sklearn","os","random", "tensorflow_addons") + dep_avail <- sapply(py_dep, function (x) reticulate::py_module_available(x)) + + if (sum(dep_avail) == length(py_dep)) { + return(TRUE) + } else { + return(FALSE) + } +} + + diff --git a/inst/examples/basic_workflow.R b/inst/examples/basic_workflow.R index a7df5c0..cc7ba9f 100644 --- a/inst/examples/basic_workflow.R +++ b/inst/examples/basic_workflow.R @@ -1,7 +1,7 @@ # Generate raw data, with numeric, binary, and categorical variables \dontrun{ -# Run where Python available -if (reticulate::py_module_available("numpy")) { +# Run where Python available and configured correctly +if (python_configured()) { set.seed(89) n_obs <- 10000 raw_data <- data.table(a = sample(c("red","yellow","blue",NA),n_obs, replace = TRUE), diff --git a/inst/examples/overimputation.R b/inst/examples/overimputation.R index 96cdfb6..0f66eae 100644 --- a/inst/examples/overimputation.R +++ b/inst/examples/overimputation.R @@ -1,6 +1,6 @@ \dontrun{ -# Run where Python available -if (reticulate::py_module_available("numpy")) { +# Run where Python available and configured correctly +if (python_configured()) { raw_data <- data.table(a = sample(c("red","yellow","blue",NA),1000, replace = TRUE), b = 1:1000,