Skip to content

Instantly share code, notes, and snippets.

@nelsonroque
Created March 5, 2019 18:25
Show Gist options
  • Save nelsonroque/d16620eab029474315c7de8eabd87dc2 to your computer and use it in GitHub Desktop.
Save nelsonroque/d16620eab029474315c7de8eabd87dc2 to your computer and use it in GitHub Desktop.
Color Dots: Scoring and Summary Scripts (for use with R's tidyverse package)
# for scoring raw, parsed data
score_color_dots <- function(df, threshold=75){
scored <- df %>%
separate(Loc1, into=c("Loc1_x", "Loc1_y"), " ", convert=T) %>%
separate(Loc2, into=c("Loc2_x", "Loc2_y"), " ", convert=T) %>%
separate(Loc3, into=c("Loc3_x", "Loc3_y"), " ", convert=T) %>%
separate(ProbedLocation, into=c("probe_x", "probe_y"), " ", convert=T) %>%
separate(FinalLocation, into=c("final_x", "final_y"), " ", convert=T) %>%
mutate(stage1.loc1_distance = distance(Loc1_x, probe_x, Loc1_y, probe_y),
stage1.loc2_distance = distance(Loc2_x, probe_x, Loc2_y, probe_y),
stage1.loc3_distance = distance(Loc3_x, probe_x, Loc3_y, probe_y)) %>%
rowwise() %>%
mutate(stage1.which.location.probed = which.min(c(stage1.loc1_distance, stage1.loc2_distance, stage1.loc3_distance))) %>%
mutate(stage1.color.at.probed.location = ifelse(stage1.which.location.probed == 1,Col1,
ifelse(stage1.which.location.probed == 2,Col2,
ifelse(stage1.which.location.probed == 3, Col3, NA))),
stage1.probe.location_x = ifelse(stage1.which.location.probed == 1, Loc1_x,
ifelse(stage1.which.location.probed == 2, Loc2_x,
ifelse(stage1.which.location.probed == 3, Loc3_x, NA))),
stage1.probe.location_y = ifelse(stage1.which.location.probed == 1, Loc1_y,
ifelse(stage1.which.location.probed == 2, Loc2_y,
ifelse(stage1.which.location.probed == 3, Loc3_y, NA)))) %>%
mutate(stage1.is.color.correct = ifelse(stage1.color.at.probed.location == ColorChoice, 1, 0)) %>%
rowwise() %>%
mutate(stage1.is.color.chosen.shown = ifelse(ColorChoice %in% c(Col1,Col2,Col3), 1, 0)) %>%
mutate(stage1.classification = ifelse(stage1.is.color.chosen.shown == 1 & stage1.is.color.correct == 1, "CORRECT",
ifelse(stage1.is.color.chosen.shown == 0 & stage1.is.color.correct == 0, "RANDOM",
ifelse(stage1.is.color.chosen.shown == 1 & stage1.is.color.correct == 0, "SWAP")))) %>%
mutate(stage2.col1_match = ifelse(ProbedColor == Col1, 1, 0),
stage2.col2_match = ifelse(ProbedColor == Col2, 1, 0),
stage2.col3_match = ifelse(ProbedColor == Col3, 1, 0)) %>%
mutate(stage2.which.location.is.probed.color = which(1 == c(stage2.col1_match, stage2.col2_match, stage2.col3_match))) %>%
mutate(stage2.location.at.probed.color.x = ifelse(stage2.which.location.is.probed.color == 1, Loc1_x,
ifelse(stage2.which.location.is.probed.color == 2, Loc2_x,
ifelse(stage2.which.location.is.probed.color == 3, Loc3_x, NA)))) %>%
mutate(stage2.location.at.probed.color.y = ifelse(stage2.which.location.is.probed.color == 1, Loc1_y,
ifelse(stage2.which.location.is.probed.color == 2, Loc2_y,
ifelse(stage2.which.location.is.probed.color == 3, Loc3_y, NA)))) %>%
mutate(stage2.loc1_distance = distance(Loc1_x, stage2.location.at.probed.color.x,
Loc1_y, stage2.location.at.probed.color.y),
stage2.loc2_distance = distance(Loc2_x, stage2.location.at.probed.color.x,
Loc2_y, stage2.location.at.probed.color.y),
stage2.loc3_distance = distance(Loc3_x, stage2.location.at.probed.color.x,
Loc3_y, stage2.location.at.probed.color.y),
stage2.distance.from.color.probe = distance(final_x, stage2.location.at.probed.color.x,
final_y, stage2.location.at.probed.color.y),
stage2.distance.from.location.probe = distance(final_x, stage1.probe.location_x,
final_y, stage1.probe.location_y)) %>%
mutate(which.location.unprobed = setdiff(c(1,2,3),c(stage1.which.location.probed, stage2.which.location.is.probed.color))) %>%
mutate(location.unprobed.x = ifelse(which.location.unprobed == 1, Loc1_x,
ifelse(which.location.unprobed == 2, Loc2_x,
ifelse(which.location.unprobed == 3, Loc3_x, NA))),
location.unprobed.y = ifelse(which.location.unprobed == 1, Loc1_y,
ifelse(which.location.unprobed == 2, Loc2_y,
ifelse(which.location.unprobed == 3, Loc3_y, NA)))) %>%
mutate(stage2.distance.from.location.unprobed = distance(final_x, location.unprobed.x,
final_y, location.unprobed.y)) %>%
mutate(stage2.is.response.distance.near.color.probe = ifelse(stage2.distance.from.color.probe <= threshold, 1, 0),
stage2.is.response.distance.near.location.probe = ifelse(stage2.distance.from.location.probe <= threshold, 1, 0),
stage2.is.response.distance.near.location.unprobed = ifelse(stage2.distance.from.location.unprobed <= threshold, 1, 0)) %>%
mutate(stage2.classification = ifelse(stage2.is.response.distance.near.color.probe == 1 & stage2.is.response.distance.near.location.probe == 0, "CORRECT",
ifelse(stage2.is.response.distance.near.color.probe == 0 & stage2.is.response.distance.near.location.probe == 1, "SWAP",
ifelse(stage2.is.response.distance.near.color.probe == 0 & stage2.is.response.distance.near.location.probe == 0, "RANDOM", NA))))
return(scored)
}
# for processing COLOR DOTS data from Survey Dolphin
summary_color_dots <- function(df, group_var) {
TASK_NAME <- "COLOR_DOTS"
summary.df <- df %>%
group_by_(.dots = group_var) %>%
summarise(median.RT.color = median(ColorRT,na.rm=T),
sd.RT.color = sd(ColorRT,na.rm=T),
median.RT.location = median(LocRT,na.rm=T),
sd.RT.location = sd(LocRT,na.rm=T),
stage2.median.precision.swap = median(stage2.distance.from.color.probe[stage2.classification == "SWAP"], na.rm=T),
stage2.sd.precision.swap = sd(stage2.distance.from.color.probe[stage2.classification == "SWAP"], na.rm=T),
stage2.median.precision.correct = median(stage2.distance.from.color.probe[stage2.classification == "CORRECT"], na.rm=T),
stage2.sd.precision.correct = sd(stage2.distance.from.color.probe[stage2.classification == "CORRECT"], na.rm=T),
stage2.median.precision.random = median(stage2.distance.from.color.probe[stage2.classification == "RANDOM"], na.rm=T),
stage2.sd.precision.random = sd(stage2.distance.from.color.probe[stage2.classification == "RANDOM"], na.rm=T),
stage2.median.precision.correct.or.swap = median(stage2.distance.from.color.probe[stage2.classification == "CORRECT" | stage2.classification == "SWAP"], na.rm=T),
stage2.sd.precision.correct.or.swap = sd(stage2.distance.from.color.probe[stage2.classification == "CORRECT" | stage2.classification == "SWAP"], na.rm=T),
stage1.swap.count = sum(stage1.classification == "SWAP"),
stage2.swap.count = sum(stage2.classification == "SWAP"),
stage1.random.count = sum(stage1.classification == "RANDOM"),
stage2.random.count = sum(stage2.classification == "RANDOM"),
stage1.correct.count = sum(stage1.classification == "CORRECT"),
stage2.correct.count = sum(stage2.classification == "CORRECT"),
n = n()) %>%
mutate(stage1.swap.prop = stage1.swap.count / n,
stage2.swap.prop = stage2.swap.count / n,
stage1.random.prop = stage1.random.count / n,
stage2.random.prop = stage2.random.count / n,
stage1.correct.prop = stage1.correct.count / n,
stage2.correct.prop = stage2.correct.count / n)
# add task name to column names
len_group_var = length(group_var)
names(summary.df)[(len_group_var+1):ncol(summary.df)] <- paste0(TASK_NAME,".",names(summary.df)[(len_group_var+1):ncol(summary.df)])
return(summary.df)
}
df <- read_ambcog("C:/Users/nur375/Box/EAS data files for AAIC submission/Raw EMA files (SAS)/color_dots.sas7bdat")
df.scored <- score_color_dots(df,threshold=75)
df.summary <- summary_color_dots(df.scored,"id")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment