## Log in again if you need to
```{r}
url <- "https://armadillo-demo.molgenis.net/"
token <- armadillo.get_token(url)
```

Now we create an object containing the token and other required login details:
```{r}
builder <- DSI::newDSLoginBuilder()
builder$append(server = "Groningen", url = url, token = token, table = "ds-workshop-24/data/CNSIM1", driver = "ArmadilloDriver", profile = "donkey")
builder$append(server = "Liverpool", url = url, token = token, table = "ds-workshop-24/data/CNSIM2", driver = "ArmadilloDriver", profile = "donkey")
builder$append(server = "Barcelona", url = url, token = token, table = "ds-workshop-24/data/CNSIM3", driver = "ArmadilloDriver", profile = "donkey")
logindata <- builder$build()
```

Now we can connect, referring to the login information we have just defined:
```{r}
conns <- DSI::datashield.login(logins = logindata, assign = TRUE, symbol = "data")
```

## Create a subset of the data only containing LAB_GLUC_ADJUSTED, PM_BMI_CONTINUOUS and sex
```{r}
ds.select("data", list(LAB_GLUC_ADJUSTED, PM_BMI_CONTINUOUS, GENDER), "data")
ds.colnames("data")
```

## Rename LAB_GLUC_ADJUSTED to "glucose",  PM_BMI_CONTINUOUS to "bmi" and GENDER to "sex"
```{r}
ds.rename("data", list(glucose = LAB_GLUC_ADJUSTED, bmi = PM_BMI_CONTINUOUS, sex = GENDER), "data")
ds.colnames("data")
```

3. Calculate the mean values for "bmi" and "glucose", and counts for "sex"

Using base DataSHIELD we can do:
```{r}
ds.mean("data$glucose")
ds.mean("data$bmi")
ds.table("data$sex")
```

Or using dsHelper:
```{r}
stats <- dh.getStats(
  df = "data",
  vars = c("glucose", "bmi", "sex")
)

stats
````

## Make a scatter plot for the same two variables
```{r}
ds.scatterPlot(x = "data$glucose", y = "data$bmi")
```

## Run a two-stage meta-analysis to see if bmi is associated with glucose adusting for sex
```{r}
meta_out <- ds.glmSLMA(
  formula = "bmi ~ glucose + sex",
  dataName = "data",
  family = "gaussian",
  newobj = "practical")
```

What are the coefficients for `glucose` for each cohort?

## Using base DataSHIELD
```{r}
meta_out$betamatrix.valid
```

## Or using dsHelper
```{r}
dh.lmTab(model = meta_out, type = "glm_slma", direction = "wide", ci_format = "paste", 
         coh_names = c("Groningen", "Liverpool", "Barcelona"), digits = 4)
```

## Plot the findings from your meta-analysis in a forest plot.
```{r}
ds.forestplot(
  mod = meta_out,
  variable = "glucose"
)
```

## Bonus: Make a scatter plot showing the residuals of your model
```{r}
ds.glmPredict(glmname = "practical", newobj = "practical.prediction")
ds.cbind(c('data$bmi', 'data$glucose'), newobj='practical_vars')
ds.completeCases('practical_vars', newobj='practical_vars.complete')

glmslma <- ds.glmSLMA(
  formula = "practical_vars.complete$bmi ~ practical_vars.complete$glucose",
  family = "gaussian",
  newobj = "practical.obj")

ds.make(
  toAssign = paste0(
    "(", glmslma$SLMA.pooled.ests.matrix[1,1],")+(",
    glmslma$SLMA.pooled.ests.matrix[2,1], "*practical_vars.complete$glucose)"),
  newobj = "practical_predicted")

ds.make(
  toAssign = "practical_vars.complete$bmi - practical_predicted",
  newobj = "residuals")

ds.scatterPlot('practical_predicted', "residuals")
```
