library(readxl)
library(spida2)
library(lattice)
tbi <- read_excel("TBI.xlsx")
tbi <- as.data.frame(tbi)
FIRST TASK ASSIGNED: I need to turn this data set from a wide file, having one row per subject, to a long file, having one row per occasion now.
As the professor stated, variables with underscores, i.e _1 or _2, specify the occasion the measurement was taken, however, looking at the variable names out of curiosity, I found the following:
names(tbi[116])
## [1] "fornix_crux_R_2"
There are some variables that have multiple underscores, which is why I am probably getting this error when I try to convert this data to long form. For variables with _k at the end (where k is some number), let’s change it to __k instead.
As another student pointed out on Piazza, an efficient way to do this is to use the gsub() function, which can be found in section 8 of the notes ‘Working with Data’ the professor provided to us. Doing this, I get this:
names(tbi) <- gsub("_(.)$", "__\\1", names(tbi))
Also, for the upcoming tasks, I’ll need the elapsed time since injury, which I’ll denote by ETSI, corresponding to each occasion a test was ran on the subject. As mentioned by the professor, date_1 (or date__1 here) will be the date of injury. I will measure the time in days elapsed from the date of injury using difftime().
ETSI1 <- rep(0, length(rownames(tbi)))
ETSI2 <- abs(difftime(tbi$date__1, tbi$date__2))
ETSI3 <- abs(difftime(tbi$date__1, tbi$date__3))
ETSI4 <- abs(difftime(tbi$date__1, tbi$date__4))
ETSI5 <- abs(difftime(tbi$date__1, tbi$date__5))
tbi$ETSI__1 <- ETSI1
tbi$ETSI__2 <- ETSI2
tbi$ETSI__3 <- ETSI3
tbi$ETSI__4 <- ETSI4
tbi$ETSI__5 <- ETSI5
Now let’s convert ‘tbi’ from wide to long. As the output will show, the scripts ’__’ have been removed, and the data now is per occasion rather than per subject. You can see this from the first ten columns for instance:
longtbi <- tolong(tbi, sep = "__")
names(longtbi)
## [1] "CC_TOT.pat" "DOB" "DOI"
## [4] "exclude" "from.volum" "group"
## [7] "id" "PMIQExclu" "sex"
## [10] "SubID" "testsession" "VBR.pat"
## [13] "wtarss" "yoe" "time"
## [16] "date" "total_brain_vol" "VBR"
## [19] "WM" "GM" "CSF"
## [22] "HC_excludecode" "CC_excludecode" "Excludecode"
## [25] "Comments_HC" "Comments_cc" "Comments"
## [28] "HPC_left_head" "HPC_left_body" "HPC_left_tail"
## [31] "HPC_L_TOT" "HPC_right_head" "HPC_right_body"
## [34] "HPC_right_tail" "HPC_R_TOT" "CC_post"
## [37] "CC_mid" "CC_ant" "CC_TOT"
## [40] "DPI.vol" "DPI.demog" "DPI"
## [43] "MPI" "days.post.injury" "months.post.injury"
## [46] "age_days" "age" "LowestGCSscore"
## [49] "LPTA" "Study" "tothrsCOG"
## [52] "TherapyTotInpt" "LOA" "thrpyhrs"
## [55] "from.demo" "fa_comments" "CC_genu"
## [58] "CC_body" "CC_splenium" "fornix_body"
## [61] "capsule_anterior_R" "capsule_anterior_L" "capsule_posterior_R"
## [64] "capsule_posterior_L" "fornix_crux_R" "fornix_crux_L"
## [67] "unciate_fasciculus_R" "unciate_fasciculus_L" "LAQ_phytot_laqscores"
## [70] "LAQ_medtot" "LAQ_soctot" "LAQ_cogtot"
## [73] "age_30" "HPC_LmR_TOT" "HPC_LmR_head"
## [76] "HPC_LmR_body" "HPC_LmR_tail" "years_post"
## [79] "idLabels" "ETSI" "Initials"
head(longtbi[, 1:10])
## CC_TOT.pat DOB DOI exclude from.volum group id PMIQExclu
## 312.1 -+-+-- 1980-02-06 2004-01-13 FALSE TRUE patient 312 1
## 313.1 -+-+-- 1945-05-15 2004-01-11 FALSE TRUE patient 313 1
## 315.1 -+-+-- 1957-02-04 2004-01-10 FALSE TRUE patient 315 0
## 316.1 -+-+-- 1962-06-12 2004-03-16 FALSE TRUE patient 316 1
## 317.1 -+-+-- 1951-07-19 2004-05-20 FALSE TRUE patient 317 0
## 318.1 -+-+-- 1982-07-02 2004-06-07 FALSE TRUE patient 318 1
## sex SubID
## 312.1 2 312
## 313.1 1 313
## 315.1 1 315
## 316.1 1 316
## 317.1 2 317
## 318.1 1 318
SECOND TASK ASSIGNED: Now I’ll plot the key variables given, VBR, CC_TOT, HPC_L_TOT, and HPC_R_TOT, against the elapsed time since injury, ETSI. I believe plotting the points per subject is appropriate here, since it would give a better picture of what’s going on rather than looking at things marginally.
Each subject has an ID stored in ‘id’ as seen earlier. I want to see how many subjects we have in total:
length(unique(tbi[, "id"]))
## [1] 155
We have 155 subjects in total, so plotting things per subject probably will be a little messy. Actually, after plotting everything, it looks clear enough to see what’s going on.
First, let’s plot VBR against ETSI now for the 155 patients. Looking at the data, there are NAs everywhere, so I chose to remove them and plot the (ETSI, VBR) pairs that are defined for each subject, hoping that it will be fairly representative of the big picture. Looking at the data, some subjects do not have VBR measurements at, say, date__3, so the data is not complete it appears, but I will go ahead and plot things this way.
To do this, I will create a new data frame with id, VBR, and ETSI, removing rows that contain any NAs.
d1 <- data.frame(id = longtbi$id, VBR = longtbi$VBR, ETSI = longtbi$ETSI)
d1 <- na.omit(d1)
xyplot(VBR ~ ETSI, data = d1, groups = id, type = 'b', xlab = "Elapsed Number of Days Since Injury",
ylab = "VBR", main = "Plot per Subject excluding NA Observations")
General impression: In general, it appears that VBR, or the ventricle to brain ratio, increases as the elapsed time since injury increases also (per subject). In other words, among subjects for which we have measurements on, it seems the brain volume generally shrinks as the number of days since injury increases.
There are a few isolated points here, which I think shows that not all subjects had measurements of VBR at all 5 occasions. The general pattern though is VBR mildly increases as ETSI increases, although for a few individuals it looks like to reaches a maximum and then stays flat. Looking at this plot, there also appears to be a lot of variability in the intercepts for each subject, and some variability in the slopes too, though it seems to be less pronounced.
Now let’s do the same for CC_TOT now:
d2 <- data.frame(id = longtbi$id, CC_TOT = longtbi$CC_TOT, ETSI = longtbi$ETSI)
d2 <- na.omit(d2)
xyplot(CC_TOT ~ ETSI, data = d2, groups = id, type = 'b', xlab = "Elapsed Number of Days Since Injury",
ylab = "CC_TOT", main = "Plot per Subject excluding NA Observations")
General impression: Here, in general, it appears that CC_TOT decreases as ETSI increases. There are a few exceptions, but for the most part, this seems to be the general pattern. Again, imagining drawing a regression line for each subject, it appears that there would be a lot of variability in the intercepts for each subject, and some noticeable variability in the slopes too.
Doing the same for HPC_L_TOT now:
d3 <- data.frame(id = longtbi$id, HPC_L_TOT = longtbi$HPC_L_TOT, ETSI = longtbi$ETSI)
d3 <- na.omit(d3)
xyplot(HPC_L_TOT ~ ETSI, data = d3, groups = id, type = 'b', xlab = "Elapsed Number of Days Since Injury",
ylab = "HPC_L_TOT", main = "Plot per Subject excluding NA Observations")
General impression: It appears in general, for each subject, HPC_L_TOT somewhat decreases at ETSI increases. Again, there appears to be a lot of variability in the intercepts, but here there also appears to be little variability in the slopes (they all look pretty close to being flat, in my view).
Lastly, doing the same for HPC_R_TOT now:
d4 <- data.frame(id = longtbi$id, HPC_R_TOT = longtbi$HPC_R_TOT, ETSI = longtbi$ETSI)
d4 <- na.omit(d4)
xyplot(HPC_R_TOT ~ ETSI, data = d4, groups = id, type = 'b', xlab = "Elapsed Number of Days Since Injury",
ylab = "HPC_R_TOT", main = "Plot per Subject excluding NA Observations")
General impression: It appears in general, for each subject, HPC_R_TOT somewhat decreases at ETSI increases. This plot looks like the previous one, in that there appears to be quite a bit of variability in the intercepts, but not so much for the slopes.
THIRD TASK ASSIGNED: Now I need to make a table showing how many observations are available on each subject.
t1 <- data.frame(table(longtbi$id))
names(t1) <- c("id", "Number of Observations")
t1
## id Number of Observations
## 1 312 5
## 2 313 5
## 3 315 5
## 4 316 5
## 5 317 5
## 6 318 5
## 7 319 5
## 8 320 5
## 9 321 5
## 10 322 5
## 11 323 5
## 12 324 5
## 13 325 5
## 14 327 5
## 15 328 5
## 16 329 5
## 17 330 5
## 18 331 5
## 19 333 5
## 20 335 5
## 21 337 5
## 22 338 5
## 23 339 5
## 24 340 5
## 25 341 5
## 26 343 5
## 27 344 5
## 28 345 5
## 29 346 5
## 30 347 5
## 31 348 5
## 32 349 5
## 33 350 5
## 34 352 5
## 35 353 5
## 36 354 5
## 37 355 5
## 38 357 5
## 39 358 5
## 40 359 5
## 41 361 5
## 42 362 5
## 43 363 5
## 44 364 5
## 45 365 5
## 46 366 5
## 47 367 5
## 48 368 5
## 49 369 5
## 50 370 5
## 51 371 5
## 52 372 5
## 53 374 5
## 54 376 5
## 55 377 5
## 56 378 5
## 57 379 5
## 58 380 5
## 59 382 5
## 60 389 5
## 61 392 5
## 62 393 5
## 63 394 5
## 64 395 5
## 65 397 5
## 66 399 5
## 67 401 5
## 68 402 5
## 69 403 5
## 70 404 5
## 71 406 5
## 72 407 5
## 73 408 5
## 74 409 5
## 75 410 5
## 76 411 5
## 77 412 5
## 78 413 5
## 79 415 5
## 80 416 5
## 81 418 5
## 82 419 5
## 83 420 5
## 84 421 5
## 85 422 5
## 86 423 5
## 87 424 5
## 88 426 5
## 89 427 5
## 90 429 5
## 91 430 5
## 92 432 5
## 93 433 5
## 94 435 5
## 95 436 5
## 96 437 5
## 97 438 5
## 98 439 5
## 99 440 5
## 100 441 5
## 101 444 5
## 102 445 5
## 103 446 5
## 104 447 5
## 105 448 5
## 106 449 5
## 107 450 5
## 108 451 5
## 109 452 5
## 110 453 5
## 111 455 5
## 112 457 5
## 113 459 5
## 114 461 5
## 115 462 5
## 116 463 5
## 117 464 5
## 118 465 5
## 119 468 5
## 120 469 5
## 121 470 5
## 122 471 5
## 123 473 5
## 124 c01 5
## 125 c02 5
## 126 c03 5
## 127 c04 5
## 128 c05 5
## 129 c08 5
## 130 c11 5
## 131 c12 5
## 132 c24 5
## 133 c25 5
## 134 c26 5
## 135 c27 5
## 136 c34 5
## 137 c35 5
## 138 c36 5
## 139 c37 5
## 140 c38 5
## 141 c39 5
## 142 c40 5
## 143 c41 5
## 144 c42 5
## 145 c43 5
## 146 c44 5
## 147 c45 5
## 148 c46 5
## 149 c47 5
## 150 c48 5
## 151 c49 5
## 152 c50 5
## 153 c51 5
## 154 c52 5
## 155 c53 5
From the table above, it appears we have five observations for each individual, but I believe it counts rows that have NAs in them too, so these are not necessarily complete observations. When looking through the original data frame ‘tbi’, I noticed there weren’t observations of, say, VBR, for certain subjects at certain dates beyond date__1 (the date of injury).