Skip to content

Instantly share code, notes, and snippets.

@AWKruijt
Created June 10, 2019 12:36
Show Gist options
  • Save AWKruijt/9586efdfd37c227a21c7820d880a14ce to your computer and use it in GitHub Desktop.
Save AWKruijt/9586efdfd37c227a21c7820d880a14ce to your computer and use it in GitHub Desktop.
```{r}
library(tictoc)
library(splithalf)
library(splithalfr)
```
```{r}
tic()
data("ds_vpt", package = "splithalfr")
# ds_vpt <- subset(ds_vpt, block_type == "assess") # cancelling this line as ds_vpt contains assess trials only
vpt_fn_sets <- function (ds) {
return (list(
# Probe-at-test
patt_yes = subset(ds, patt == "yes"),
# Probe-at-control
patt_no = subset(ds, patt == "no")
))
}
vpt_fn_score <- function (sets) {
rt_yes <- subset(sets$patt_yes, response == 1)$rt
rt_yes <- rt_yes[rt_yes >= 200 & rt_yes <= 520]
rt_no <- subset(sets$patt_no, response == 1)$rt
rt_no <- rt_no[rt_no >= 200 & rt_no <= 520]
return (mean(rt_no) - mean(rt_yes))
}
## splithalfing
vpt_splits <- sh_apply(ds_vpt, "UserID", vpt_fn_sets, vpt_fn_score, 5000)
# Spearman-Brown
mean_sb_by_split(vpt_splits)
toc()
```
```{r}
tic()
ds_vpt <- as.data.frame(splithalfr::ds_vpt)
ds_vpt$correct [ds_vpt$response == 1] <- 1
ds_vpt$correct [ds_vpt$response != 1] <- 0
ds_vpt$rt <- as.numeric(ds_vpt$rt)
ds_vpt$rt <- as.numeric(ds_vpt$rt)
splithalf_diff(ds_vpt, RTmintrim = 200, RTmaxtrim = 520, no.iterations = 5000, incErrors = F, var.RT = "rt", var.participant = "UserID", var.correct = "correct", var.trialnum = "trial", var.compare = "patt", compare1 = "yes", compare2 = "no")
toc()
```
```{r}
# check if difference in estimates may be due to splithalf() using an adapted Spearman-Brown formula in which the absolute value of the correlation is used - this makes it so that a negative correlation between two parts also results in an SB estimate that correspons to the SB estimate if the correlation had been positive:
# example:
corpos <- .2
corneg <- -.2
2*corpos/(1+corpos) # 0.3333333
2*corneg/(1+corneg) # -0.5
2*corneg/(1+ abs(corneg)) # -0.3333333
# adapt formula's from splithalfr() to implement the abs(cor(x,y)) in the SB function:
spearman_brownabs <- function (x, y) {
return (2 * cor(x,y) / (1 + abs(cor(x,y))))
}
mean_sbabs_by_split <- function (ds) {
return (mean_rel_by_split(ds, spearman_brownabs))
}
require(dplyr)
mean_rel_by_split <- function (ds, fn_rel) {
# Check on missing values
if (any(is.na(ds[c("score_1", "score_2")]))) {
warning ("input data contained missing values; these were pairwise removed before calculating the reliability coefficient")
# Remove missing values pairwise
rows_missing = is.na(ds$score_1) | is.na(ds$score_2)
ds = ds[!rows_missing,]
}
ds_rs <- ds %>%
group_by(split) %>%
summarize(
r = fn_rel(.data$score_1, .data$score_2)
)
return (mean(ds_rs$r))
}
# compare SB estimates with and without abs(cor(x,y)) tweak:
# Spearman-Brown
mean_sb_by_split(vpt_splits)
# Spearman-Brown
mean_sbabs_by_split(vpt_splits)
# the latter one is more in line with the SB esitmate returned by splithalf()
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment