Last updated: 2023-11-15

Checks: 5 2

Knit directory: ~/multistate2/

This reproducible R Markdown analysis was created with workflowr (version 1.7.0). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


The R Markdown file has unstaged changes. To know which version of the R Markdown file created these results, you’ll want to first commit it to the Git repo. If you’re still working on the analysis, you can ignore this warning. When you’re finished, you can run wflow_publish to commit the R Markdown file and build the HTML.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20230211) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Using absolute paths to the files within your workflowr project makes it difficult for you and others to run your code on a different machine. Change the absolute path(s) below to the suggested relative path(s) to make your code more reproducible.

absolute relative
~/multistate2//code/smoothtest.R code/smoothtest.R
~/multistate2//code/newsmooth.R code/newsmooth.R
~/multistate2/code/fitarray.R code/fitarray.R
~/multistate2/code/arrayindicate.R code/arrayindicate.R
~/multistate2//output/dfascvd_newbp.rds output/dfascvd_newbp.rds
~/multistate2/code/frs30_URBUT/fun.frs_30y.R code/frs30_URBUT/fun.frs_30y.R
~/multistate2/output/fosauc.pdf output/fosauc.pdf

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.

The results in this page were generated with repository version 988af83. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .DS_Store
    Ignored:    .Rproj.user/
    Ignored:    analysis/.DS_Store
    Ignored:    code/.DS_Store
    Ignored:    data/
    Ignored:    lesliepics/.DS_Store
    Ignored:    output/
    Ignored:    plots/.DS_Store

Unstaged changes:
    Modified:   analysis/NRI.Rmd
    Modified:   analysis/fos.Rmd
    Modified:   analysis/index.Rmd
    Modified:   analysis/rocplots.Rmd

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the repository in which changes were made to the R Markdown (analysis/fos.Rmd) and HTML (docs/fos.html) files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.

File Version Author Date Message
Rmd ecd7828 Sarah Urbut 2023-10-13 Update
Rmd 273d024 Sarah Urbut 2023-09-22 Update
Rmd 4d7d65c Sarah Urbut 2023-09-17 Update

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

#rm(list=ls())
source("multistate2/code/utils.R")
source("~/multistate2//code/smoothtest.R")
source("~/multistate2//code/newsmooth.R")
source("~/multistate2/code/fitarray.R")
library("reshape2")
source("~/multistate2/code/arrayindicate.R")
source("../code/frs30_URBUT/fun.frs_30ynew.R")
load("~/Library/CloudStorage/Dropbox-Personal///pheno_dir/output/merged_pheno_censor_final_withdrugs_smoke.rds")

fos=read.table("~/Desktop/sraempty/phs000007.v33.pht006027.v4.p14.c1.vr_wkthru_ex09_1_1001s.HMB-IRB-MDS.txt.gz",header = T,skip = 1,sep="\t")
fhs=read.table("~/Desktop/sraempty/phs000007.v33.pht007777.v3.p14.c1.vr_wkthru_ex32_0_0997s.HMB-IRB-MDS.txt.gz",header = T,skip = 1,sep="\t")
surv=read.table("/Users/sarahurbut/Desktop/sraempty/phs000007.v33.pht003316.v10.p14.c1.vr_survcvd_2019_a_1334s.HMB-IRB-MDS.txt.gz",skip=1,sep="\t",header=T)
dfh$cad.prs.lec=cut(dfh$cad.prs,breaks = c(-5,-0.84,0.84,5),labels = c("low","mid","high"))
dfh$int=interaction(dfh$f.31.0.0,dfh$cad.prs.lec)
# Relabel the levels of the interaction variable
levels(dfh$int) <- c(1,2,3,4,5,6)


train=dfh[1:(nrow(dfh)*0.80),]

dfascvd=readRDS("~/multistate2//output/dfascvd_newbp.rds")
test=dfh[!(dfh$identifier%in%train$identifier),]
test=merge(test,dfascvd[,-which(names(dfascvd)%in%c("age","anylipidmed0","bp_med2","smoke"))],by.x="identifier",by.y="sample_id")
test$ascvd_10y_accaha=test$as2
test$phenos.enrollment=test$f.21003.0.0
test=data.table(test)
source("~/multistate2/code/frs30_URBUT/fun.frs_30y.R")
library(CVrisk)
ages=c(40:80)
nstates=c("Health", "Ht","HyperLip","Dm","Cad","death","Ht&HyperLip","HyperLip&Dm","Ht&Dm","Ht&HyperLip&Dm")

