knitr::opts_chunk$set(
  collapse = TRUE,
  comment = ">",
  results = "asis",
  prompt = FALSE,
  cache = FALSE,
  message = FALSE,
  warning = FALSE,
  echo = TRUE
)
library(googlesheets4)
sheets_auth(
  email = gargle::gargle_oauth_email(),
  path = "../../../GLAD-Questionnaires/sheet_scripts/glad-dict.json",
  scopes = "https://www.googleapis.com/auth/spreadsheets",
  cache = gargle::gargle_oauth_cache(),
  use_oob = gargle::gargle_oob_default(),
  token = NULL
)

# googlesheets4 option, see https://gargle.r-lib.org/articles/non-interactive-auth.html
options(gargle_oauth_email = TRUE)

List of functions in the package

library(gladfunctions)

Clean Qualtrics exports

Raw data

raw_path <- "~/Data/GLAD/data_raw/"
dat_list <- GLAD_read(raw_path)

Clean data

clean_path <- "~/Data/GLAD/data_clean/"
GLAD_clean("ALL", dat_list, clean_path, limits = FALSE, rename = TRUE, format = "rds")
GLAD_clean("CIDID", dat_list, clean_path, limits = FALSE, rename = TRUE, format = "rds")

An empty text file with the questionnaire name will export all variables in that questionnaire.

GLAD_select(clean_path,
  export_path = "./Data-Request/",
  person = "Leo",
  c("DEM.txt", "PAD.txt"), format = "sav"
)

Read in data file and dictionary sheet

PAD <-
  readRDS(paste(clean_path, "rds_renamed/PAD_Renamed.rds", sep = "/"))

# `GLAD_sheet` is a vectorised function (vectorised functions take a vector
# and operate on all the items in a vector). This means that we can read in
# multiple sheets at once and store them in a list.

# We need to use [[1]] (with double square brackets) to extract the first
# element of an R list list.

sheet <- GLAD_sheet("PAD")[[1]]

Add derived variables to data

Please check sheet 'PTSD' on the dictionary sheet for a simple example and "AGP" for a more complicated one. Instructions are also available here.

PTSD <- readRDS(paste(clean_path, "rds_renamed/PTSD_Renamed.rds", sep = "/"))
sheet2 <- GLAD_sheet("PTSD")[[1]]

AGP <- readRDS(paste(clean_path, "rds_renamed/AGP_Renamed.rds", sep = "/"))
sheet3 <- GLAD_sheet("AGP")[[1]]

PTSD_withderived <- GLAD_derive(PTSD, sheet2)
AGP_withderived <- GLAD_derive(AGP, sheet3)

# These are the last few columns we just added
tail(colnames(PTSD_withderived), 2)
tail(colnames(AGP_withderived), 4)

Get variable descriptions

GLAD_getdescr(colnames(PAD)[5:8], sheet)

Special feature: as.numeric factor variables

We normally are unable to recode factor variables as numeric variable.

Applying as.numeric to the R built-in class factor only returns its internal integer representation.

unique(as.numeric(as.factor(PAD[["pad.anx_future_panic_attacks"]])))

However, the lfactor package preserves numeric values when recoding a numeric variable to factor. Therefore, we can use this package to recode our factor variables to numeric variables.

For example, for a Binary variable:

unique(as.numeric(PAD[["pad.anx_future_panic_attacks"]]))

Alternatively, you can use numeric copies of the factor variables. These numeric copies have been copied from the raw data and put into the cleaned data. These copy variables have the same names as the original variables but with "_numeric" at the end.

grep("numeric", colnames(PAD), value = T)

Plots

```r

Use fig.width to avoid the figure being truncated.

GLAD_plot(data = PAD, var = "Sex", googlesheet = sheet)

```r
GLAD_plot(
  data = PAD,
  var = "pad.anx_future_panic_attacks",
  googlesheet = sheet
)
continuous_plots <- GLAD_plot(
  data = PAD,
  var = "pad.frequency_panic_attacks",
  googlesheet = sheet,
  include_outlier = FALSE,
  limits = c(1, 50),
  binwidth = "FD"
)
continuous_plots$point
continuous_plots$hist
continuous_plots$density
continuous_plots$densitybysex
GLAD_plot(
  data = PAD,
  var = "pad.sweating",
  title = "PAD Screening",
  googlesheet = sheet
)

Quantile plot

GLAD_qplot(data = PAD, var = "pad.frequency_panic_attacks", googlesheet = sheet)

Variance tests by sex

These tests allow us to explore whether variances differ across sex.

GLAD_vartest(data = PAD, var = "pad.frequency_panic_attacks")

For quantile plot and variance test, I also have a version that extracts all the continuous variables and loops through them. Would that be more desirable?

Missingness

missingness <- GLAD_missing(data = PAD)
 ```

* Percentage of participants with no variable missing.

```r
missingness$percent
 ```

```r
missingness$descr
missingness$freq

The table below: a value of '1' indicates the percentage of participants missing for the corresponding variable.

print(missingness$table)
missingness$plot


lyh970817/gladfunctions documentation built on Sept. 19, 2021, 2:01 p.m.