Phase 1

Phase 1 Analyses

Tasks to do:

  • Demographics
  • Composites (Afterlife Beliefs, FTP, SF-36, RCI)
  • Add social partner preferences
  • Make a correlation matrix of continuous variables

Read in dataset

For the code to run, the working directory should be set to the “Analyses” folder in box: (Box>Time Perception and Goals Study>Phase 1>Analyses)

phase1<-read.csv("./data/phase1.csv")

Make Composites

FTP

#Reverse score items 8, 9, and 10
phase1$FTP_8.r <- 8-phase1$FTP_8
phase1$FTP_9.r <- 8-phase1$FTP_9
phase1$FTP_10.r <- 8-phase1$FTP_10

phase1$FTP_mean <- rowMeans(
  as.data.frame(
    lapply(phase1[,c("FTP_1", "FTP_2","FTP_3","FTP_4","FTP_5",
                     "FTP_6", "FTP_7", "FTP_8.r", "FTP_9.r", "FTP_10.r")],
           function(x) as.numeric(as.character(x)))
  ), na.rm = TRUE
)

ART

#ART
phase1$ART_mean <- rowMeans(
  as.data.frame(
    lapply(phase1[,c("ART_1", "ART_2", "ART_3", "ART_4")],
           function(x) as.numeric(as.character(x)))
  ), na.rm = TRUE
)

RCI-10

phase1$RCI_mean <- rowMeans(
  as.data.frame(
    lapply(phase1[,c("RCI.10_1", "RCI.10_2","RCI.10_3","RCI.10_4","RCI.10_5",
                     "RCI.10_6", "RCI.10_7", "RCI.10_8", "RCI.10_9", "RCI.10_10")],
           function(x) as.numeric(as.character(x)))
  ), na.rm = TRUE
)

SF-36

Here’s the scoring guide: SF-36 Scoring Guide

A higher score represents better health

#STEP ONE. Recode items

#Actually, first I will rename them
library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
#the old column names are confusing...
colnames(phase1[c(46:80)])
 [1] "SF.36_1"    "SF.36_2"    "SF_3_1"     "SF_3_2"     "SF_3_3"    
 [6] "SF_3_4"     "SF_3_5"     "SF_3_6"     "SF_3_7"     "SF_3_8"    
[11] "SF_3_9"     "SF_3_10"    "SF.36_13_1" "SF.36_13_2" "SF.36_13_3"
[16] "SF.36_13_4" "SF.36_17_1" "SF.36_17_2" "SF.36_17_3" "SF.36_20"  
[21] "SF.36_21"   "SF.36_22"   "SF.36_23_1" "SF.36_23_2" "SF.36_23_3"
[26] "SF.36_23_4" "SF.36_23_5" "SF.36_23_6" "SF.36_23_7" "SF.36_23_8"
[31] "SF.36_23_9" "SF.36_32"   "SF.36_33_1" "SF.36_33_2" "SF.36_33_3"
#So we can rename them SF36_1-36

phase1 <- phase1 %>%
  rename_with(
    .fn = ~ paste0("SF36_", seq_along(.)),
    .cols = 46:80
  )
phase1[1,46:80]
  SF36_1 SF36_2 SF36_3 SF36_4 SF36_5 SF36_6 SF36_7 SF36_8 SF36_9 SF36_10
1      1      3      3      3      3      3      3      3      3       3
  SF36_11 SF36_12 SF36_13 SF36_14 SF36_15 SF36_16 SF36_17 SF36_18 SF36_19
1       3       3       2       2       2       2       2       1       1
  SF36_20 SF36_21 SF36_22 SF36_23 SF36_24 SF36_25 SF36_26 SF36_27 SF36_28
1       3       2       1       4      NA       5       5       4       4
  SF36_29 SF36_30 SF36_31 SF36_32 SF36_33 SF36_34 SF36_35
1       3       4       3       5       5       1       4
#NOTE: ITEM 36 IS MISSING

# Step 1 Item numbers 1, 2, 20, 22, 34, 36 get the following scores:
#1 turns to 100, 2 to 75, 3 to 50, 4 to 25, and 5 to 0

#Make a subset with all relevant columns
SF<- phase1[,46:80]

#Recode values according to the scoring guide
SF <- SF |> 
  mutate(across(c(1, 2, 20, 22, 34), ~ case_when(
    . == 1 ~ 100,
    . == 2 ~ 75,
    . == 3 ~ 50,
    . == 4 ~ 25,
    . == 5 ~ 0,
    TRUE ~ .  # Keep original value if it's not 1–5
  )))