library(CVrisk)
m=merge(fos[,c("shareid","AGE1","SBP1","HDL1","SEX","CURRSMK1","HRX1","LIPRX1","TC1","DMRX1")],surv,by = "shareid")
prs=fread("~/Library/CloudStorage/Dropbox-Personal/Fram_allchr_CAD_c1.profile")
prs$score=scale(prs$V2)

firstmeasure=na.omit(merge(m,prs[,c("V1","score")],by.x="shareid",by.y="V1"))
# in FOS, using the wkthru:
# SEX       1   Male
# SEX       2   Female
# SEX       .   Unknown

## for our model, female is 0, male is 1


firstmeasure$sex=ifelse(firstmeasure$SEX==2,"female","male")
firstmeasure$sexu=ifelse(firstmeasure$SEX==2,0,1)
firstmeasure$cad.prs.lec=cut(firstmeasure$score,breaks = c(-5.02,-0.84,0.84,5.02),labels = c("low","mid","high"))

firstmeasure$int=interaction(firstmeasure$sexu,firstmeasure$cad.prs.lec)
levels(firstmeasure$int) <- c(1,2,3,4,5,6)


firstmeasure$as2=compute_CVrisk2(firstmeasure,scores = "as2",age = "AGE1",gender = "sex",
                       race = "white",sbp = "SBP1",hdl = "HDL1",
                bp_med ="HRX1",totchol = "TC1",diabetes = "DMRX1",smoker = "CURRSMK1",lipid_med = "LIPRX1")$as2

    dat = data.frame(
      "id" = firstmeasure$shareid,
      "mysex" = as.factor(firstmeasure$sex),
      "myage" = firstmeasure$AGE1,
      "mysbp" = firstmeasure$SBP1,
      "mytreat" = firstmeasure$HRX1,
      "mysmoking" = firstmeasure$CURRSMK1,
      "mydiabetes" = firstmeasure$DMRX1,
      "mytotalchol" = firstmeasure$TC1,
      "myhdl" = firstmeasure$HDL1,
      "Race" = "white",
      "mystatnow" = firstmeasure$LIPRX1
    )
    #
    frs = fun.frs_30yn(
      dat,
      id = "id",
      sex = "mysex",
      age = "myage",
      sbp = "mysbp",
      treat = "mytreat",
      smoking = "mysmoking",
      diabetes = "mydiabetes",
      totalchol = "mytotalchol",
      hdl = "myhdl"
    )

firstmeasure$lt=frs$frs_orig
ages=c(20:80)
modelfit=fitfunc2(data.table(train),ages = ages,nstates = nstates,mode = "binomial",covariates ="cad.prs+f.31.0.0+smoke+antihtn_now+statin_now")
#                   

firstmeasure=firstmeasure[firstmeasure$LIPRX1==0&firstmeasure$HRX1==0&firstmeasure$TC1<240&firstmeasure$DMRX1==0,]

b=coefplotsmooth2(ages = ages,start = "Health",stop = "Cad",modelfit = modelfit,window_width = 20,span = 0.75,degree = 2)
coefs=b$custom_smooth

ten.year.new=lifetime.new=ascvd.30.year=ascvd.10.year=emp.ten.year=emp.lifetime=vector()
agesint=seq(40,55,by=5)
aucmat=semat=matrix(NA,nrow=length(agesint),ncol=4)
firstmeasure$round5=round_any(firstmeasure$AGE1,5,f = round)


