







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!








Abstract This document details the importation of raw data, and the matching/merging of the various data sets into one dataset. This document constitutes a “literate program”: it contains computer code that is run in the process of creating this document, and the merged data set is created simultaneously. Thus, it is a reliable, reproducible description of the data set used in further analysis.
In this section we detail the importing of the raw data files, and provide brief structure summaries.
randomization This dataset gives information about the assignment of subjects to treatments. The randomization data were provided in a PDF file. I copied and pasted the information into a text file. We subtract a constant from each subject ID to remove the first 4 digits.
R> randomization = read.table("A6121123 A7.2-2.txt", header = TRUE) R> randomization$Subj = randomization$Subj - 10010000
R> str(randomization)
`data.frame': 24 obs. of 6 variables: $ Subj : num 1101 1102 1104 1106 1107 ... $ seq : Factor w/ 6 levels "ABC","ACB","BAC",..: 2 1 5 1 2 5 1 3 1 2 ... $ ActSeq : Factor w/ 6 levels "ABC","ACB","BAC",..: 2 1 5 1 2 5 1 3 1 2 ... $ FirstTrtDate: Factor w/ 6 levels "02FEB2005","14MAR2005",..: 1 1 6 5 4 4 2 5 5 6 ... $ FirstTrtTime: Factor w/ 4 levels "6:59","7:00",..: 3 4 2 1 2 2 4 2 4 2 ... $ AgeGrp : Factor w/ 2 levels "46-64","65+": 1 2 2 1 2 2 1 1 1 1 ...
driving The driving data were sent as an Excel file that was opened and re-saved as a comma-delimited file.
R> driving = read.csv("Pfizer All Data Sent Final.csv", na = ".") R> driving$Group = factor(driving$Group) R> driving$Week = factor(driving$Week) R> driving$Day = factor(driving$Day) R> driving$Time = factor(driving$Time)
R> str(driving)
`data.frame': 72 obs. of 32 variables: $ Center.Num : int 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 ... $ Screen.Num : Factor w/ 24 levels "1001-1101","1001-1102",..: 5 6 12 13 4 8 9 14 3 10 ... $ Rand : int 1 2 3 4 5 6 7 8 9 10 ... $ Group : Factor w/ 6 levels "1","2","3","4",..: 1 1 1 1 2 2 2 2 3 3 ... $ Week : Factor w/ 3 levels "1","2","3": 1 1 1 1 1 1 1 1 1 1 ... $ Order : Factor w/ 2 levels "NS","SN": 2 1 2 1 2 1 2 1 2 1 ... $ Day : Factor w/ 2 levels "4","5": 1 2 1 2 1 2 1 2 1 2 ...
$ Time : Factor w/ 2 levels "1","2": 1 1 2 2 1 1 2 2 1 1 ... $ P1.Phase : num -0.710 -0.916 -1.102 -1.008 -0. ... $ P1.RMS.Error : num 2.47 2.55 2.75 2.80 2. ... $ P1.Steer.Instab : num 2.537 1.271 2.472 0.683 3. ... $ P1.Lane.Exc : int 0 0 0 0 12 0 0 0 0 2 ... $ P1.Velo.Instab : num 8.43 6.69 7.60 7.84 4. ... $ P1.SD.of.Lat.Pos : num 0.478 0.367 0.294 0.311 0. ... $ P1.Fix.Rate : num 2.752 0.239 0.363 0.275 0. ... $ P1.Sacc.Rate : num 2.750 0.237 0.361 0.273 0. ... $ P1.Avg.Fix.Length : num 0.266 4.101 2.677 3.562 1. ... $ P1.Avg.Sacc.Length: num 0.0975 0.0824 0.0770 0.0745 0. ... $ P2.Steer.Instab : num 37.7 37.0 37.8 NA 37. ... $ P2.Lane.Exc : int 5 14 9 NA 32 43 10 6 8 9 ... $ P2.Velo.Instab : num 14.4 16.5 14.2 NA 19. ... $ P2.SD.of.Lat.Pos : num 1.11 1.86 1.45 NA 1. ... $ P2.Fix.Rate : num 2.726 0.424 0.877 NA 0. ... $ P2.Sacc.Rate : num 2.726 0.423 0.876 NA 0. ... $ P2.Avg.Fix.Length : num 0.273 2.251 1.059 NA 1. ... $ P2.Avg.Sacc.Length: num 0.0942 0.1082 0.0815 NA 0. ... $ FE.Accel.Rel : num NA NA NA NA NA NA NA NA NA NA ... $ FE.Min.TTC : num NA NA NA NA NA NA NA NA NA NA ... $ FE.Fix.Rate : num NA NA NA NA NA NA NA NA NA NA ... $ FE.Sacc.Rate : num NA NA NA NA NA NA NA NA NA NA ... $ FE.Avg.Fix.Length : num NA NA NA NA NA NA NA NA NA NA ... $ FE.Avg.Sacc.Length: num NA NA NA NA NA NA NA NA NA NA ...
npdata The neuropsychological data were provided as an SPSS SAV file. The dataset contains two missing- value codes: − 3 denotes a value that is missing because the protocol does not call for data to be collected there; − 9 is the code for an actual nonresponse. When we merge the datasets, the − 3 s will not be an issue at all. However, it is expedient to just set all the negative values to missing.
R> library(foreign) R> npdata = read.spss("np-Data.sav", to.data.frame = TRUE) R> for (i in 1:length(npdata)) npdata[[i]][npdata[[i]] < 0] = NA R> rm(i)
R> str(npdata)
`data.frame': 192 obs. of 67 variables: $ GROUP : num 4 3 3 4 6 5 4 1 1 5 ... $ ID : num 1128 1104 1115 1101 1139 ... $ SESSION : num 1 1 1 1 1 1 1 1 1 1 ... $ AGE : num 60 68 64 56 57 61 66 60 72 58 ... $ EDUYRS : num 16 12 18 17 18 14 13.5 19 16 12 ... $ HAND : Factor w/ 2 levels "right","left": 1 1 2 1 1 1 1 1 1 1 ... $ MFVPT : num 13 16 21 18 21 20 18 18 17 20 ... $ REYCOPY : num 34 30 30 34 34 34 36 31 29 35 ... $ COPYTIME: num 530 133 133 434 291 245 342 120 84 208 ... $ COPYPERC: atomic 17 14 8 17 17 17 17 14 17 17 ... ..- attr(, "value.labels")= Named num 17 .. ..- attr(, "names")= chr ">16%ile" $ REYRECAL: num 9.5 16 17 20.5 23.5 22 25 20 19 17. ... $ RECLTSCR: num 35 53 51 55 62 61 72 57 62 49 ... $ RECLPERC: num 7 62 54 69 88 86 99 76 88 46 ... $ BVMT1 : num 3 3 5 6 8 9 5 3 6 8 ... $ BVMT1TSC: num 38 40 49 52 61 68 49 38 56 62 ... $ BVMT1PER: num 12 16 46 58 86 96 46 12 73 88 ... $ BVMT2 : num 4 8 6 9 10 11 7 6 8 10 ... $ BVMT2TSC: num 31 52 41 54 58 65 47 40 53 59 ... $ BVMT2PER: num 3 58 18 66 79 93 38 16 62 82 ... $ BVMT3 : num 4 8 7 9 10 11 8 10 9 10 ... $ BVMT3TSC: atomic 24 47 41 48 53 60 46 55 52 54 ... ..- attr(, "value.labels")= Named num 19 .. ..- attr(, "names")= chr "<20tscr" $ BVMT3PER: atomic 1 38 18 42 62 84 34 69 58 66 ... ..- attr(, "value.labels")= Named num 0 .. ..- attr(, "names")= chr "<1%ile"
$ PreImp : int 8 7 8 11 13 9 1 1 4 2 ... $ PostDrowsy: int 13 NA 7 NA 12 NA NA 51 NA 33 ... $ PostImp : int 10 NA 8 NA 13 NA NA 48 NA 36 ...
The data are matched by first sorting to ensure that the subject IDs line up (and also confirming that the subject IDs match among the tables)
R> ord = order(randomization$Subj) R> randomization = randomization[ord, ] R> print(randomization$Subj)
[1] 1101 1102 1104 1106 1107 1108 1111 1113 1114 1115 1116 1117 1118 1119 1120 [16] 1125 1127 1128 1131 1132 1136 1139 1140 1143
R> ord = order(npdata$SESSION, npdata$ID) R> npdata = npdata[ord, ] R> print(npdata$ID[1:24])
[1] 1101 1102 1104 1106 1107 1108 1111 1113 1114 1115 1116 1117 1118 1119 1120 [16] 1125 1127 1128 1131 1132 1136 1139 1140 1143
R> ord = order(driving$Week, driving$Screen.Num) R> driving = driving[ord, ] R> print(driving$Screen.Num[1:24])
[1] 1001-1101 1001-1102 1001-1104 1001-1106 1001-1107 1001-1108 1001- [8] 1001-1113 1001-1114 1001-1115 1001-1116 1001-1117 1001-1118 1001- [15] 1001-1120 1001-1125 1001-1127 1001-1128 1001-1131 1001-1132 1001- [22] 1001-1139 1001-1140 1001- 24 Levels: 1001-1101 1001-1102 1001-1104 1001-1106 1001-1107 ... 1001-
R> ord = order(other$Day, other$Week, other$ScreenNo) R> other = other[ord, ] R> print(other$ScreenNo[1:24])
[1] 1001-1101 1001-1102 1001-1104 1001-1106 1001-1107 1001-1108 1001- [8] 1001-1113 1001-1114 1001-1115 1001-1116 1001-1117 1001-1118 1001- [15] 1001-1120 1001-1125 1001-1127 1001-1128 1001-1131 1001-1132 1001- [22] 1001-1139 1001-1140 1001- 24 Levels: 1001-1101 1001-1102 1001-1104 1001-1106 1001-1107 ... 1001-
R> rm(ord)
We now need to select appropriate entries from each variable and bring them into a merged data set. Note that the data tables have different numbers of observations For analysis purposes, we want the merged data set to have 3 × 24 = 72 rows, consisting of an observation for each subject for each week of the experiment. Thus, the driving data set serves as the basic structure.
R> merged = driving
Variables from the randomization table need to come in three times:
R> attach(randomization) R> merged$Seq = rep(seq, 3) R> s = as.character(seq) R> trt = c(substr(s, 1, 1), substr(s, 2, 2), substr(s, 3, 3)) R> merged$Trt = factor(trt, labels = c("Tol", "Oxy", "Pbo")) R> levels(merged$Seq) = c("TOP", "TPO", "OTP", "OPT", "PTO", "POT") R> rm(s, trt) R> detach()
To bring-in variables from the npdata table, the following functions are useful. forsess returns the indices if the rows in npdata that correspond to the given session(s).
R> forsess = function(s) { R+ which(!is.na(match(npdata$SESSION, s))) R+ }
addnp adds the given variable var from npdata to merged, using the specified sessions sess. When it is only one session, the 24 values are replicated 3 times and the given prefix is prepended to the variable name. Otherwise it is assumed (without checking!) that the defaut sessions are 1:7, and three variables are added: three copies of the variable for session 1 (prepended with Base for the baseline values), one for sessions 2, 4, and 6 (prepended with Pre because these are at the beginnings of the experimental weeks); and one for sessions 3, 5, and 7 (prepended with Post because these are at the ends of the experimental weeks).
R> addnp = function(var, sess = 1:7, prefix = "") { R+ var = as.character(substitute(var)) R+ x = npdata[[var]] R+ if (length(sess) == 1) { R+ x = x[forsess(sess)] R+ nm1 = paste(prefix, var, sep = "") R+ merged[[nm1]] <<- rep(x, 3) R+ cat(paste("Variable", nm1, "added\n")) R+ } R+ else { R+ base = x[forsess(1)] R+ x1 = x[forsess(c(2, 4, 6))] R+ x2 = x[forsess(c(3, 5, 7))] R+ nmbase = paste("Base", var, sep = "") R+ nm1 = paste("Pre", var, sep = "") R+ nm2 = paste("Post", var, sep = "") R+ merged[[nmbase]] <<- rep(base, 3) R+ merged[[nm1]] <<- x R+ merged[[nm2]] <<- x R+ cat(paste("Variables ", nmbase, ", ", nm1, ", and ", R+ nm2, " added\n", sep = "")) R+ } R+ }
So here we go with the needed neuropsychological variables:
Variables measured in sessions 1–
R> addnp(TMTASCR)
Variables BaseTMTASCR, PreTMTASCR, and PostTMTASCR added
R> addnp(TMTBSCR)
Variables BaseTMTBSCR, PreTMTBSCR, and PostTMTBSCR added
R> addnp(DIGITSYM)
Variables BaseDIGITSYM, PreDIGITSYM, and PostDIGITSYM added
R> addnp(LNSSCORE)
Variables BaseLNSSCORE, PreLNSSCORE, and PostLNSSCORE added
R> addnp(RAVLT1)
Variables BaseRAVLT1, PreRAVLT1, and PostRAVLT1 added
R> addnp(RAVLTSUM)
Variables BaseRAVLTSUM, PreRAVLTSUM, and PostRAVLTSUM added
R> addnp(RAVLTDLY)
Variables BaseRAVLTDLY, PreRAVLTDLY, and PostRAVLTDLY added
R> addnp(BUTTONS)
Variables BaseBUTTONS, PreBUTTONS, and PostBUTTONS added
R> addnp(CHOOSER)
Variables BaseCHOOSER, PreCHOOSER, and PostCHOOSER added
R> addnp(TAPPER)
Variables BaseTAPPER, PreTAPPER, and PostTAPPER added
The Pfizer protocol specifies two composite variables that we will label Memory and Speed, each an average of three standardized variables. For convenience, we define vectors with their names:
R> memvars = c("RAVLT1", "RAVLTSUM", "RAVLTDLY") R> spdvars = c("TMTASCR", "TMTBSCR", "CHOOSER")
Here is a utility function to create these variables. It standardizes with respect to the values in sessions wrt.sess, and outputs the values for the sessions in out.sess. If gain=TRUE, the values from session 1 are subtracted first, and we make sure that session 1 is excluded from wrt.sess.
R> comp = function(vars, wrt.sess = 1:7, out.sess = c(3, 5, 7), R+ gain = FALSE) { R+ if (gain) R+ wrt.sess = wrt.sess[wrt.sess != 1] R+ i.in = forsess(wrt.sess) R+ i.out = forsess(out.sess)
R+ std1 = function(v) { R+ x = npdata[, v] R+ if (gain) R+ x = x - rep(x[25:48], 8) R+ (x[i.out] - mean(x[i.in]))/sd(x[i.in]) R+ } R+ Z = sapply(vars, std1) R+ apply(Z, 1, mean) R+ } R> merged$BaseMemory = rep(comp(memvars, out = 1), 3) R> merged$PreMemory = comp(memvars, out = c(2, 4, 6)) R> merged$PostMemory = comp(memvars) R> merged$BaseSpeed = rep(comp(spdvars, out = 1), 3) R> merged$PreSpeed = comp(spdvars, out = c(2, 4, 6)) R> merged$PostSpeed = comp(spdvars)
The other table has 144 observations; after sorting, the first 72 are data collected on day 4 or each week, and the last 72 are data collected on day 5. The code below reflects this organization in extracting separate variables for days 4 and 5.
R> merged$PreDrowsy4 = other$PreDrowsy[1:72] R> merged$PreDrowsy5 = other$PreDrowsy[73:144] R> merged$PreImp4 = other$PreImp[1:72] R> merged$PreImp5 = other$PreImp[73:144] R> merged$PostDrowsy4 = other$PostDrowsy[1:72] R> merged$PostDrowsy5 = other$PostDrowsy[73:144] R> merged$PostImp4 = other$PostImp[1:72] R> merged$PostImp5 = other$PostImp[73:144]
In these variables, “pre” and “post” refer to data collected before and after the test on that day (neuropsy- chological on one day and driving on the other, depending on Order).
Here is a structure listing of the merged dataset.
R> str(merged)
`data.frame': 72 obs. of 91 variables: $ Center.Num : int 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 ... $ Screen.Num : Factor w/ 24 levels "1001-1101","1001-1102",..: 1 2 3 4 5 6 7 8 9 10 ... $ Rand : int 13 15 9 5 1 2 24 6 7 10 ... $ Group : Factor w/ 6 levels "1","2","3","4",..: 4 4 3 2 1 1 6 2 2 3 ... $ Week : Factor w/ 3 levels "1","2","3": 1 1 1 1 1 1 1 1 1 1 ... $ Order : Factor w/ 2 levels "NS","SN": 2 2 2 2 2 1 1 1 2 1 ... $ Day : Factor w/ 2 levels "4","5": 1 1 1 1 1 2 2 2 1 2 ... $ Time : Factor w/ 2 levels "1","2": 1 2 1 1 1 1 2 1 2 1 ... $ P1.Phase : num -0.993 -1.080 -1.353 -0.888 -0. ... $ P1.RMS.Error : num 2.39 2.91 2.58 2.29 2. ... $ P1.Steer.Instab : num 1.14 2.01 1.13 3.40 2. ... $ P1.Lane.Exc : int 0 0 0 12 0 0 0 0 0 2 ... $ P1.Velo.Instab : num 5.88 7.27 3.63 4.88 8. ... $ P1.SD.of.Lat.Pos : num 0.221 0.271 0.307 0.243 0. ...
Here are statistical summaries of all the variables
R> summary(merged)
We now save the data as both an R worksheet and a CSV file that can be imported into practically any statistical program. In the CSV file, missing values will be empty cells. R> save.image("merged.RData")