R/AXCPT.Descriptives.R

#' Get Descriptives for AX-CPT
#'
#' More detailed description
#'
#' @param () None needed right
#'
#' @return I wonder what this does
#'
#' @examples
#' Run Descript() function; then follow instruction in Console
#'
#' @export

# Create dataset based on prompt input
descript <- function(data)
{
  if(missing(data)){dat <- read.csv("https://www.dropbox.com/s/tp0bfomoprwxh7u/AXCPTsample.csv?dl=1")
  print("!!! USING SAMPLE DATA !!!")}
  else {dat <- data}
  as.factor(assign("phaseInp", readline(prompt="Descriptives; Enter test(default) or retest:  "), .GlobalEnv))
  if(phaseInp == ""){as.factor(assign("phaseInp", "test"))}
  {
    #Only selecting subjects with all data && appropriate columns
    datSCol <- dat[c("ID","trialType", "probeCorrect", "probeReacTime", "session", "phase")]
    tt <- table(datSCol$ID)
    datComp <- subset(datSCol, ID %in% names(tt[tt == 1296]))
    datComp$ID <- factor(datComp$ID)
    IDlength <- as.numeric(length(levels(datComp$ID)))
    x2 <- subset(datComp, phase == phaseInp)

    ############################################################################################################################
    ##########descriptives######################################################################################################
    ############################################################################################################################

    #Separating the columns
    x2.BL <- subset(x2, session == "baseline")[c(1:4)]
    x2.PR <- subset(x2, session == "proactive")[c(1:4)]
    x2.RE <- subset(x2, session == "reactive")[c(1:4)]

    #Create Empty DF, enter text
    df <- setNames(data.frame(matrix(ncol = 5, nrow = 15)), c("AX-CPT", "trialType", "Baseline", "Proactive", "Reactive"))
    df[1, 1] = "Average Err(sd)"; df[7, 1] = "Average RT(sd)"; df[11, 1] = "PBI-errors"; df[12, 1] = "PBI-RTs"
    df[13, 1] = "PBI-comp"; df[14, 1] = "d'-context"; df[15, 1] = "A-cue bias"
    df[1, 2] = "AX"; df[2, 2] = "AY"; df[3, 2] = "BX"; df[4, 2] = "BY"; df[5, 2] = "Ang";
    df[6, 2] = "Bng"; df[7, 2] = "AX"; df[8, 2] = "AY"; df[9, 2] = "BX"; df[10, 2] = "BY";
    #Empty frames
    df[2,1] = ""; df[3,1] = "";df[4,1] = "";df[5,1] = "";df[6,1] = ""; df[8,1] = ""; df[9,1] = "";df[10,1] = "";
    df[11,2] = ""; df[12,2] = "";df[13,2] = "";df[14,2] = "";df[15,2] = "";
    #Fill in values
    #Baseline
    df[1, 3] = paste0(round(mean(subset(1 - x2.BL$probeCorrect, x2.BL$trialType == "AX")),3),"(",
                      round(sd(subset(x2.BL$probeCorrect, x2.BL$trialType == "AX")),3),")")
    df[2, 3] = paste0(round(mean(subset(1 - x2.BL$probeCorrect, x2.BL$trialType == "AY")),3),"(",
                      round(sd(subset(x2.BL$probeCorrect, x2.BL$trialType == "AY")),3),")")
    df[3, 3] = paste0(round(mean(subset(1 - x2.BL$probeCorrect, x2.BL$trialType == "BX")),3),"(",
                      round(sd(subset(x2.BL$probeCorrect, x2.BL$trialType == "BX")),3),")")
    df[4, 3] = paste0(round(mean(subset(1 - x2.BL$probeCorrect, x2.BL$trialType == "BY")),3),"(",
                      round(sd(subset(x2.BL$probeCorrect, x2.BL$trialType == "BY")),3),")")
    df[5, 3] = paste0(round(mean(subset(1 - x2.BL$probeCorrect, x2.BL$trialType == "Ang")),3),"(",
                      round(sd(subset(x2.BL$probeCorrect, x2.BL$trialType == "Ang")),3),")")
    df[6, 3] = paste0(round(mean(subset(1 - x2.BL$probeCorrect, x2.BL$trialType == "Bng")),3),"(",
                      round(sd(subset(x2.BL$probeCorrect, x2.BL$trialType == "Bng")),3),")")
    df[7, 3] = paste0(round(mean(subset(x2.BL$probeReacTime, x2.BL$trialType == "AX")),0),"(",
                      round(sd(subset(x2.BL$probeReacTime, x2.BL$trialType == "AX")),0),")")
    df[8, 3] = paste0(round(mean(subset(x2.BL$probeReacTime, x2.BL$trialType == "AY")),0),"(",
                      round(sd(subset(x2.BL$probeReacTime, x2.BL$trialType == "AY")),0),")")
    df[9, 3] = paste0(round(mean(subset(x2.BL$probeReacTime, x2.BL$trialType == "BX")),0),"(",
                      round(sd(subset(x2.BL$probeReacTime, x2.BL$trialType == "BX")),0),")")
    df[10, 3] =paste0(round(mean(subset(x2.BL$probeReacTime, x2.BL$trialType == "BY")),0),"(",
                      round(sd(subset(x2.BL$probeReacTime, x2.BL$trialType == "BY")),0),")")

    #Proactive
    df[1, 4] = paste0(round(mean(subset(1 - x2.PR$probeCorrect, x2.PR$trialType == "AX")),3),"(",
                      round(sd(subset(x2.PR$probeCorrect, x2.PR$trialType == "AX")),3),")")
    df[2, 4] = paste0(round(mean(subset(1 - x2.PR$probeCorrect, x2.PR$trialType == "AY")),3),"(",
                      round(sd(subset(x2.PR$probeCorrect, x2.PR$trialType == "AY")),3),")")
    df[3, 4] = paste0(round(mean(subset(1 - x2.PR$probeCorrect, x2.PR$trialType == "BX")),3),"(",
                      round(sd(subset(x2.PR$probeCorrect, x2.PR$trialType == "BX")),3),")")
    df[4, 4] = paste0(round(mean(subset(1 - x2.PR$probeCorrect, x2.PR$trialType == "BY")),3),"(",
                      round(sd(subset(x2.PR$probeCorrect, x2.PR$trialType == "BY")),3),")")
    df[5, 4] = paste0(round(mean(subset(1 - x2.PR$probeCorrect, x2.PR$trialType == "Ang")),3),"(",
                      round(sd(subset(x2.PR$probeCorrect, x2.PR$trialType == "Ang")),3),")")
    df[6, 4] = paste0(round(mean(subset(1 - x2.PR$probeCorrect, x2.PR$trialType == "Bng")),3),"(",
                      round(sd(subset(x2.PR$probeCorrect, x2.PR$trialType == "Bng")),3),")")
    df[7, 4] = paste0(round(mean(subset(x2.PR$probeReacTime, x2.PR$trialType == "AX")),0),"(",
                      round(sd(subset(x2.PR$probeReacTime, x2.PR$trialType == "AX")),0),")")
    df[8, 4] = paste0(round(mean(subset(x2.PR$probeReacTime, x2.PR$trialType == "AY")),0),"(",
                      round(sd(subset(x2.PR$probeReacTime, x2.PR$trialType == "AY")),0),")")
    df[9, 4] = paste0(round(mean(subset(x2.PR$probeReacTime, x2.PR$trialType == "BX")),0),"(",
                      round(sd(subset(x2.PR$probeReacTime, x2.PR$trialType == "BX")),0),")")
    df[10, 4] =paste0(round(mean(subset(x2.PR$probeReacTime, x2.PR$trialType == "BY")),0),"(",
                      round(sd(subset(x2.PR$probeReacTime, x2.PR$trialType == "BY")),0),")")
    #Reactive
    df[1, 5] = paste0(round(mean(subset(1 - x2.RE$probeCorrect, x2.RE$trialType == "AX")),3),"(",
                      round(sd(subset(x2.RE$probeCorrect, x2.RE$trialType == "AX")),3),")")
    df[2, 5] = paste0(round(mean(subset(1 - x2.RE$probeCorrect, x2.RE$trialType == "AY")),3),"(",
                      round(sd(subset(x2.RE$probeCorrect, x2.RE$trialType == "AY")),3),")")
    df[3, 5] = paste0(round(mean(subset(1 - x2.RE$probeCorrect, x2.RE$trialType == "BX")),3),"(",
                      round(sd(subset(x2.RE$probeCorrect, x2.RE$trialType == "BX")),3),")")
    df[4, 5] = paste0(round(mean(subset(1 - x2.RE$probeCorrect, x2.RE$trialType == "BY")),3),"(",
                      round(sd(subset(x2.RE$probeCorrect, x2.RE$trialType == "BY")),3),")")
    df[5, 5] = paste0(round(mean(subset(1 - x2.RE$probeCorrect, x2.RE$trialType == "Ang")),3),"(",
                      round(sd(subset(x2.RE$probeCorrect, x2.RE$trialType == "Ang")),3),")")
    df[6, 5] = paste0(round(mean(subset(1 - x2.RE$probeCorrect, x2.RE$trialType == "Bng")),3),"(",
                      round(sd(subset(x2.RE$probeCorrect, x2.RE$trialType == "Bng")),3),")")
    df[7, 5] = paste0(round(mean(subset(x2.RE$probeReacTime, x2.RE$trialType == "AX")),0),"(",
                      round(sd(subset(x2.RE$probeReacTime, x2.RE$trialType == "AX")),0),")")
    df[8, 5] = paste0(round(mean(subset(x2.RE$probeReacTime, x2.RE$trialType == "AY")),0),"(",
                      round(sd(subset(x2.RE$probeReacTime, x2.RE$trialType == "AY")),0),")")
    df[9, 5] = paste0(round(mean(subset(x2.RE$probeReacTime, x2.RE$trialType == "BX")),0),"(",
                      round(sd(subset(x2.RE$probeReacTime, x2.RE$trialType == "BX")),0),")")
    df[10, 5] =paste0(round(mean(subset(x2.RE$probeReacTime, x2.RE$trialType == "BY")),0),"(",
                      round(sd(subset(x2.RE$probeReacTime, x2.RE$trialType == "BY")),0),")")

    ############################################################################################################################
    #### d-prime, A-cue, and PBI ###############################################################################################
    ############################################################################################################################

    #PBI, d-prime, and A-cue Err indices only require a log-linear correction (LLC):
    #Total Trial Length per trialType && session

    BLAX.tTot <- as.numeric(length(subset(x2.BL$probeCorrect, x2.BL$trialType == "AX" & x2.BL$ID == levels(x2.BL$ID)[1] )))
    BLAY.tTot <- as.numeric(length(subset(x2.BL$probeCorrect, x2.BL$trialType == "AY" & x2.BL$ID == levels(x2.BL$ID)[1] )))
    BLBX.tTot <- as.numeric(length(subset(x2.BL$probeCorrect, x2.BL$trialType == "BX" & x2.BL$ID == levels(x2.BL$ID)[1] )))

    PRAX.tTot <- as.numeric(length(subset(x2.PR$probeCorrect, x2.PR$trialType == "AX" & x2.PR$ID == levels(x2.PR$ID)[1] )))
    PRAY.tTot <- as.numeric(length(subset(x2.PR$probeCorrect, x2.PR$trialType == "AY" & x2.PR$ID == levels(x2.PR$ID)[1] )))
    PRBX.tTot <- as.numeric(length(subset(x2.PR$probeCorrect, x2.PR$trialType == "BX" & x2.PR$ID == levels(x2.PR$ID)[1] )))

    REAX.tTot <- as.numeric(length(subset(x2.RE$probeCorrect, x2.RE$trialType == "AX" & x2.RE$ID == levels(x2.RE$ID)[1] )))
    REAY.tTot <- as.numeric(length(subset(x2.RE$probeCorrect, x2.RE$trialType == "AY" & x2.RE$ID == levels(x2.RE$ID)[1] )))
    REBX.tTot <- as.numeric(length(subset(x2.RE$probeCorrect, x2.RE$trialType == "BX" & x2.RE$ID == levels(x2.RE$ID)[1] )))

    trialSelect <- c("AX", "AY", "BX")
    x3 <- subset(x2, trialType %in% trialSelect)[1:5]
    x3.agg <- setNames(aggregate(x3[,3:4],
                                 list(x3$ID, x3$trialType, x3$session), mean),
                       c("ID", "trialType", "session", "probeCorrect", "probeReacTime"))

    #split by session
    x4.AX <- subset(x3.agg, trialType == "AX")
    x4.AY <- subset(x3.agg, trialType == "AY")
    x4.BX <- subset(x3.agg, trialType == "BX")

    #apply log-linear correction (# of err + .5)/(# trials +1)
    x4.AX[4] <- x4.AX[4]*BLAX.tTot
    x4.AX[4] <- BLAX.tTot - x4.AX[4]
    x4.AX[4] <- x4.AX[4] + 0.5
    x4.AX[4] <- (x4.AX[4])/(BLAX.tTot + 1)

    x4.AY[4] <- x4.AY[4]*BLAY.tTot
    x4.AY[4] <- BLAY.tTot - x4.AY[4]
    x4.AY[4] <- x4.AY[4] + 0.5
    x4.AY[4] <- (x4.AY[4])/(BLAY.tTot + 1)

    x4.BX[4] <- x4.BX[4]*BLBX.tTot
    x4.BX[4] <- BLBX.tTot - x4.BX[4]
    x4.BX[4] <- x4.BX[4] + 0.5
    x4.BX[4] <- (x4.BX[4])/(BLBX.tTot + 1)

    #Combine log-linear corrected
    x5 <- rbind(x4.AX, x4.AY, x4.BX)

    ############################################################################################################################
    ##### PBI errors and rt ####################################################################################################
    ############################################################################################################################
    #PBI errors
    x6.BLPBIe <- as.data.frame(x5[1:IDlength,1])
    x6.BLPBIe <- cbind(x6.BLPBIe, subset(x5, trialType == "AY" & session == "baseline")[4])
    x6.BLPBIe <- setNames(x6.BLPBIe, c("ID", "AYPC"))
    x6.BLPBIe <- cbind(x6.BLPBIe, subset(x5, trialType == "BX" & session == "baseline")[4])
    x6.BLPBIe <- setNames(x6.BLPBIe, c("ID", "AYPC", "BXPC"))
    x6.BLPBIe <- cbind(x6.BLPBIe, ((x6.BLPBIe$AYPC - x6.BLPBIe$BXPC)/(x6.BLPBIe$AYPC+x6.BLPBIe$BXPC)))
    x6.BLPBIe <- setNames(x6.BLPBIe, c("ID", "AYPC", "BXPC", "PBI"))

    x6.PRPBIe <- as.data.frame(x5[1:IDlength,1])
    x6.PRPBIe <- cbind(x6.PRPBIe, subset(x5, trialType == "AY" & session == "proactive")[4])
    x6.PRPBIe <- setNames(x6.PRPBIe, c("ID", "AYPC"))
    x6.PRPBIe <- cbind(x6.PRPBIe, subset(x5, trialType == "BX" & session == "proactive")[4])
    x6.PRPBIe <- setNames(x6.PRPBIe, c("ID", "AYPC", "BXPC"))
    x6.PRPBIe <- cbind(x6.PRPBIe, ((x6.PRPBIe$AYPC - x6.PRPBIe$BXPC)/(x6.PRPBIe$AYPC+x6.PRPBIe$BXPC)))
    x6.PRPBIe <- setNames(x6.PRPBIe, c("ID", "AYPC", "BXPC", "PBI"))

    x6.REPBIe <- as.data.frame(x5[1:IDlength,1])
    x6.REPBIe <- cbind(x6.REPBIe, subset(x5, trialType == "AY" & session == "reactive")[4])
    x6.REPBIe <- setNames(x6.REPBIe, c("ID", "AYPC"))
    x6.REPBIe <- cbind(x6.REPBIe, subset(x5, trialType == "BX" & session == "reactive")[4])
    x6.REPBIe <- setNames(x6.REPBIe, c("ID", "AYPC", "BXPC"))
    x6.REPBIe <- cbind(x6.REPBIe, ((x6.REPBIe$AYPC - x6.REPBIe$BXPC)/(x6.REPBIe$AYPC+x6.REPBIe$BXPC)))
    x6.REPBIe <- setNames(x6.REPBIe, c("ID", "AYPC", "BXPC", "PBI"))

    #PBI rt
    x6.BLPBIr <- as.data.frame(x5[1:IDlength,1])
    x6.BLPBIr <- cbind(x6.BLPBIr, subset(x5, trialType == "AY" & session == "baseline")[5])
    x6.BLPBIr <- setNames(x6.BLPBIr, c("ID", "AYPC"))
    x6.BLPBIr <- cbind(x6.BLPBIr, subset(x5, trialType == "BX" & session == "baseline")[5])
    x6.BLPBIr <- setNames(x6.BLPBIr, c("ID", "AYPC", "BXPC"))
    x6.BLPBIr <- cbind(x6.BLPBIr, ((x6.BLPBIr$AYPC - x6.BLPBIr$BXPC)/(x6.BLPBIr$AYPC+x6.BLPBIr$BXPC)))
    x6.BLPBIr <- setNames(x6.BLPBIr, c("ID", "AYPC", "BXPC", "PBI"))

    x6.PRPBIr <- as.data.frame(x5[1:IDlength,1])
    x6.PRPBIr <- cbind(x6.PRPBIr, subset(x5, trialType == "AY" & session == "proactive")[5])
    x6.PRPBIr <- setNames(x6.PRPBIr, c("ID", "AYPC"))
    x6.PRPBIr <- cbind(x6.PRPBIr, subset(x5, trialType == "BX" & session == "proactive")[5])
    x6.PRPBIr <- setNames(x6.PRPBIr, c("ID", "AYPC", "BXPC"))
    x6.PRPBIr <- cbind(x6.PRPBIr, ((x6.PRPBIr$AYPC - x6.PRPBIr$BXPC)/(x6.PRPBIr$AYPC+x6.PRPBIr$BXPC)))
    x6.PRPBIr <- setNames(x6.PRPBIr, c("ID", "AYPC", "BXPC", "PBI"))

    x6.REPBIr <- as.data.frame(x5[1:IDlength,1])
    x6.REPBIr <- cbind(x6.REPBIr, subset(x5, trialType == "AY" & session == "reactive")[5])
    x6.REPBIr <- setNames(x6.REPBIr, c("ID", "AYPC"))
    x6.REPBIr <- cbind(x6.REPBIr, subset(x5, trialType == "BX" & session == "reactive")[5])
    x6.REPBIr <- setNames(x6.REPBIr, c("ID", "AYPC", "BXPC"))
    x6.REPBIr <- cbind(x6.REPBIr, ((x6.REPBIr$AYPC - x6.REPBIr$BXPC)/(x6.REPBIr$AYPC+x6.REPBIr$BXPC)))
    x6.REPBIr <- setNames(x6.REPBIr, c("ID", "AYPC", "BXPC", "PBI"))

    ############################################################################################################################
    ##### d-prime ##############################################################################################################
    ############################################################################################################################

    #Fill in PBI, d-prime, A-cue
    df[11, 3] =paste0(round(mean(x6.BLPBIe$PBI),2),"(",round((sd(x6.BLPBIe$PBI)),2),")")
    df[11, 4] =paste0(round(mean(x6.PRPBIe$PBI),2),"(",round((sd(x6.PRPBIe$PBI)),2),")")
    df[11, 5] =paste0(round(mean(x6.REPBIe$PBI),2),"(",round((sd(x6.REPBIe$PBI)),2),")")
    df[12, 3] =paste0(round(mean(x6.BLPBIr$PBI),2),"(",round((sd(x6.BLPBIr$PBI)),2),")")
    df[12, 4] =paste0(round(mean(x6.PRPBIr$PBI),2),"(",round((sd(x6.PRPBIr$PBI)),2),")")
    df[12, 5] =paste0(round(mean(x6.REPBIr$PBI),2),"(",round((sd(x6.REPBIr$PBI)),2),")")
    df[13, 3] =paste0(round((mean(x6.BLPBIe$PBI) + mean(x6.BLPBIr$PBI)),2),"(",round(sd(x6.BLPBIe$PBI + x6.BLPBIr$PBI),2),")")
    df[13, 4] =paste0(round((mean(x6.PRPBIe$PBI) + mean(x6.PRPBIr$PBI)),2),"(",round(sd(x6.PRPBIe$PBI + x6.PRPBIr$PBI),2),")")
    df[13, 5] =paste0(round((mean(x6.REPBIe$PBI) + mean(x6.REPBIr$PBI)),2),"(",round(sd(x6.REPBIe$PBI + x6.REPBIr$PBI),2),")")


    ############################################################################################################################
    ##### Finalize and Show df #################################################################################################
    ############################################################################################################################

    View(df)
    assign(paste0(phaseInp, ".desc"), df, .GlobalEnv)

  }
  rm(list=setdiff(ls(), c("dat", "test.desc", "retest.desc")))
  rm(phaseInp, envir = .GlobalEnv)
}
jpsnijder/AXCPT documentation built on May 9, 2019, 7:34 p.m.