Skip to content

Commit

Permalink
remove travis, update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
craddm committed Feb 10, 2021
1 parent 4b40114 commit 1a7172f
Show file tree
Hide file tree
Showing 18 changed files with 297 additions and 87 deletions.
3 changes: 3 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
^\.Rproj\.user$
^\.travis\.yml$
^docs$
^LICENSE\.md$
^README\.Rmd$
^\.github$
1 change: 1 addition & 0 deletions .github/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.html
86 changes: 86 additions & 0 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# For help debugging build failures open an issue on the RStudio community with the 'github-actions' tag.
# https://community.rstudio.com/new-topic?category=Package%20development&tags=github-actions
on:
push:
branches:
- main
- master
pull_request:
branches:
- main
- master

name: R-CMD-check

jobs:
R-CMD-check:
runs-on: ${{ matrix.config.os }}

name: ${{ matrix.config.os }} (${{ matrix.config.r }})

strategy:
fail-fast: false
matrix:
config:
- {os: windows-latest, r: 'release'}
- {os: macOS-latest, r: 'release'}
- {os: ubuntu-20.04, r: 'release', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"}
- {os: ubuntu-20.04, r: 'devel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"}

env:
R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
RSPM: ${{ matrix.config.rspm }}
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

steps:
- uses: actions/checkout@v2

- uses: r-lib/actions/setup-r@v1
with:
r-version: ${{ matrix.config.r }}

- uses: r-lib/actions/setup-pandoc@v1

- name: Query dependencies
run: |
install.packages('remotes')
saveRDS(remotes::dev_package_deps(dependencies = TRUE), ".github/depends.Rds", version = 2)
writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version")
shell: Rscript {0}

- name: Cache R packages
if: runner.os != 'Windows'
uses: actions/cache@v2
with:
path: ${{ env.R_LIBS_USER }}
key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }}
restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-

- name: Install system dependencies
if: runner.os == 'Linux'
run: |
while read -r cmd
do
eval sudo $cmd
done < <(Rscript -e 'writeLines(remotes::system_requirements("ubuntu", "20.04"))')
- name: Install dependencies
run: |
remotes::install_deps(dependencies = TRUE)
remotes::install_cran("rcmdcheck")
shell: Rscript {0}

- name: Check
env:
_R_CHECK_CRAN_INCOMING_REMOTE_: false
run: |
options(crayon.enabled = TRUE)
rcmdcheck::rcmdcheck(args = c("--no-manual", "--as-cran"), error_on = "warning", check_dir = "check")
shell: Rscript {0}

- name: Upload check results
if: failure()
uses: actions/upload-artifact@main
with:
name: ${{ runner.os }}-r${{ matrix.config.r }}-results
path: check
5 changes: 0 additions & 5 deletions .travis.yml

This file was deleted.

8 changes: 5 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
Package: metaSDT
Type: Package
Title: Calculate Type 1 and Type 2 Signal Detection Measures
Date: 2018-05-04
Date: 2021-02-10
Version: 0.5.0
Authors@R: c(
person("Matt", "Craddock", email = "[email protected]", role = "cre"))
Description: Type 1 and Type 2 signal detection measures for simple 2AFC tasks.
License: What license is it under?
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
Imports:
Expand All @@ -17,4 +17,6 @@ Imports:
purrr
Suggests:
testthat
RoxygenNote: 6.0.1
RoxygenNote: 7.1.1
URL: https://github.com/craddm/metaSDT
BugReports: https://github.com/craddm/metaSDT/issues
2 changes: 2 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
YEAR: 2021
COPYRIGHT HOLDER: metaSDT authors
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# MIT License

Copyright (c) 2021 metaSDT authors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
4 changes: 3 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ export(rating_sdt)
export(sdt_counts)
export(type_1_sdt)
import(dplyr)
import(purrr)
import(rlang)
import(tidyr)
importFrom(nleqslv,nleqslv)
importFrom(purrr,map)
importFrom(purrr,map2)
importFrom(purrr,map_dbl)
importFrom(stats,optim)
importFrom(stats,pnorm)
importFrom(stats,qnorm)
82 changes: 51 additions & 31 deletions R/sdt_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ type_1_sdt <- function(df, stimulus = NULL, response = NULL,
d_prime <- (1 / s) * qnorm(s1_HR) - qnorm(s1_FA)
c_raw <- (-1 / (1 + s)) * (qnorm(s1_HR) + qnorm(s1_FA))
c_prime <- c_raw / d_prime
data.frame(d_prime, c_raw, c_prime, s1_HR, s1_FA)
data.frame(d_prime,
c_raw,
c_prime,
s1_HR,
s1_FA)
}