for(i in 1:length(agesint)){
  age=agesint[i]
  

  atrisk=firstmeasure[which(firstmeasure$round5==age),]
  
   ar = data.frame(
      "intercept" = 1,
      "cad.prs" = atrisk$score,
      "sex" = atrisk$sexu,
      "smoke" = atrisk$CURRSMK1,
      "antihtn_now" = atrisk$HRX1,
      "statin_now" = atrisk$LIPRX1
      )
   
   ar=na.omit(ar)

    
    atrisk$msten= compute_prediction_product_matrix(
        atrisk = ar,
        agepredinterval = c(age:(age + 10)),
        coefmat = coefs
      )$PredictedIntervalrisk
    
     ten.year.new[i] = mean(atrisk$msten)

    
    atrisk$mslifetime=compute_prediction_product_matrix(
        atrisk = ar,
        agepredinterval = c(age:(80)),
        coefmat = coefs
      )$PredictedIntervalrisk
    lifetime.new[i] = mean(atrisk$mslifetime)
    emp.ten.year[i] = mean(atrisk$chd==1&atrisk$chddate<(365*10))
    emp.lifetime[i] = mean(atrisk$chd==1&atrisk$chddate<(365*(80-age)))
                                        
  
   ascvd.10.year[i]=mean(atrisk$as2)
   ascvd.30.year[i]=mean(atrisk$lt)
   
   
  atrisk$outcometen=ifelse(atrisk$chd==1&atrisk$chddate<(365*10),1,0)
  atrisk$outcomelife=ifelse(atrisk$chd==1&atrisk$chddate<(365*30),1,0)

     #d=d[!is.na(d$ascvd_10y_accaha),]
  aucmat[i, 1] = roc(atrisk$outcometen ~ atrisk$msten)$auc
  semat[i, 1] = sqrt(var(roc(atrisk$outcometen ~ atrisk$msten)))
  #
  aucmat[i, 2] = roc(atrisk$outcometen ~ atrisk$as2)$auc
  semat[i, 2] = sqrt(var(roc(atrisk$outcometen ~ atrisk$as2)))
  #
  aucmat[i, 3] = roc(atrisk$outcomelife ~ atrisk$mslifetime)$auc
  semat[i, 3] = sqrt(var(roc(atrisk$outcomelife ~ atrisk$mslifetime)))

  aucmat[i, 4] =  roc(atrisk$outcomelife ~ atrisk$lt)$auc
  semat[i, 4] = sqrt(var(roc(atrisk$outcomelife ~ atrisk$lt)))
  
}
  
  
  

library(ehaGoF)

pcten=gofRMSE(Obs = as.vector(emp.ten.year*100),Prd = as.vector(ascvd.10.year))
msten=gofRMSE(Obs = as.vector(emp.ten.year*100),Prd = as.vector(ten.year.new*100))
mslif=gofRMSE(Obs = as.vector(emp.lifetime*100),Prd = as.vector(lifetime.new*100))
pclif=gofRMSE(Obs = as.vector(emp.lifetime*100),Prd = as.vector(ascvd.30.year))


df <- data.frame(
  metric = c("PCEten", "MSTen", "MSLife", "FRS30y"),
  value = c(pcten,msten,mslif,pclif)
)

df$metric=factor(df$metric,levels = c("MSTen","PCEten","MSLife","FRS30y"))

# Create the barplot
rmse=ggplot(df, aes(x = metric, y = value,fill=metric)) +
  geom_bar(stat = "identity") +
  labs(y = "RMSE (%)", x = "Score",fill="score") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))



rownames(aucmat)=rownames(semat)=agesint
colnames(aucmat)=colnames(semat)=c("MSten","PCEten","MSLife","FRS30y")



# Convert matrices to data frames
df_auc <- as.data.frame(aucmat)
df_se <- as.data.frame(semat)

# Add age column
df_auc$age <- rownames(df_auc)
df_se$age <- rownames(df_se)

# Convert data frames to long format
df_auc_long <- df_auc %>%
  gather(key = "metric", value = "value", -age)

df_auc_long$metric=factor(df_auc_long$metric,levels = c("MSten","PCEten","MSLife","FRS30y"))

df_se_long <- df_se %>%
  gather(key = "metric", value = "se_value", -age)

df_se_long$metric=factor(df_se_long$metric,levels = c("MSten","PCEten","MSLife","FRS30y"))

# Join both data frames
df <- left_join(df_auc_long, df_se_long, by = c("age", "metric"))

# Plot using ggplot2
fos_auc=ggplot(df, aes(x = age, y = value, fill = metric, group = metric)) +
  geom_bar(stat = "identity", position = position_dodge(width = 0.8)) +
  geom_errorbar(
    aes(ymin = value - se_value, ymax = value + se_value),
    position = position_dodge(width = 0.8),
    width = 0.25
  ) +
  labs(y = "AUC ROC", x = "Ages",fill="Score") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))


df <- data.frame(
  metric = c( "MSLife", "FRS30y"),
  value = c(pcten,msten,mslif,pclif)
)

df$metric=factor(df$metric,levels = c("MSLife","FRS30y"))

# Create the barplot
rmse=ggplot(df[c(3,4),], aes(x = metric, y = value,fill=metric)) +
  geom_bar(stat = "identity") +
  labs(y = "RMSE (%)", x = "Score",fill="score") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))



rownames(aucmat)=rownames(semat)=agesint
colnames(aucmat)=colnames(semat)=c("MSten","PCE","MSLife","FRS30y")



# Convert matrices to data frames
df_auc <- as.data.frame(aucmat[,c(3,4)])
df_se <- as.data.frame(semat[,c(3,4)])

# Add age column
df_auc$age <- rownames(df_auc)
df_se$age <- rownames(df_se)

# Convert data frames to long format
df_auc_long <- df_auc %>%
  gather(key = "metric", value = "value", -age)

df_auc_long$metric=factor(df_auc_long$metric,levels = c("MSLife","FRS30y"))

df_se_long <- df_se %>%
  gather(key = "metric", value = "se_value", -age)

df_se_long$metric=factor(df_se_long$metric,levels = c("MSLife","FRS30y"))

# Join both data frames
df <- left_join(df_auc_long, df_se_long, by = c("age", "metric"))

# Plot using ggplot2
fos_auc=ggplot(df, aes(x = age, y = value, fill = metric, group = metric)) +
  geom_bar(stat = "identity", position = position_dodge(width = 0.8)) +
  geom_errorbar(
    aes(ymin = value - se_value, ymax = value + se_value),
    position = position_dodge(width = 0.8),
    width = 0.25
  ) +
  labs(y = "AUC ROC", x = "Ages",fill="Score") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

ggsave(ggarrange(rmse,fos_auc,common.legend = T),file="~/multistate2/output/fosauc.pdf",dpi=600)
fos=read.table("~/Desktop/sraempty/phs000007.v33.pht006027.v4.p14.c1.vr_wkthru_ex09_1_1001s.HMB-IRB-MDS.txt.gz",header = T,skip = 1,sep="\t")
fhs=read.table("~/Desktop/sraempty/phs000007.v33.pht007777.v3.p14.c1.vr_wkthru_ex32_0_0997s.HMB-IRB-MDS.txt.gz",header = T,skip = 1,sep="\t")
surv=read.table("/Users/sarahurbut/Desktop/sraempty/phs000007.v33.pht003316.v10.p14.c1.vr_survcvd_2019_a_1334s.HMB-IRB-MDS.txt.gz",skip=1,sep="\t",header=T)
dfh$cad.prs.lec=cut(dfh$cad.prs,breaks = c(-5,-0.84,0.84,5),labels = c("low","mid","high"))
dfh$int=interaction(dfh$f.31.0.0,dfh$cad.prs.lec)
# Relabel the levels of the interaction variable
levels(dfh$int) <- c(1,2,3,4,5,6)


train=dfh[1:(nrow(dfh)*0.80),]

dfascvd=readRDS("~/multistate2//output/dfascvd_newbp.rds")
test=dfh[!(dfh$identifier%in%train$identifier),]
test=merge(test,dfascvd[,-which(names(dfascvd)%in%c("age","anylipidmed0","bp_med2","smoke"))],by.x="identifier",by.y="sample_id")
test$ascvd_10y_accaha=test$as2
test$phenos.enrollment=test$f.21003.0.0
test=data.table(test)
source("~/multistate2/code/frs30_URBUT/fun.frs_30y.R")
library(CVrisk)
ages=c(40:80)
nstates=c("Health", "Ht","HyperLip","Dm","Cad","death","Ht&HyperLip","HyperLip&Dm","Ht&Dm","Ht&HyperLip&Dm")

