R/data_read.R

Defines functions read_data

read_data <- function(data, holdout,
                      treated_column_name, outcome_column_name, weights) {
  if (is.character(data)) {
    tryCatch(
      error = function(cnd) {
        stop('Cannot read `data` .csv file from working directory')
      },
      data <- read.csv(data, header = TRUE)
    )
    cov_inds <-
      which(!(colnames(data) %in% c(treated_column_name, outcome_column_name)))
    for (cov in cov_inds) {
      data[[cov]] <- as.factor(data[[cov]])
    }
  }

  if (is.character(holdout)) {
    tryCatch(
      error = function(cnd) {
        stop('Cannot read `holdout` .csv file from working directory')
      },
      holdout <- read.csv(holdout, header = TRUE)
    )
    cov_inds <- which(!(colnames(holdout) %in%
                        c(treated_column_name, outcome_column_name)))

    for (cov in cov_inds) {
      holdout[[cov]] <- as.factor(holdout[[cov]])
    }
  }

  if (is.numeric(holdout) && length(holdout) == 1) {
    if (is.null(weights)) { # Use holdout to compute PE
      holdout_inds <-
        sample(seq_len(nrow(data)), size = round(holdout * nrow(data)))
      holdout <- data[holdout_inds, ]
      data <- data[-holdout_inds, ]
    }
    else {
      # Don't need a holdout; for now, here's a hacky fix to not break things
      #  downstream
      holdout <- data
    }
  }

  return(list(data = data,
              holdout = holdout))
}

Try the FLAME package in your browser

Any scripts or data that you put into this service are public.

FLAME documentation built on Dec. 11, 2021, 9:26 a.m.