RobertF
RobertF

Reputation: 904

R error: Error in x[[jj]] <- v : attempt to select less than one element in integerOneIndex when running function

I'm seeing the above error when I attempt to run the following function code snippet on my test dataset:

df <- data.frame(id=c(1,2,3,4), actual=c(0,1,0,1), score=c(.7223,.8904,.0037,.0025), prediction=c(1,0,0,1),
                 black=c(.6476,.0534,.0702,NA), hispanic=c(.1406,.8262,.0645,NA), asian=c(.1100,.0141,.7930,NA),
                 white=c(.0444,.0740,.0056,NA), female=c(0,1,0,1), male=c(1,0,1,0), weight=c(.1755,.8101,.1332,.6420))

adverse_impact_ratio <- function (
  data_frame,
  lower_outcome_favorable,
  outcome,
  true_outcome,
  pg_names,
  cg_names,
  sample_weight,
  air_threshold
) {

  data <- data_frame
  
  if (is.null(sample_weight)) {
    data['sample_weight'] <- 1
    sample_weight <- 'sample_weight'
  }
  
  if (lower_outcome_favorable) {
    data[outcome] <- 1 - data[outcome]
    if (!is.null(true_outcome)) {
      data[true_outcome] <- 1 - data[true_outcome]
    }
  }
}

adverse_impact_ratio(
  df, 
  1, 
  df$score, 
  df$actual, 
  pg_names=c('black', 'hispanic', 'asian', 'female'), 
  cg_names=c('white', 'white', 'white', 'male'), 
  df$weight, 
  0.9) 

Any ideas what I'm doing wrong? I've seen similar questions in this forum but they usually involve for loops, which isn't the case here. Thanks!

Upvotes: 1

Views: 447

Answers (1)

brunomp
brunomp

Reputation: 44

try:

adverse_impact_ratio(
  df, 
  1, 
  "score", 
  "actual", 
  pg_names=c('black', 'hispanic', 'asian', 'female'), 
  cg_names=c('white', 'white', 'white', 'male'), 
  df$weight, 
  0.9) 

Upvotes: 2

Related Questions