SF <- SF |> 
  mutate(across(c(3, 4, 5, 6, 7, 8, 9, 10, 11, 12   ), ~ case_when(
    . == 1 ~ 0,
    . == 2 ~ 50,
    . == 3 ~ 100,
    TRUE ~ .  # Keep original value if it's not 1–5
  )))

SF <- SF |> 
  mutate(across(c(13, 14, 15, 16, 17, 18, 19    ), ~ case_when(
    . == 1 ~ 0,
    . == 2 ~ 100,
    TRUE ~ .  # Keep original value if it's not 1–5
  )))

SF <- SF |> 
  mutate(across(c(21, 23, 26, 27, 30), ~ case_when(
    . == 1 ~ 100,
    . == 2 ~ 80,
    . == 3 ~ 60,
    . == 4 ~ 40,
    . == 5 ~ 20,
    . == 6 ~ 0,
    TRUE ~ .  # Keep original value if it's not 1–5
  )))

SF <- SF |> 
  mutate(across(c(24, 25, 28, 29, 31), ~ case_when(
    . == 1 ~ 0,
    . == 2 ~ 20,
    . == 3 ~ 40,
    . == 4 ~ 60,
    . == 5 ~ 80,
    . == 6 ~ 100,
    TRUE ~ .  # Keep original value if it's not 1–5
  )))

SF <- SF |> 
  mutate(across(c(32, 33, 35), ~ case_when(
    . == 1 ~ 0,
    . == 2 ~ 25,
    . == 3 ~ 50,
    . == 4 ~ 75,
    . == 5 ~ 100,
    TRUE ~ .  # Keep original value if it's not 1–5
  )))

#STEP 2. Average items to form scales

#Physical functioning
SF$phys_fun <- rowMeans(SF[, 3:12], na.rm = TRUE)
phase1$phys_fun<-SF$phys_fun

#Role Limitations due to physical health
SF$rl_ph <- rowMeans(SF[, 13:16], na.rm = TRUE)
phase1$rl_ph<-SF$rl_ph

#Role limitations due to emotional problems
SF$rl_ep <- rowMeans(SF[, 17:19], na.rm = TRUE)
phase1$rl_ep<-SF$rl_ep

#Energy/fatigue
SF$ener <- rowMeans(SF[, c(23, 27, 29, 31)], na.rm = TRUE)
phase1$ener<-SF$ener

#Emotional well-being
SF$emo_wel <- rowMeans(SF[, c(24, 25, 26, 28, 30)], na.rm = TRUE)
phase1$emo_wel<-SF$emo_wel

#Social functioning
SF$soc_fun <- rowMeans(SF[, c(20,32)], na.rm = TRUE)
phase1$soc_fun<-SF$soc_fun

#Pain
SF$pain <- rowMeans(SF[, c(21,22)], na.rm = TRUE)
phase1$pain<-SF$pain

#General health
SF$gen_health <- rowMeans(SF[, c(1, 33, 34, 35, 36)], na.rm = TRUE)
phase1$gen_health<-SF$gen_health

Correlation Matrix

cor.matrix.vars <- data.frame(
  FTP_mean  = phase1$FTP_mean ,
  RCI_mean  = phase1$RCI_mean ,
  ART_mean = phase1$ART_mean,
  gen_health = phase1$gen_health ,
  pain = phase1$pain,
  soc_fun = phase1$soc_fun,
  emo_wel = phase1$emo_wel,
  rl_ep = phase1$rl_ep,
  rl_ph = phase1$rl_ph,
  phys_fun = phase1$phys_fun
)


cor.matrix.vars <- data.frame(lapply(cor.matrix.vars, function(x) as.numeric(as.character(x))))
cor_matrix <- cor(cor.matrix.vars, use = "pairwise.complete.obs")
print(cor_matrix)
              FTP_mean    RCI_mean    ART_mean gen_health       pain
FTP_mean    1.00000000  0.33276140 -0.06141829  0.4723321  0.4780242
RCI_mean    0.33276140  1.00000000 -0.02095957  0.3298692  0.2078803
ART_mean   -0.06141829 -0.02095957  1.00000000 -0.1814282 -0.1732114
gen_health  0.47233212  0.32986925 -0.18142816  1.0000000  0.6941485
pain        0.47802415  0.20788026 -0.17321140  0.6941485  1.0000000
soc_fun     0.39375802  0.48844551 -0.07437738  0.7039997  0.6661411
emo_wel     0.52066429  0.43462896  0.09552651  0.5803051  0.5380105
rl_ep       0.45768326  0.39362040  0.02125515  0.3669501  0.5913309
rl_ph       0.49352737  0.35000346 -0.43683404  0.7895801  0.8118421
phys_fun    0.34040006  0.28579402 -0.33794013  0.8462054  0.7185091
               soc_fun    emo_wel      rl_ep      rl_ph   phys_fun
