-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Additional race stratification #57
Open
clchand23
wants to merge
14
commits into
main
Choose a base branch
from
dev-race-strat
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b2b305a
Update EpiStats.R
clchand23 47e43f8
Update NetParams.R
clchand23 2bf6aa0
Generalize race.cat variable
clchand23 173d264
Update EpiStats.R
clchand23 714f529
Update NetParams.R
clchand23 b9c3041
Update NetStats.R
clchand23 5e519df
Update NetStats.R
clchand23 1bdb124
Update NetStats.R
clchand23 9ea391c
Update EpiStats.R
clchand23 67642c9
Update NetStats.R
clchand23 d3b4c67
Update EpiStats.R
clchand23 4aca5a7
Update EpiStats.R
clchand23 fad1eb9
Update DESCRIPTION
clchand23 3c49e3c
Update build_epistats.Rd
clchand23 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,7 @@ build_netparams <- function(epistats, | |
## Inputs ## | ||
geog.lvl <- epistats$geog.lvl | ||
race <- epistats$race | ||
race.level <- epistats$race.level | ||
age.limits <- epistats$age.limits | ||
age.breaks <- epistats$age.breaks | ||
age.sexual.cessation <- epistats$age.sexual.cessation | ||
|
@@ -151,33 +152,56 @@ build_netparams <- function(epistats, | |
d$count.oo.part.trunc <- ifelse(d$count.oo.part > 100, 100, d$count.oo.part) | ||
|
||
|
||
## Race ethnicity ## | ||
if (race == TRUE) { | ||
# Race Ethnicity | ||
d$race.cat3 <- rep(NA, nrow(d)) | ||
d$race.cat3[d$race.cat == "black"] <- 1 | ||
d$race.cat3[d$race.cat == "hispanic"] <- 2 | ||
d$race.cat3[d$race.cat %in% c("white", "other")] <- 3 | ||
mult_race_cat <- c("asian", "ai/an", "mult", "nh/pi") | ||
flat_race.level <- unlist(race.level) | ||
|
||
# Determine which variables to use in ARTnet | ||
if (any(flat_race.level %in% mult_race_cat)) { | ||
l <- merge(l, d[, c("AMIS_ID", "race")], by = "AMIS_ID", all.x = TRUE) | ||
p_race_var <- "p_race2" | ||
race_var <- "race" | ||
} else { | ||
p_race_var <- "p_race.cat" | ||
race_var <- "race.cat" | ||
} | ||
|
||
l$race.cat3[l$race.cat == "black"] <- 1 | ||
l$race.cat3[l$race.cat == "hispanic"] <- 2 | ||
l$race.cat3[l$race.cat %in% c("white", "other")] <- 3 | ||
# Assign race categories based on race.level | ||
race.categories <- seq_along(race.level) | ||
|
||
l$p_race.cat3 <- rep(NA, nrow(l)) | ||
l$p_race.cat3[l$p_race.cat == "black"] <- 1 | ||
l$p_race.cat3[l$p_race.cat == "hispanic"] <- 2 | ||
l$p_race.cat3[l$p_race.cat %in% c("white", "other")] <- 3 | ||
d$race.cat.num <- rep(NA, nrow(d)) | ||
l$race.cat.num <- rep(NA, nrow(l)) | ||
l$p_race.cat.num <- rep(NA, nrow(l)) | ||
|
||
# redistribute NAs in proportion to non-missing partner races | ||
probs <- prop.table(table(l$race.cat3, l$p_race.cat3), 1) | ||
for (i in seq_along(race.level)) { | ||
d$race.cat.num[d[[race_var]] %in% race.level[[i]]] <- race.categories[i] | ||
l$race.cat.num[l[[race_var]] %in% race.level[[i]]] <- race.categories[i] | ||
l$p_race.cat.num[l[[p_race_var]] %in% race.level[[i]]] <- race.categories[i] | ||
} | ||
|
||
imp_black <- which(is.na(l$p_race.cat3) & l$race.cat3 == 1) | ||
l$p_race.cat3[imp_black] <- sample(1:3, length(imp_black), TRUE, probs[1, ]) | ||
# Redistribute NAs in proportion to non-missing partner races | ||
probs <- prop.table(table(l$race.cat.num, l$p_race.cat.num), 1) | ||
|
||
imp_hisp <- which(is.na(l$p_race.cat3) & l$race.cat3 == 2) | ||
l$p_race.cat3[imp_hisp] <- sample(1:3, length(imp_hisp), TRUE, probs[2, ]) | ||
for (i in race.categories) { | ||
imp_indices <- which(is.na(l$p_race.cat.num) & l$race.cat.num == i) | ||
if (length(imp_indices) > 0) { | ||
l$p_race.cat.num[imp_indices] <- sample(race.categories, length(imp_indices), TRUE, probs[i, ]) | ||
} | ||
} | ||
|
||
imp_white <- which(is.na(l$p_race.cat3) & l$race.cat3 == 3) | ||
l$p_race.cat3[imp_white] <- sample(1:3, length(imp_white), TRUE, probs[3, ]) | ||
# Initialize race.combo and assign combinations dynamically | ||
l$race.combo <- rep(NA, nrow(l)) | ||
combo_index <- 1 | ||
for (i in race.categories) { | ||
# Case 1: Same race as one combination | ||
l$race.combo[l$race.cat.num == i & l$p_race.cat.num == i] <- combo_index | ||
combo_index <- combo_index + 1 | ||
|
||
# Case 2: Race compared with all other race groups | ||
l$race.combo[l$race.cat.num == i & l$p_race.cat.num %in% setdiff(race.categories, i)] <- combo_index | ||
combo_index <- combo_index + 1 | ||
} | ||
|
||
} | ||
|
||
|
@@ -318,21 +342,21 @@ build_netparams <- function(epistats, | |
## nodematch("race", diff = TRUE) ---- | ||
|
||
if (race == TRUE) { | ||
lmain$same.race <- ifelse(lmain$race.cat3 == lmain$p_race.cat3, 1, 0) | ||
lmain$same.race <- ifelse(lmain$race.cat.num == lmain$p_race.cat.num, 1, 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
if (is.null(geog.lvl)) { | ||
mod <- glm(same.race ~ as.factor(race.cat3), | ||
mod <- glm(same.race ~ as.factor(race.cat.num), | ||
data = lmain, family = binomial()) | ||
|
||
dat <- data.frame(race.cat3 = 1:3) | ||
dat <- data.frame(race.cat.num = race.categories) | ||
pred <- predict(mod, newdata = dat, type = "response") | ||
|
||
out$main$nm.race <- as.numeric(pred) | ||
} else { | ||
mod <- glm(same.race ~ geogYN + as.factor(race.cat3), | ||
mod <- glm(same.race ~ geogYN + as.factor(race.cat.num), | ||
data = lmain, family = binomial()) | ||
|
||
dat <- data.frame(geogYN = 1, race.cat3 = 1:3) | ||
dat <- data.frame(geogYN = 1, race.cat.num = race.categories) | ||
pred <- predict(mod, newdata = dat, type = "response") | ||
|
||
out$main$nm.race <- as.numeric(pred) | ||
|
@@ -358,18 +382,18 @@ build_netparams <- function(epistats, | |
} | ||
|
||
if (is.null(geog.lvl)) { | ||
mod <- glm(deg.main ~ as.factor(race.cat3), | ||
mod <- glm(deg.main ~ as.factor(race.cat.num), | ||
data = d, family = poisson()) | ||
|
||
dat <- data.frame(race.cat3 = 1:3) | ||
dat <- data.frame(race.cat.num = race.categories) | ||
pred <- predict(mod, newdata = dat, type = "response") | ||
|
||
out$main$nf.race <- as.numeric(pred) | ||
} else { | ||
mod <- glm(deg.main ~ geogYN + as.factor(race.cat3), | ||
mod <- glm(deg.main ~ geogYN + as.factor(race.cat.num), | ||
data = d, family = poisson()) | ||
|
||
dat <- data.frame(geogYN = 1, race.cat3 = 1:3) | ||
dat <- data.frame(geogYN = 1, race.cat.num = race.categories) | ||
pred <- predict(mod, newdata = dat, type = "response") | ||
|
||
out$main$nf.race <- as.numeric(pred) | ||
|
@@ -642,21 +666,21 @@ build_netparams <- function(epistats, | |
|
||
## nodematch("race") ---- | ||
|
||
lcasl$same.race <- ifelse(lcasl$race.cat3 == lcasl$p_race.cat3, 1, 0) | ||
lcasl$same.race <- ifelse(lcasl$race.cat.num == lcasl$p_race.cat.num, 1, 0) | ||
|
||
if (is.null(geog.lvl)) { | ||
mod <- glm(same.race ~ as.factor(race.cat3), | ||
mod <- glm(same.race ~ as.factor(race.cat.num), | ||
data = lcasl, family = binomial()) | ||
|
||
dat <- data.frame(race.cat3 = 1:3) | ||
dat <- data.frame(race.cat.num = race.categories) | ||
pred <- predict(mod, newdata = dat, type = "response") | ||
|
||
out$casl$nm.race <- as.numeric(pred) | ||
} else { | ||
mod <- glm(same.race ~ geogYN + as.factor(race.cat3), | ||
mod <- glm(same.race ~ geogYN + as.factor(race.cat.num), | ||
data = lcasl, family = binomial()) | ||
|
||
dat <- data.frame(geogYN = 1, race.cat3 = 1:3) | ||
dat <- data.frame(geogYN = 1, race.cat.num = race.categories) | ||
pred <- predict(mod, newdata = dat, type = "response") | ||
|
||
out$casl$nm.race <- as.numeric(pred) | ||
|
@@ -686,18 +710,18 @@ build_netparams <- function(epistats, | |
|
||
if (is.null(geog.lvl)) { | ||
|
||
mod <- glm(deg.casl ~ as.factor(race.cat3), | ||
mod <- glm(deg.casl ~ as.factor(race.cat.num), | ||
data = d, family = poisson()) | ||
|
||
dat <- data.frame(race.cat3 = 1:3) | ||
dat <- data.frame(race.cat.num = race.categories) | ||
pred <- predict(mod, newdata = dat, type = "response") | ||
|
||
out$casl$nf.race <- as.numeric(pred) | ||
} else { | ||
mod <- glm(deg.casl ~ geogYN + as.factor(race.cat3), | ||
mod <- glm(deg.casl ~ geogYN + as.factor(race.cat.num), | ||
data = d, family = poisson()) | ||
|
||
dat <- data.frame(geogYN = 1, race.cat3 = 1:3) | ||
dat <- data.frame(geogYN = 1, race.cat.num = race.categories) | ||
pred <- predict(mod, newdata = dat, type = "response") | ||
|
||
out$casl$nf.race <- as.numeric(pred) | ||
|
@@ -971,21 +995,21 @@ build_netparams <- function(epistats, | |
|
||
## nodematch("race", diff = TRUE) ---- | ||
|
||
linst$same.race <- ifelse(linst$race.cat3 == linst$p_race.cat3, 1, 0) | ||
linst$same.race <- ifelse(linst$race.cat.num == linst$p_race.cat.num, 1, 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same |
||
|
||
if (is.null(geog.lvl)) { | ||
mod <- glm(same.race ~ as.factor(race.cat3), | ||
mod <- glm(same.race ~ as.factor(race.cat.num), | ||
data = linst, family = binomial()) | ||
|
||
dat <- data.frame(race.cat3 = 1:3) | ||
dat <- data.frame(race.cat.num = race.categories) | ||
pred <- predict(mod, newdata = dat, type = "response") | ||
|
||
out$inst$nm.race <- as.numeric(pred) | ||
} else { | ||
mod <- glm(same.race ~ geogYN + as.factor(race.cat3), | ||
mod <- glm(same.race ~ geogYN + as.factor(race.cat.num), | ||
data = linst, family = binomial()) | ||
|
||
dat <- data.frame(geogYN = 1, race.cat3 = 1:3) | ||
dat <- data.frame(geogYN = 1, race.cat.num = race.categories) | ||
pred <- predict(mod, newdata = dat, type = "response") | ||
|
||
out$inst$nm.race <- as.numeric(pred) | ||
|
@@ -1015,18 +1039,18 @@ build_netparams <- function(epistats, | |
## nodefactor("race") ---- | ||
|
||
if (is.null(geog.lvl)) { | ||
mod <- glm(count.oo.part ~ as.factor(race.cat3), | ||
mod <- glm(count.oo.part ~ as.factor(race.cat.num), | ||
data = d, family = poisson()) | ||
|
||
dat <- data.frame(race.cat3 = 1:3) | ||
dat <- data.frame(race.cat.num = race.categories) | ||
pred <- predict(mod, newdata = dat, type = "response") / (364 / time.unit) | ||
|
||
out$inst$nf.race <- as.numeric(pred) | ||
} else { | ||
mod <- glm(count.oo.part ~ geogYN + as.factor(race.cat3), | ||
mod <- glm(count.oo.part ~ geogYN + as.factor(race.cat.num), | ||
data = d, family = poisson()) | ||
|
||
dat <- data.frame(geogYN = 1, race.cat3 = 1:3) | ||
dat <- data.frame(geogYN = 1, race.cat.num = race.categories) | ||
pred <- predict(mod, newdata = dat, type = "response") / (364 / time.unit) | ||
|
||
out$inst$nf.race <- as.numeric(pred) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this section that create the
race.combos
could be made into an exported function. This way it could be re-used directly on EpiModel HIV.