Skip to content

Commit

Permalink
Fix imputation
Browse files Browse the repository at this point in the history
  • Loading branch information
solivella committed May 24, 2024
1 parent a53fce3 commit 4519f6c
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions R/race_prediction_funs.R
Original file line number Diff line number Diff line change
Expand Up @@ -415,16 +415,14 @@ predict_race_new <- function(
}
}

## Normalize (recycle marginal)
preds <- preds/rowSums(preds)
## Revert to Pr(Race|Surname) for missing predictions
if(impute.missing){
miss_ind <- !is.finite(preds$c_whi_last)
if(any(miss_ind)){
preds[miss_ind,] <- voter.file[miss_ind, grep("_last$", names(voter.file))] *
matrix(race.margin, nrow=nrow(voter.file[miss_ind,]), ncol=length(race.margin), byrow = TRUE)
}
for(i in ncol(preds)){
preds[, i] <- dplyr::coalesce(df[, i], race.margin[i])
}
}
## Normalize (recycle marginal)
preds <- preds/rowSums(preds)
## Revert to national Pr(Race) for missing predictions
colnames(preds) <- paste("pred", eth, sep = ".")

return(data.frame(cbind(voter.file[c(vars.orig)], preds)))
Expand Down

0 comments on commit 4519f6c

Please sign in to comment.