-
Notifications
You must be signed in to change notification settings - Fork 1
/
0-Prepare-data.R
49 lines (38 loc) · 1.81 KB
/
0-Prepare-data.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Title: Prepare data for "PACE overlap" analysis
# Author: Ewan Carr
library(tidyverse)
library(fs)
library(haven)
library(here)
library(MplusAutomation)
# Load the raw data -----------------------------------------------------------
df <- read_sav("PACE database_25082013.sav")
items <- function(template, sequence) {
paste(sapply(sequence, function(x) {
gsub("#", x, template) }),
collapse = " ")
}
# Rename variables (replace dots with underscores) ----------------------------
names(df) <- gsub("\\.", "_", names(df))
# Select required variables (0 and 52 weeks) ----------------------------------
pick <- function(search) {
grep(search, names(df), value = TRUE)
}
required_variables <- c("pin", "trialarm",
pick("^cfq[q]*[0-9]+(_0|12|52F)"), # Chalder fatigue
pick("^pf\\d+_(0|12F|52F)"), # SF36 physical
pick("^wsaq\\d+_(0|12|52)"), # WSAQ
pick("^siq\\d+[r]*_(0|12|52)")) # Mediators
selected <- select(df, required_variables) %>%
select(-cfq1124F)
# Rename variables for consistency --------------------------------------------
names(selected) <- names(selected) %>%
str_replace("(pf|wsaq|siq|cfq)q*([0-9]*)[_]*(0|12|52)F*", "\\1\\2_\\3")
# Save version for future use -------------------------------------------------
save(selected, file = here("data", "clean", "pace.Rdata"))
# Prepare data for Mplus ------------------------------------------------------
model_statement <- prepareMplusData(selected,
here("data", "clean", "mplus.dat"),
inpfile = TRUE)
# Save version of raw data, for use elsewhere ---------------------------------
save(df, file = here("data", "clean", "pace_raw.Rdata"))