```{r setup, warning=FALSE, message=FALSE, include=FALSE}
# Load required packages
library(learnr)
library(xfun)
```

```{r machinelearningCausal_quiz1, echo=FALSE}
question("Q1. Say, we are exploring the association between diabetes (exposure) and CVD (outcome) using a dataset where data were collecting using simple random sampling. Age, sex, race, income, diet, smoking, and hypertension are the confounders. The name of the analytic datset is dat.analytic. How could you fit cross-validated Super Learner to estimate the propensity scores?",
  answer("fit.ps <- glm(cvd ~ age + sex + race + income + diet + smoking + hypertension, data = dat.analytic, family = binomial)", message = "Logistic regression is a single learner in this example."),
  answer("fit.ps <- SuperLearner(Y = dat.analytic$cvd, X = dat.analytic[, c('age', 'sex', 'race', 'income', 'diet', 'smoking', 'hypertension')], family = 'binomial', SL.library = c('SL.glm', 'SL.glmnet', 'SL.rpart'), verbose = FALSE, method = 'method.NNLS')", message = "In the propensity score model, we model the exposure."),
  answer("fit.ps <- CV.SuperLearner(Y = dat.analytic$cvd, X = dat.analytic$diabetes, family = 'binomial', SL.library = c('SL.glm', 'SL.glmnet', 'SL.rpart'), verbose = FALSE, V = 10, method = 'method.NNLS')", message = "In the propensity score model, we model the exposure."),
  answer("fit.ps <- SuperLearner(Y = dat.analytic$diabetes, X = dat.analytic[, c('age', 'sex', 'race', 'income', 'diet', 'smoking', 'hypertension')], family = 'binomial', SL.library = c('SL.glm', 'SL.glmnet', 'SL.rpart'), verbose = FALSE, method = 'method.NNLS')", correct = TRUE),
  allow_retry = TRUE,
  random_answer_order = TRUE
)
```

```{r machinelearningCausal_quiz2, echo=FALSE}
question("Q2. How could you extract the cross-validated propensity score from Q1?",
  answer("pscore <- cbind(fit.ps$SL.predict, fit.ps$library.predict)[,1]", correct = TRUE),
  answer("pscore <- cbind(fit.ps$SL.predict, fit.ps$library.predict)[1,]", message = "This will give us the predictions for the first row/participant"),
  answer("pscore <- cbind(fit.ps$SL.predict, fit.ps$library.predict)[,3]", message = "This will give us the predictions for the second learner, e.g., glmnet in our example."),
  answer("pscore <- cbind(fit.ps$SL.predict, fit.ps$library.predict)[,4]", message = "This will give us the predictions for the third learner, e.g., rpart in our example."),
  allow_retry = TRUE,
  random_answer_order = TRUE
)
```

```{r machinelearningCausal_quiz3, echo=FALSE}
question("Q3. Using the propensity score calculated in Q2, we match an exposed (having diabetes) with a control (no diabetes). The name of the matched dataset is 'dat.match'. What could be the outcome model in estimating the effect of diabetes on CVD?",
  answer("fit <- glm(cvd ~ diabetes, data = dat.match)"),
  answer("fit <- glm(cvd ~ diabetes, data = dat.match, family = binomial)", correct = TRUE),
  answer("fit <- glm(diabetes ~ cvd, design = dat.match, family = binomial)"),
  answer("fit <- glm(cvd ~ diabetes, design = dat.analytic, family = binomial)"),
  incorrect = "Check whether you are using the correct outcome formula and/or the correct dataset.",
  allow_retry = TRUE,
  random_answer_order = TRUE
)
```

```{r machinelearningCausal_quiz4, echo=FALSE}
question("Q4. Reconsider Q1 where data were collected using a complex survey design, e.g., NHANES 2017-18. The name of the survey features are: psu, strata, survey.weight. How could could fit your model in Question Q1 using the DuGoff et al. (2014) approach? ",
  answer("fit.ps <- SuperLearner(Y = dat.analytic$diabetes, X = dat.analytic[, c('age', 'sex', 'race', 'income', 'diet', 'smoking', 'hypertension')], family = 'binomial', SL.library = c('SL.glm', 'SL.glmnet', 'SL.rpart'), verbose = FALSE, method = 'method.NNLS')", message = "As per DuGoff et al. (2014) approach, you must use the survey features as covariates in the propensity score model."),
  answer("fit.ps <- CV.SuperLearner(Y = dat.analytic$cvd, X = dat.analytic$diabetes, family = 'binomial', SL.library = c('SL.glm', 'SL.glmnet', 'SL.rpart'), verbose = FALSE, V = 10, method = 'method.NNLS')", message = "In the propensity score model, we model the exposure."),
  answer("fit.ps <- SuperLearner(Y = dat.analytic$diabetes, X = dat.analytic[, c('age', 'sex', 'race', 'income', 'diet', 'smoking', 'hypertension', 'psu', 'strata', 'survey.weight')], family = 'binomial', SL.library = c('SL.glm', 'SL.glmnet', 'SL.rpart'), verbose = FALSE, method = 'method.NNLS')", correct = TRUE),
  answer("fit.ps <- SuperLearner(Y = dat.analytic$cvd, X = dat.analytic[, c('age', 'sex', 'race', 'income', 'diet', 'smoking', 'hypertension', 'psu', 'strata', 'survey.weight')], family = 'binomial', SL.library = c('SL.glm', 'SL.glmnet', 'SL.rpart'), verbose = FALSE, method = 'method.NNLS')", message = "In the propensity score model, we model the exposure."),
  allow_retry = TRUE,
  random_answer_order = TRUE
)
```
