Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add creativity related functions #81

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Imports:
Suggests:
covr,
mockery,
proxy,
readr,
roxygen2,
testthat (>= 3.0.0),
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

export(ant_alert)
export(ant_orient)
export(aut)
export(bart)
export(bps)
export(capacity)
Expand All @@ -12,6 +13,7 @@ export(countcorrect)
export(countcorrect2)
export(cpt)
export(crt)
export(dat)
export(driving)
export(drm)
export(dualnback)
Expand Down Expand Up @@ -40,6 +42,7 @@ export(sumweighted)
export(switchcost)
export(symncmp)
export(synwin)
export(vg)
export(wrangle_data)
import(dplyr)
import(rlang)
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

* Added `wrangle_data()` and `preproc_data()` functions, which were previously in tarflow.iquizoo package.

## New Features

* Added support for three creativity related tasks.

## Bug Fixes

* Fixed an edge case when `fit_numerosity()` will stuck in infinite loop.
Expand Down
72 changes: 72 additions & 0 deletions R/aut.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#' Alternate Uses Task (AUT)
#'
#' The Alternate Uses Task (AUT) is a test of divergent thinking, which is a
#' component of creativity. The test-taker is given a common object and asked
#' to list as many uses for that object as they can think of. The test is
#' scored based on the number of uses generated, the originality of the uses,
#' the flexibility of the uses, and the fluency of the uses. The originality
#' score is based on the number of participants who generated the same use for
#' the object. The flexibility score is based on the number of categories of
#' uses generated. The fluency score is based on the total number of uses
#' generated.
#'
#' @template common
#' @template options
#' @return An object with the same class as `data` contains following values:
#'
#' \item{originality}{The originality score.}
#'
#' \item{flexibility}{The flexibility score.}
#'
#' \item{fluency}{The fluency score.}
#'
#' @export
aut <- function(data, .by = NULL, .input = NULL, .extra = NULL) {
.input <- list(
name_tool = "tool",
name_use = "use",
name_use_type = "use_type",
name_grader = "subj",
name_score_origin = "score_origin"

Check warning on line 30 in R/aut.R

View check run for this annotation

Codecov / codecov/patch

R/aut.R#L25-L30

Added lines #L25 - L30 were not covered by tests
) |>
update_settings(.input)
.extra <- list(
grade_types = data.frame(),
grade_scores = data.frame()

Check warning on line 35 in R/aut.R

View check run for this annotation

Codecov / codecov/patch

R/aut.R#L32-L35

Added lines #L32 - L35 were not covered by tests
) |>
update_settings(.extra)
data |>
left_join(
.extra$grade_types,
by = c(.input$name_tool, .input$name_use),
relationship = "many-to-many"

Check warning on line 42 in R/aut.R

View check run for this annotation

Codecov / codecov/patch

R/aut.R#L37-L42

Added lines #L37 - L42 were not covered by tests
) |>
left_join(
.extra$grade_scores,
by = c(.input$name_grader, .input$name_tool, .input$name_use_type),

Check warning on line 46 in R/aut.R

View check run for this annotation

Codecov / codecov/patch

R/aut.R#L44-L46

Added lines #L44 - L46 were not covered by tests
) |>
filter(.data[[.input$name_use_type]] != "0") |>
group_by(pick(all_of(c(.by, .input$name_grader, .input$name_tool)))) |>
summarise(
originality = sum(.data[[.input$name_score_origin]]),
flexibility = n_distinct(.data[[.input$name_use_type]]),
fluency = n(),
.groups = "drop_last"

Check warning on line 54 in R/aut.R

View check run for this annotation

Codecov / codecov/patch

R/aut.R#L48-L54

Added lines #L48 - L54 were not covered by tests
) |>
# average across tools
summarise(
across(
c("originality", "flexibility", "fluency"),
mean

Check warning on line 60 in R/aut.R

View check run for this annotation

Codecov / codecov/patch

R/aut.R#L57-L60

Added lines #L57 - L60 were not covered by tests
),
.groups = "drop_last"

Check warning on line 62 in R/aut.R

View check run for this annotation

Codecov / codecov/patch

R/aut.R#L62

Added line #L62 was not covered by tests
) |>
# average across graders
summarise(
across(
c("originality", "flexibility", "fluency"),
mean

Check warning on line 68 in R/aut.R

View check run for this annotation

Codecov / codecov/patch

R/aut.R#L65-L68

Added lines #L65 - L68 were not covered by tests
),
.groups = "drop"

Check warning on line 70 in R/aut.R

View check run for this annotation

Codecov / codecov/patch

R/aut.R#L70

Added line #L70 was not covered by tests
)
}
39 changes: 39 additions & 0 deletions R/dat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#' Divergent Association Test
#'
#' Note you must input words distance matrix as a `dist` object for this
#' function to work.
#'
#' @template common
#' @template options
#' @return An object with the same class as `data` contains following values:
#' \item{score}{Divergent Association Test score.}
#' @export
dat <- function(data, .by = NULL, .input = NULL, .extra = NULL) {
if (!requireNamespace("proxy", quietly = TRUE)) {
stop("Please install the `proxy` package to use this function.")

Check warning on line 13 in R/dat.R

View check run for this annotation

Codecov / codecov/patch

R/dat.R#L13

Added line #L13 was not covered by tests
}
.input <- list(name_words = "words") |>
update_settings(.input)
.extra <- list(
# only keep the first 7 words by default (Olson et al. 2021)
minimum = 7,
w2v = data.frame() # note this must be set by the user
) |>
update_settings(.extra)
data |>
summarise(
score = .data[[.input$name_words]] |>
stringr::str_split_1("-") |>
get_dat_score(.extra$minimum, .extra$w2v),
.by = all_of(.by)
) |>
vctrs::vec_restore(data)
}

get_dat_score <- function(words, minimum, w2v) {
w2v_matched <- w2v[match(words, rownames(w2v)), ]
if (nrow(w2v_matched) < minimum) {
return(NA_real_)
}
mean(proxy::dist(w2v_matched[seq_len(minimum), ], method = "cosine"))
}
34 changes: 34 additions & 0 deletions R/vg.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#' Verb Generation
#'
#' This test requires the user to generate a verb that is semantically related
#' to each given noun. The score is the average cosine distance between the
#' generated verb and the given nouns.
#'
#' @template common
#' @template options
#' @return An object with the same class as `data` contains following values:
#'
#' \item{score}{Average cosine distance between the generated verbs and the
#' given nouns.}
#' @export
vg <- function(data, .by = NULL, .input = NULL, .extra = NULL) {
.input <- list(
name_noun = "noun",
name_verb = "verb",
name_dist = "dist_cos"

Check warning on line 18 in R/vg.R

View check run for this annotation

Codecov / codecov/patch

R/vg.R#L15-L18

Added lines #L15 - L18 were not covered by tests
) |>
update_settings(.input)
.extra <- list(
min_prop_miss = 0.2,
dists = data.frame() # note this must be set by the user

Check warning on line 23 in R/vg.R

View check run for this annotation

Codecov / codecov/patch

R/vg.R#L20-L23

Added lines #L20 - L23 were not covered by tests
) |>
update_settings(.extra)
data |>
left_join(.extra$dists, by = c(.input$name_noun, .input$name_verb)) |>
filter(mean(!is.na(.data[[.input$name_verb]])) >= .extra$min_prop_miss) |>
summarise(
score = mean(.data[[.input$name_dist]], na.rm = TRUE),
.by = all_of(.by)

Check warning on line 31 in R/vg.R

View check run for this annotation

Codecov / codecov/patch

R/vg.R#L25-L31

Added lines #L25 - L31 were not covered by tests
) |>
vctrs::vec_restore(data)

Check warning on line 33 in R/vg.R

View check run for this annotation

Codecov / codecov/patch

R/vg.R#L33

Added line #L33 was not covered by tests
}
39 changes: 39 additions & 0 deletions man/aut.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions man/dat.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions man/vg.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions tests/testthat/_snaps/dat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Basic test

{
"type": "list",
"attributes": {
"names": {
"type": "character",
"attributes": {},
"value": ["id", "score"]
},
"row.names": {
"type": "integer",
"attributes": {},
"value": [1, 2]
},
"class": {
"type": "character",
"attributes": {},
"value": ["data.frame"]
}
},
"value": [
{
"type": "integer",
"attributes": {},
"value": [1, 2]
},
{
"type": "double",
"attributes": {},
"value": [1.01973648, "NA"]
}
]
}

13 changes: 13 additions & 0 deletions tests/testthat/test-dat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
test_that("Basic test", {
data <- data.frame(id = 1:2, words = c("A-B-C-D-E-F-G", "A-I"))
dists <- withr::with_seed(
1,
{
nobs <- 7
mat <- matrix(rnorm(100 * nobs), nrow = nobs)
rownames(mat) <- LETTERS[1:nobs]
}
)
dat(data, .by = "id", .extra = list(w2v = mat)) |>
expect_snapshot_value(style = "json2")
})