Skip to content
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

Adjust code to read dr for AP for both constant and uncertain modes #79

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 13 additions & 52 deletions R/ithim_setup_parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -163,60 +163,21 @@ ithim_setup_parameters <- function(NSAMPLES = 1,
}

#### AP DOSE RESPONSE
AP_DOSE_RESPONSE_QUANTILE <<- AP_DOSE_RESPONSE_QUANTILE
## shortcut: use saved median values
if(!AP_DOSE_RESPONSE_QUANTILE){
global_path <- file.path(find.package('ithimr',lib.loc=.libPaths()), 'extdata/global/')
global_path <- paste0(global_path, "/")
DR_AP_LIST <<- readRDS(paste0(global_path,"dose_response/drap/dr_ap_list.Rds"))
}else{
if(AP_DOSE_RESPONSE_QUANTILE == T ) {
ap_diseases <- subset(DISEASE_INVENTORY, air_pollution==1)
dr_ap_list <- list()
ap_diseases <- subset(DISEASE_INVENTORY,air_pollution==1)
ap_parameters <- list()
for(disease in ap_diseases$ap_acronym){
for(letter in c('ALPHA_','BETA_','GAMMA_','TMREL_')){
if(AP_DOSE_RESPONSE_QUANTILE){
ap_parameters[[paste0('AP_DOSE_RESPONSE_QUANTILE_',letter,disease)]] <- runif(NSAMPLES,0,1)
parameters[[paste0('AP_DOSE_RESPONSE_QUANTILE_',letter,disease)]] <- ap_parameters[[paste0('AP_DOSE_RESPONSE_QUANTILE_',letter,disease)]]
} else {
ap_parameters[[paste0('AP_DOSE_RESPONSE_QUANTILE_',letter,disease)]] <- 0.5
}
}
dr_ap <- subset(DR_AP,cause_code==disease)
dr_ap_list[[disease]] <- list()
quant1 <- ap_parameters[[paste0('AP_DOSE_RESPONSE_QUANTILE_GAMMA_',disease)]]
quant2 <- ap_parameters[[paste0('AP_DOSE_RESPONSE_QUANTILE_BETA_',disease)]]
quant3 <- ap_parameters[[paste0('AP_DOSE_RESPONSE_QUANTILE_ALPHA_',disease)]]
quant4 <- ap_parameters[[paste0('AP_DOSE_RESPONSE_QUANTILE_TMREL_',disease)]]
for(age in unique(dr_ap$age_code)){
dr_ap_age <- subset(dr_ap,age_code==age)
#######################################
lbeta <- log(dr_ap_age$beta)
lgamma <- log(dr_ap_age$gamma)
gamma_val <- quantile(density(lgamma),quant1)
beta_val <- c()
for(i in 1:ifelse(AP_DOSE_RESPONSE_QUANTILE,NSAMPLES,1)){
den <- kde2d(lgamma,lbeta,n=c(1,100),h=0.2,lims=c(gamma_val[i],gamma_val[i],min(lbeta)-1,max(lbeta)+1))
beta_val[i] <- approx(x=cumsum(den$z)/sum(den$z),y=den$y,xout=quant2[i])$y
}
mod <- gam(log(alpha)~te(log(gamma),log(beta)),data=dr_ap_age)
pred_val <- predict(mod, newdata=data.frame(beta=exp(beta_val),gamma=exp(gamma_val)),se.fit=T)
alpha_val <- qnorm(quant3,pred_val$fit,sqrt(mod$sig2))
# generate a value for tmrel given alpha, beta and gamma
mod <- gam(log(tmrel)~ns(log(gamma),df=8)+ns(log(beta),df=8)+ns(log(alpha),df=8),data=dr_ap_age)
pred_val <- predict(mod, newdata=data.frame(alpha=exp(alpha_val),beta=exp(beta_val),gamma=exp(gamma_val)),se.fit=T)
tmrel_val <- qnorm(quant4,pred_val$fit,sqrt(mod$sig2))
dr_ap_list[[disease]][[age]] <- data.frame(alpha=exp(alpha_val),beta=exp(beta_val),gamma=exp(gamma_val),tmrel=exp(tmrel_val))
}
if(AP_DOSE_RESPONSE_QUANTILE){
# turn list inside out, so it's indexed first by sample
parameters$DR_AP_LIST <- lapply(1:NSAMPLES,function(x)lapply(dr_ap_list,function(y) lapply(y,function(z)z[x,])))
}else{
DR_AP_LIST <<- dr_ap_list
}
}

for(disease in ap_diseases$ap_acronym)
parameters[[paste0('AP_DOSE_RESPONSE_QUANTILE_',disease)]] <- runif(NSAMPLES,0,1)
}

# Read dr_ap_list.Rds which contains parameter values by diseases and ages, for both constant and uncertain mode
global_path <- file.path(find.package('ithimr',lib.loc=.libPaths()), 'extdata/global/')
global_path <- paste0(global_path, "/")
DR_AP_LIST <<- readRDS(paste0(global_path,"dose_response/drap/dr_ap_list.Rds"))

if(AP_DOSE_RESPONSE_QUANTILE)
parameters$DR_AP_LIST <- DR_AP_LIST

parameters
}

Expand Down