README.md

cogsciutils

An R-package with helper functions for social scientists and cognitive psychologists. Note, the package is currently being developed (bleeding edge).

What you can do

Installation

First time installation

library(devtools) #maybe install.packages('devtools')
devtools::install_github('janajarecki/cogsciutils')

Usage

library(cogsciutils)

Participants

The paricipants() function summarizes demographic data and prints a text like this

participants(mydata, id = "id", age = "age", gender = "fem", excl = 0)

In total ten participants completed the study (zero were excluded), 6 males and 4 females (60% and 40%, respectively), mean age 50 years (med = 51, sd = 18, range 21-73 years).

Details of participants()

# Let's make sample data
set.seed(232)
N <- 10
mydata <- data.frame(id = 1:N,
                    age = sample(18:75, N),
                    fem = sample(c("m","f"), N, T))
head(mydata, 3)
#   id age fem
# 1  1  73   f
# 2  2  71   m
# 3  3  23   f
participants(mydata, id = "id", age = "age", gender = "fem", excl = 0)

Further usage

Choice rules

Binary data: from a vector

Calculate Luce choice rule

library(cogsciutils)
# Binary predictions
binaryPredictions <- c(.22, .5, .73)
choicerule(binaryPredictions, "softmax", tau = 2)
choicerule(binaryPredictions, "argmax")
choicerule(binaryPredictions, "epsilon", eps = .2)

Predictions for three or more variables: from a matrix

# Make some predictions for three variables A, B, C
predictions <- cbind(A = c(.1,.5,.4), B = c(.3,.1,.4), C = c(.6, .4, .2))
choicerule(predictions, "luce")
choicerule(predictions, "argmax")
choicerule(predictions, "epsilon", eps = .2)

Goodness of fit

Fit from raw observed data

Calculate the log likelihood for some observatinos

library(cogsciutils)

# N = 100 observations
set.seed(121)
x <- sample(0:1, 100, rep = T) # observations of 0s and 1s
y <- rep(0.55, 100) # predicting to observe 1 with probability 55 %
gof(obs = x, pred = y, type = "loglik") # log likelihood
gof(obs = x, pred = y, type = "mse") # mean squared error, average squared deviations
gof(obs = x, pred = y, type = "sse") # sum of squared errors, sum of squared deviations
gof(obs = x, pred = y, type = "loglik", saturated = TRUE) # saturated log lik

# Ignore first 5 observations
gof(obs = x, pred = y, type = "loglik", discount = 5)
gof(obs = x, pred = y, type = "loglik", saturated = TRUE, discount = 5) # saturated log lik

Fit from proportions observed of responses

Calculate the fit from the aggregated relative frequencies of responses. In this case you can also calculate the weighted sum of squares wsse and weighted mean squared error wmse. Weighting means predictions close to 0.5 are discounted in the fit, and predictions closer to 0 and 1 are more important. Weighting is done by dividing through the variance (var = p * (1-p)) of each unique prediction.

library(cogsciutils)

# Sum of squared errors (SSE) between predictions and observations
x <- c(.96, .78) # observations as proportions of responses (aggregated)
y <- c(.85, .95) # predictions for the response

# How is the goodness of fit?
gof(obs = x, pred = y, type = "loglik") # log likelihood
gof(obs = x, pred = y, type = "mse") # mean squared deviations
gof(obs = x, pred = y, type = "sse") # sum of squared deviations

# If you want the weighted SSE the function needs to know
# how many data points underly the observed proportions
n <- c(100, 100)
gof(obs = x, pred = y, type = "wmse", n = n) # mean squared deviations
gof(obs = x, pred = y, type = "wsse", n = n) # sum of squared deviations


JanaJarecki/cognitiveutils documentation built on Sept. 9, 2020, 9:11 a.m.