library(CVrisk)
m=merge(fos[,c("shareid","AGE1","SBP1","HDL1","SEX","CURRSMK1","HRX1","LIPRX1","TC1","DMRX1")],surv,by = "shareid")
prs=fread("~/Library/CloudStorage/Dropbox-Personal/Fram_allchr_CAD_c1.profile")
prs$score=scale(prs$V2)

firstmeasure=na.omit(merge(m,prs[,c("V1","score")],by.x="shareid",by.y="V1"))
# in FOS, using the wkthru:
# SEX       1   Male
# SEX       2   Female
# SEX       .   Unknown

## for our model, female is 0, male is 1


firstmeasure$sex=ifelse(firstmeasure$SEX==2,"female","male")
firstmeasure$sexu=ifelse(firstmeasure$SEX==2,0,1)
firstmeasure$cad.prs.lec=cut(firstmeasure$score,breaks = c(-5.02,-0.84,0.84,5.02),labels = c("low","mid","high"))

firstmeasure$int=interaction(firstmeasure$sexu,firstmeasure$cad.prs.lec)
levels(firstmeasure$int) <- c(1,2,3,4,5,6)


library(table1)
f2=setdiff(names(fos),names(firstmeasure))
fo2=data.table(fos)
fo2=fo2[,..f2]
fo2$id=fos$dbGaP_Subject_ID
dat=merge(firstmeasure,fo2,by.x = "dbGaP_Subject_ID",by.y="id")
dat[is.na(dat)]=0
dat=dat[-(which(dat$chddate<=0)),]
dat$HRever=apply(dat,1,function(x){
  ifelse(sum(x[grep(names(dat),pattern="HRX")]>0),1,0)})
dat$HT=apply(dat,1,function(x){
  ifelse(sum(x[grep(names(dat),pattern="SBP")]>150)>0,1,0)})
dat$HL=apply(dat,1,function(x){
  ifelse(sum(x[grep(names(dat),pattern="TC")]>200)>0,1,0)})
dat$DM=apply(dat,1,function(x){
  ifelse(sum(x[grep(names(dat),pattern="DMR")]>0),1,0)})

dat$agerec=as.numeric(dat$AGE1)
dat$agerec[dat$agerec<18]=18
dat$f.31.0.0=dat$sex
dat$Cad_0_Any=as.factor(ifelse(dat$chd==1,1,0))
dat$cad.prs.lev=factor(dat$cad.prs.lec,levels = c("low","mid","high"),labels=c("Low","Intermediate","High"))
dat$smoke=ifelse(dat$CURRSMK1==1,1,0)
dat$durationfollowed=apply(dat,1,function(x){
  round(max(as.numeric(x[grep(names(dat),pattern="DATE")]))/365.25,2)
})
dat$durationfollowed[dat$durationfollowed==0]=12
library(table1)
label(dat$f.31.0.0) <- "Sex"
label(dat$HRever) <- "Start an anti-Hypertensive"
label(dat$smoke) <- "Current Smoker"
label(dat$HT) <- "Develop Hypertension"
label(dat$DM) <- "Develop Diabetes"
label(dat$HL) <- "Develop Hyperlipidemia"
label(dat$Cad_0_Any) <- "Develop Coronary Disease"



library(table1)
label(dat$f.31.0.0) <- "Sex"
label(dat$durationfollowed)="Years Followed"
label(dat$SBP1) <- "SBP at Baseline"
label(dat$smoke) <- "Current Smoker"
label(dat$HT) <- "Develop Hypertension"
label(dat$DM) <- "Develop Diabetes"
label(dat$HL) <- "Develop Hyperlipidemia"
label(dat$Cad_0_Any) <- "Develop Coronary Disease"
label(dat$agerec) = "Age of First Measured"
f=table1(~ f.31.0.0 +agerec+HT+Cad_0_Any+HL+HRever+smoke+durationfollowed|cad.prs.lev, data=dat)

sessionInfo()