Skip to content

Commit

Permalink
Add assigment week 4
Browse files Browse the repository at this point in the history
Add assigment week 4 (reproducible repo
  • Loading branch information
kirstenvankessel committed Nov 22, 2023
1 parent b4eda6a commit 4b3b05f
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 0 deletions.
1 change: 1 addition & 0 deletions Kirsten/Assignment week 4/.Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
^LICENSE\.md$
Binary file added Kirsten/Assignment week 4/Figure_STRATOS.tif
Binary file not shown.
21 changes: 21 additions & 0 deletions Kirsten/Assignment week 4/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# MIT License

Copyright (c) 2023 Week_4_Exercise_2 authors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
8 changes: 8 additions & 0 deletions Kirsten/Assignment week 4/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

# Week_4_Exercise_2

<!-- badges: start -->
<!-- badges: end -->

The goal of Week_4_Exercise_2 is to create a research compendium of the simulation study performed by Boulesteix, Groenwold, Abrahamowicz, et al. (2020).

13 changes: 13 additions & 0 deletions Kirsten/Assignment week 4/Week_4_Exercise_2.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX
Binary file not shown.
100 changes: 100 additions & 0 deletions Kirsten/Assignment week 4/scripts/original_script.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# ====================================================================
# R CODE
# small scale simulation study to investigate impact of measurement error
# measurement error on (continuous) exposure and/or (continuous) confounding variable
# ====================================================================
# ====================================================================
# libraries:
library(Hmisc)
library(mice)
library(tidyverse)
#setwd("")
# ====================================================================
# set working directory:
# setwd("")
# ====================================================================
# The data can be dowloaded in xpt form from https://wwwn.cdc.gov/nchs/nhanes/continuousnhanes/default.aspx?BeginYear=2015
# read data:
d1 <- sasxport.get("DEMO_I.xpt")
d2 <- sasxport.get("BPX_I.xpt")
d3 <- sasxport.get("BMX_I.xpt")
d4 <- sasxport.get("GHB_I.xpt")
d5 <- sasxport.get("TCHOL_I.xpt")
d1.t <- subset(d1,select=c("seqn","riagendr","ridageyr"))
d2.t <- subset(d2,select=c("seqn","bpxsy1"))
d3.t <- subset(d3,select=c("seqn","bmxbmi"))
d4.t <- subset(d4,select=c("seqn","lbxgh"))
d5.t <- subset(d5,select=c("seqn","lbdtcsi"))
d <- merge(d1.t,d2.t)
d <- merge(d,d3.t)
d <- merge(d,d4.t)
d <- merge(d,d5.t)
# ====================================================================
# rename variables:
# RIAGENDR - Gender
# RIDAGEYR - Age in years at screening
# BPXSY1 - Systolic: Blood pres (1st rdg) mm Hg
# BMXBMI - Body Mass Index (kg/m**2)
# LBDTCSI - Total Cholesterol (mmol/L)
# LBXGH - Glycohemoglobin (%)
d$age <- d$ridageyr
d$sex <- d$riagendr
d$bp <- d$bpxsy1
d$bmi <- d$bmxbmi
d$HbA1C <- d$lbxgh
d$chol <- d$lbdtcsi
d$age[d$age<18] <- NA
# ====================================================================
# select complete cases:
dc <- cc(subset(d,select=c("age","sex","bmi","HbA1C","bp")))
# analysis:
summary(lm(bp ~ HbA1C + age + as.factor(sex), data=dc))
confint(lm(bp ~ HbA1C + age + as.factor(sex), data=dc))
summary(lm(bp ~ HbA1C + bmi + age + as.factor(sex), data=dc))
confint(lm(bp ~ HbA1C + bmi + age + as.factor(sex), data=dc))
# ====================================================================
# simulation of measurement error:
ref <- lm(bp ~ HbA1C + bmi + age + as.factor(sex), data=dc)$coef[2]
n.sim <- 1e3
perc.me.exp <- seq(0,.5,.1)
perc.me.conf<- seq(0,.5,.1)
scenarios <- expand.grid(perc.me.exp,perc.me.conf)
var.exp <- var(dc$HbA1C)
var.conf <- var(dc$bmi)
n <- dim(dc)[1]
beta.hat <- matrix(ncol=dim(scenarios)[1], nrow=n.sim)
for (k in 1:n.sim){
print(k)
set.seed(k)
for (i in 1:dim(scenarios)[1]){
var.me.exp <- var.exp*scenarios[i,1]/(1-scenarios[i,1])
var.me.conf <- var.conf*scenarios[i,2]/(1-scenarios[i,2])
dc$HbA1C.me <- dc$HbA1C + rnorm(dim(dc)[1], 0, sqrt(var.me.exp) )
dc$bmi.me <- dc$bmi + rnorm(dim(dc)[1], 0, sqrt(var.me.conf) )
beta.hat[k,i] <- lm(bp ~ HbA1C.me + age + bmi.me + as.factor(sex), data=dc)$coef[2]
}}
# ====================================================================
# create figure:
tot.mat <- cbind(100*scenarios,apply(beta.hat,2,mean))
colnames(tot.mat) <- c("me.exp","me.conf","estimate")
FIGURE <- ggplot(tot.mat, aes(me.exp, me.conf)) +
geom_tile(color="white",aes(fill = estimate)) +
geom_text(aes(label = round(estimate, 2))) +
scale_fill_gradient2(low="#D55E00",mid="white",high = "#56B4E9", midpoint=ref) +
labs(x=paste("% of total variance of HbA1c due to measurement error"),
y=paste("% of total variance of BMI due to measurement error")) +
coord_equal()+
scale_y_continuous(breaks=unique(tot.mat[,1]))+
scale_x_continuous(breaks=unique(tot.mat[,1]))+
theme(panel.background = element_rect(fill='white', colour='grey'),
plot.title=element_text(hjust=0),
axis.ticks=element_blank(),
axis.title=element_text(size=12),
axis.text=element_text(size=10),
legend.title=element_text(size=12),
legend.text=element_text(size=10))
FIGURE
# savePlot("Figure_STRATOS.tif", type="tif")
# ====================================================================
# END OF R CODE
# ===================================================================
102 changes: 102 additions & 0 deletions Kirsten/Assignment week 4/scripts/revised_script.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# ====================================================================
# R CODE
# small scale simulation study to investigate impact of measurement error
# measurement error on (continuous) exposure and/or (continuous) confounding variable
# ====================================================================
# ====================================================================
# libraries:
library(Hmisc)
library(mice)
library(tidyverse)
#setwd("")
# ====================================================================
# set working directory:
# setwd("")
# ====================================================================
# The data can be dowloaded in xpt form from https://wwwn.cdc.gov/nchs/nhanes/continuousnhanes/default.aspx?BeginYear=2015
# read data:
d1 <- sasxport.get("docs/DEMO_I.xpt")
d2 <- sasxport.get("docs/BPX_I.xpt")
d3 <- sasxport.get("docs/BMX_I.xpt")
d4 <- sasxport.get("docs/GHB_I.xpt")
d5 <- sasxport.get("docs/TCHOL_I.xpt")
d1.t <- subset(d1,select=c("seqn","riagendr","ridageyr"))
d2.t <- subset(d2,select=c("seqn","bpxsy1"))
d3.t <- subset(d3,select=c("seqn","bmxbmi"))
d4.t <- subset(d4,select=c("seqn","lbxgh"))
d5.t <- subset(d5,select=c("seqn","lbdtcsi"))
d <- merge(d1.t,d2.t)
d <- merge(d,d3.t)
d <- merge(d,d4.t)
d <- merge(d,d5.t)
# ====================================================================
# rename variables:
# RIAGENDR - Gender
# RIDAGEYR - Age in years at screening
# BPXSY1 - Systolic: Blood pres (1st rdg) mm Hg
# BMXBMI - Body Mass Index (kg/m**2)
# LBDTCSI - Total Cholesterol (mmol/L)
# LBXGH - Glycohemoglobin (%)
d$age <- d$ridageyr
d$sex <- d$riagendr
d$bp <- d$bpxsy1
d$bmi <- d$bmxbmi
d$HbA1C <- d$lbxgh
d$chol <- d$lbdtcsi
d$age[d$age<18] <- NA
# ====================================================================
# select complete cases:
dc <- cc(subset(d,select=c("age","sex","bmi","HbA1C","bp")))
# analysis:
summary(lm(bp ~ HbA1C + age + as.factor(sex), data=dc))
confint(lm(bp ~ HbA1C + age + as.factor(sex), data=dc))
summary(lm(bp ~ HbA1C + bmi + age + as.factor(sex), data=dc))
confint(lm(bp ~ HbA1C + bmi + age + as.factor(sex), data=dc))
# ====================================================================
# simulation of measurement error:
ref <- lm(bp ~ HbA1C + bmi + age + as.factor(sex), data=dc)$coef[2]
n.sim <- 1e3
perc.me.exp <- seq(0,.5,.1)
perc.me.conf<- seq(0,.5,.1)
scenarios <- expand.grid(perc.me.exp,perc.me.conf)
var.exp <- var(dc$HbA1C)
var.conf <- var(dc$bmi)
n <- dim(dc)[1]
beta.hat <- matrix(ncol=dim(scenarios)[1], nrow=n.sim)
for (k in 1:n.sim){
print(k)
set.seed(k)
for (i in 1:dim(scenarios)[1]){
var.me.exp <- var.exp*scenarios[i,1]/(1-scenarios[i,1])
var.me.conf <- var.conf*scenarios[i,2]/(1-scenarios[i,2])
dc$HbA1C.me <- dc$HbA1C + rnorm(dim(dc)[1], 0, sqrt(var.me.exp) )
dc$bmi.me <- dc$bmi + rnorm(dim(dc)[1], 0, sqrt(var.me.conf) )
beta.hat[k,i] <- lm(bp ~ HbA1C.me + age + bmi.me + as.factor(sex), data=dc)$coef[2]
}}
# ====================================================================
# create figure:
tot.mat <- cbind(100*scenarios,apply(beta.hat,2,mean))
colnames(tot.mat) <- c("me.exp","me.conf","estimate")
FIGURE <- ggplot(tot.mat, aes(me.exp, me.conf)) +
geom_tile(color="white",aes(fill = estimate)) +
geom_text(aes(label = round(estimate, 2))) +
scale_fill_gradient2(low="#D55E00",mid="white",high = "#56B4E9", midpoint=ref) +
labs(x=paste("% of total variance of HbA1c due to measurement error"),
y=paste("% of total variance of BMI due to measurement error")) +
coord_equal()+
scale_y_continuous(breaks=unique(tot.mat[,1]))+
scale_x_continuous(breaks=unique(tot.mat[,1]))+
theme(panel.background = element_rect(fill='white', colour='grey'),
plot.title=element_text(hjust=0),
axis.ticks=element_blank(),
axis.title=element_text(size=12),
axis.text=element_text(size=10),
legend.title=element_text(size=12),
legend.text=element_text(size=10))

tiff("results/Figure_STRATOS.tif", compression = "zip")
FIGURE
dev.off()
# ====================================================================
# END OF R CODE
# ===================================================================

0 comments on commit 4b3b05f

Please sign in to comment.