FTP_mean    0.39375802 0.52066429 0.45768326  0.4935274  0.3404001
RCI_mean    0.48844551 0.43462896 0.39362040  0.3500035  0.2857940
ART_mean   -0.07437738 0.09552651 0.02125515 -0.4368340 -0.3379401
gen_health  0.70399966 0.58030506 0.36695006  0.7895801  0.8462054
pain        0.66614115 0.53801052 0.59133088  0.8118421  0.7185091
soc_fun     1.00000000 0.80682689 0.68989760  0.7243209  0.7041067
emo_wel     0.80682689 1.00000000 0.64264518  0.5444328  0.4893878
rl_ep       0.68989760 0.64264518 1.00000000  0.5818254  0.3792092
rl_ph       0.72432091 0.54443282 0.58182536  1.0000000  0.8284023
phys_fun    0.70410665 0.48938780 0.37920917  0.8284023  1.0000000
library(ggcorrplot)
Warning: package 'ggcorrplot' was built under R version 4.4.3
Loading required package: ggplot2
ggcorrplot::ggcorrplot(cor_matrix, lab = TRUE,
                       lab_size=3, type = "upper", 
                       colors = c("blue", "white", "red"), 
                       title = "Correlation Matrix", tl.cex = 11)

cor_pmat <- function(mat) {
  n <- ncol(mat)
  p.mat <- matrix(NA, n, n)
  colnames(p.mat) <- rownames(p.mat) <- colnames(mat)
  
  for (i in 1:n) {
    for (j in 1:n) {
      # Only test if i != j (optional), or else cor.test will throw warning about cor(x,x)
      if (i == j) {
        p.mat[i, j] <- 0
      } else {
        # Use complete.obs to handle missing values for each pair
        test <- cor.test(mat[,i], mat[,j], use = "pairwise.complete.obs")
        p.mat[i, j] <- test$p.value
      }
    }
  }
  return(p.mat)
}

p_matrix <- cor_pmat(cor.matrix.vars)
p_matrix
              FTP_mean   RCI_mean   ART_mean   gen_health         pain
FTP_mean   0.000000000 0.10409200 0.77055820 1.712065e-02 1.565369e-02
RCI_mean   0.104091999 0.00000000 0.92078638 1.073211e-01 3.186984e-01
ART_mean   0.770558199 0.92078638 0.00000000 3.854304e-01 4.076682e-01
gen_health 0.017120645 0.10732113 0.38543038 0.000000e+00 1.186044e-04
pain       0.015653692 0.31869841 0.40766823 1.186044e-04 0.000000e+00
soc_fun    0.051475207 0.01323517 0.72383531 8.595598e-05 2.778116e-04
emo_wel    0.007620475 0.02992252 0.64966996 2.357542e-03 5.535786e-03
rl_ep      0.021418958 0.05156409 0.91967280 7.117504e-02 1.851378e-03
rl_ph      0.012172728 0.08631662 0.02900759 2.698555e-06 8.388492e-07
phys_fun   0.095909949 0.16608370 0.09849060 9.868921e-08 5.223265e-05
                soc_fun      emo_wel        rl_ep        rl_ph     phys_fun
FTP_mean   5.147521e-02 7.620475e-03 0.0214189583 1.217273e-02 9.590995e-02
RCI_mean   1.323517e-02 2.992252e-02 0.0515640888 8.631662e-02 1.660837e-01
ART_mean   7.238353e-01 6.496700e-01 0.9196728048 2.900759e-02 9.849060e-02
gen_health 8.595598e-05 2.357542e-03 0.0711750422 2.698555e-06 9.868921e-08
pain       2.778116e-04 5.535786e-03 0.0018513778 8.388492e-07 5.223265e-05
soc_fun    0.000000e+00 1.105625e-06 0.0001357634 4.242025e-05 8.564995e-05
emo_wel    1.105625e-06 0.000000e+00 0.0005317524 4.897020e-03 1.303260e-02
rl_ep      1.357634e-04 5.317524e-04 0.0000000000 2.281416e-03 6.155413e-02
rl_ph      4.242025e-05 4.897020e-03 0.0022814156 0.000000e+00 3.170937e-07
phys_fun   8.564995e-05 1.303260e-02 0.0615541266 3.170937e-07 0.000000e+00
ggcorrplot(cor_matrix,
           p.mat = p_matrix,
           sig.level = 0.05,
           insig = "pch",
           pch = 15,
           pch.cex=14.23,
           pch.col = "white",
           lab = TRUE,
           lab_size = 3,
           type = "upper",
           colors = c("blue", "white", "red"),
           title = "Correlation Matrix",
           tl.cex = 11)