PAaSO/courses/covariates.Rmd

135 lines
2.6 KiB
Text

---
title: "Which covariates to adjust for? (DAGs)"
author: "AGDamsbo"
date: "`r Sys.Date()`"
output:
pdf_document: default
html_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Labels
```{r}
```
```{r}
library(ggdag)
library(ggplot2)
theme_set(theme_dag())
```
```{r}
ex_ca_dag <- dagify(ever_smoker ~ ami + socio_eco,
pad ~ ever_smoker,
edu ~ socio_eco,
ever_smoker ~ socio_eco,
pase_0 ~ ever_smoker + edu + pad,
labels = c(
"pase_0" = "Physical\n activity",
"ami" = "AMI",
"ever_smoker" = "Smoking",
"pad" = "PAD",
"socio_eco" = "Socio\n status",
"edu" = "Education"
),
# latent = "unhealthy",
exposure = "edu",
outcome = "pase_0"
)
ggdag(ex_ca_dag, text = FALSE, use_labels = "label")
```
```{r}
ggdag_paths(ex_ca_dag, text = FALSE, use_labels = "label", shadow = TRUE)
```
```{r}
ggdag_adjustment_set(ex_ca_dag, text = FALSE, use_labels = "label", shadow = TRUE)
```
# Excercise
```{r}
dag <- dagify(death ~ chd + HF + age + sex + wght,
HF~chd,
age~chd,
chd~sex,
HF~sex,
wght~sex,
# latent = "unhealthy",
exposure = "chd",
outcome = "death"
)
dag |> ggdag(text = TRUE)
dag |> ggdag_adjustment_set(text = TRUE, shadow = TRUE)
```
```{r}
dag <- dagify(ami~fat+age+sex+obm1+obp1+SESm1,
fat~sCm1+sex+sCp1+obm1+SESm1,
sCp1~sCm1+obm1,
sCm1~age,
obp1~obm1+SESm1,
obm1~SESm1,
SESm1~age,
# latent = "unhealthy",
exposure = "fat",
outcome = "ami"
)
dag |> ggdag(text = TRUE)
dag |> ggdag_adjustment_set(text = TRUE, shadow = TRUE)
```
```{r}
dag <- dagify(wgt~DM1+GA+smoke+par+BS,
GA~DM1+smoke+par+BS+SES,
smoke~DM1+par+SES,
par~DM1+SES,
BS~DM1+SES,
DM1~SES,
# latent = "unhealthy",
exposure = "DM1",
outcome = "wgt"
)
dag |> ggdag(text = TRUE,stylized = TRUE)
dag |> ggdag_adjustment_set(text = TRUE, shadow = TRUE,stylized = TRUE)
```
```{r}
testImplications <- function( covariance.matrix, sample.size ){
library(ggm)
tst <- function(i){ pcor.test( pcor(i,covariance.matrix), length(i)-2, sample.size )$pvalue }
tos <- function(i){ paste(i,collapse=" ") }
implications <- list(c("A","B"),
c("A","D","E"),
c("B","E"),
c("D","Z","A","B"),
c("D","Z","B","E"),
c("E","Z","A"))
data.frame( implication=unlist(lapply(implications,tos)),
pvalue=unlist( lapply( implications, tst ) ) )
}
library(dagitty)
testImplications()
```