#' Type 1 SDT for rating data
Expand Down Expand Up @@ -107,14 +111,16 @@ type_1_sdt <- function(df, stimulus = NULL, response = NULL,
#' The output is a data frame with various Type-1 measures, one for each
#' criterion.
#'
#' @param nR_S1 Responses to S1 stimulus. See below for advice.
#' @param nR_S2 Responses to S2 stimulus. See below for advice.
#' @param nr_s1 Responses to S1 stimulus. See below for advice.
#' @param nr_s2 Responses to S2 stimulus. See below for advice.
#' @param add_constant Adds a small constant to every cell to account for
#' boundaries - i.e. log-linear correction. Default = TRUE.
#' @author Matt Craddock \email{[email protected]}
#' @author Matt Craddock \email{matt@@mattcraddock.com}
#' @export

rating_sdt <- function (nr_s1, nr_s2, add_constant = FALSE) {
rating_sdt <- function(nr_s1,
nr_s2,
add_constant = FALSE) {

if (add_constant) {
nr_s1 <- nr_s1 + (1 / length(nr_s1))
Expand Down Expand Up @@ -175,16 +181,16 @@ rating_sdt <- function (nr_s1, nr_s2, add_constant = FALSE) {

sdt_counts <- function(df, stimulus = NULL, response = NULL,
split_resp = TRUE) {
stim_col <- enquo(stimulus)
resp_col <- enquo(response)
stim_col <- rlang::enquo(stimulus)
resp_col <- rlang::enquo(response)
df <- dplyr::group_by(df, !!stim_col, !!resp_col)
df <- dplyr::summarise(df, total = n())
df <- dplyr::ungroup(df)
df <- tidyr::complete(df, !!stim_col, !!resp_col, fill = list(total = 0))
if (split_resp) {
df <- tidyr::spread(df, !!stim_col, "total")
}
return(df)
df
}

#' Fit Type 2 SDT using Maximum Likelihood Estimation.
Expand Down Expand Up @@ -294,7 +300,7 @@ fit_meta_d_MLE <- function(nR_S1, nR_S2, s = 1, add_constant = TRUE) {
d1 <- (1 / s) * qnorm(ratingHR[t1_index]) - qnorm(ratingFAR[t1_index])
meta_d1 <- d1

c1 <- (-1 / (1 + s)) * ( qnorm(ratingHR) + qnorm(ratingFAR) )
c1 <- (-1 / (1 + s)) * (qnorm(ratingHR) + qnorm(ratingFAR) )
t1c1 <- c1[t1_index]
t2c1 <- c1[t2_index]

Expand Down Expand Up @@ -547,9 +553,9 @@ fit_meta_d_logL <- function(x, parameters) {
#' responses) to account for 0 or 1 values. Defaults to TRUE for ease of use
#' across multiple datasets.
#'@author Maniscalco & Lau. Ported to R by Matt Craddock
#' \email{[email protected]}
#' \email{matt@@mattcraddock.com}
#'@import dplyr
#'@import purrr
#'@importFrom purrr map_dbl map2 map
#'@export

fit_meta_d_SSE <- function(nR_S1, nR_S2, s = 1, d_min = -5, d_max = 5,
Expand Down Expand Up @@ -590,55 +596,63 @@ fit_meta_d_SSE <- function(nR_S1, nR_S2, s = 1, d_min = -5, d_max = 5,
sum(nR_S1[(n_ratings + 1):(n_ratings * 2)])
}

d_grid <- seq(d_min, d_max, by = d_grain)
d_grid <- seq(d_min,
d_max,
by = d_grain)
c_grid <- c_prime * d_grid

S1mu <- -d_grid / 2
S2mu <- d_grid / 2
S1sd <- 1
S2sd <- 1 / s

bounds <- 5 * max(S1sd, S2sd)
bounds <- 5 * max(S1sd,
S2sd)
SSEmin <- Inf

param_space <- map2(S1mu, S2mu,
~seq(.x - bounds, .y + bounds, by = .001)) # param space
param_space <- purrr::map2(S1mu,
S2mu,
~seq(.x - bounds,
.y + bounds,
by = .001)) # param space

min_z <- map2(param_space, c_grid, ~min(abs(.x - .y)))
c_ind <- map2(param_space, c_grid, ~which.min(abs(.x - .y)))
min_z <- purrr::map2(param_space, c_grid, ~min(abs(.x - .y)))
c_ind <- purrr::map2(param_space,
c_grid,
~which.min(abs(.x - .y)))

HRs <- map2(param_space, S2mu,
HRs <- purrr::map2(param_space, S2mu,
~ 1 - (pnorm(.x, .y, S2sd)))
FARs <- map2(param_space, S1mu,
FARs <- purrr::map2(param_space, S1mu,
~ 1 - (pnorm(.x, .y, S1sd)))

# fit type 2 data for S1 responses
est_HR2s_rS1 <- map2(FARs, c_ind, ~(1 - .x[1:.y]) / (1 - .x[.y]))
est_FAR2s_rS1 <- map2(HRs, c_ind, ~(1 - .x[1:.y]) / (1 - .x[.y]))
est_HR2s_rS1 <- purrr::map2(FARs, c_ind, ~(1 - .x[1:.y]) / (1 - .x[.y]))
est_FAR2s_rS1 <- purrr::map2(HRs, c_ind, ~(1 - .x[1:.y]) / (1 - .x[.y]))

SSE <- NULL
SSE_rS1 <- matrix(data = NaN, nrow = length(d_grid), ncol = n_ratings - 1)
rS1_ind <- matrix(data = NaN, nrow = length(d_grid), ncol = n_ratings - 1)

for (n in (1:(n_ratings - 1))) {
SSE <- map2(est_HR2s_rS1, est_FAR2s_rS1,
SSE <- purrr::map2(est_HR2s_rS1, est_FAR2s_rS1,
~ (.x - obs_HR2_rS1[[n]]) ^ 2 + (.y - obs_FAR2_rS1[[n]]) ^ 2)
SSE_rS1[, n] <- map_dbl(SSE, min)
inds <- unlist(map(SSE, which.min))
SSE_rS1[, n] <- purrr::map_dbl(SSE, min)
inds <- unlist(purrr::map(SSE, which.min))
rS1_ind[1:length(inds), n] <- inds
}

# fit type 2 data for S2 responses
est_HR2s_rS2 <- map2(HRs, c_ind, ~ .x[.y:length(.x)] / .x[.y])
est_FAR2s_rS2 <- map2(FARs, c_ind, ~ .x[.y:length(.x)] / .x[.y])
est_HR2s_rS2 <- purrr::map2(HRs, c_ind, ~ .x[.y:length(.x)] / .x[.y])
est_FAR2s_rS2 <- purrr::map2(FARs, c_ind, ~ .x[.y:length(.x)] / .x[.y])

SSE_rS2 <- matrix(data = NaN, nrow = length(d_grid), ncol = n_ratings - 1)
rS2_ind <- matrix(data = NaN, nrow = length(d_grid), ncol = n_ratings - 1)
for (n in (1:(n_ratings - 1))) {
SSE <- map2(est_HR2s_rS2, est_FAR2s_rS2,
SSE <- purrr::map2(est_HR2s_rS2, est_FAR2s_rS2,
~(.x - obs_HR2_rS2[[n]]) ^ 2 + (.y - obs_FAR2_rS2[[n]]) ^ 2)
SSE_rS2[, n] <- map_dbl(SSE, min)
inds <- unlist(map(SSE, which.min))
SSE_rS2[, n] <- purrr::map_dbl(SSE, min)
inds <- unlist(purrr::map(SSE, which.min))
rS2_ind[1:length(inds), n] <- inds
}

Expand Down Expand Up @@ -717,7 +731,10 @@ fit_meta_d_SSE <- function(nR_S1, nR_S2, s = 1, d_min = -5, d_max = 5,
#' @export
#'

fit_meta_d_bal <- function (nR_S1, nR_S2, s = 1, add_constant = FALSE) {
fit_meta_d_bal <- function(nR_S1,
nR_S2,
s = 1,
add_constant = FALSE) {

if (add_constant) {
nR_S1 <- nR_S1 + (1 / length(nR_S1))
Expand Down Expand Up @@ -778,10 +795,12 @@ fit_meta_d_bal <- function (nR_S1, nR_S2, s = 1, add_constant = FALSE) {
}

#' Internal function for fitting meta_d_plus
#'
#' @param x0 Starting parameters.
#' @param th Theta.
#' @param hp Hit rate for positive responses
#' @param fp False positive rate for positive responses.
#' @keywords internal

fit_metad_plus <- function(x0, th, hp, fp) {
y1 <- (1 - pnorm(x0[[1]], x0[[2]], 1)) /
Expand All @@ -792,11 +811,12 @@ fit_metad_plus <- function(x0, th, hp, fp) {


#' Internal function for fitting meta_d_minus
#'
#' @param x0 Starting parameters.
#' @param th Theta
#' @param hm Hit rate for negative responses
#' @param fm False positive rate for negative responses

#' @keywords internal
fit_metad_minus <- function(x0, th, hm, fm) {
y1 <- pnorm(x0[[1]], 0, 1) / pnorm(th * x0[[2]], 0, 1) - hm
y2 <- pnorm(x0[[1]], x0[[2]], 1) / pnorm(th * x0[[2]], x0[[2]], 1) - fm
Expand Down
22 changes: 0 additions & 22 deletions R/type_1_stan

This file was deleted.

Loading

0 comments on commit 1a7172f

Please sign in to comment.