We are going to analize the data set qpdat,
which is included in quickpsy. It has a tidy form: each
column corresponds to a variable and each row to an observation.
To fit the psychometric functions (the default shape is the cumulative normal function), we use the main function of quickpsy, which has the name of the package. We introduce the data, the name of the explanatory variable, the name of the response variable and the name of the grouping variables.
library(quickpsy)
fit <- quickpsy(qpdat, phase, resp, grouping = c("participant", "cond"))
#> Using only 100 bootstrap samples. Use the B argument, to modify it.quickpsy creates a object of class quickpsy with a list
of data frames with all the results. We can use names to
see the names of the output data frames
names(fit)
#> [1] "averages" "limits" "psych_fun"
#> [4] "nll_fun" "nll_fun_saturated" "parini"
#> [7] "par" "hessian" "ypred"
#> [10] "x_seq" "curves" "logliks"
#> [13] "loglikssaturated" "aic" "deviance"
#> [16] "thresholds" "avbootstrap" "avbootstrap"
#> [19] "parbootstrap" "ypredbootstrap" "curvesbootstrap"
#> [22] "thresholdsbootstrap" "x_str" "log"
#> [25] "guess" "lapses" "grouping"To obtain the thresholds, for example, we can do
fit$thresholds
#> # A tibble: 6 × 6
#> # Groups: participant, cond [6]
#> participant cond prob thre threinf thresup
#> <chr> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 Participant1 cond1 0.5 -70.2 -83.4 -54.2
#> 2 Participant1 cond2 0.5 -121. -133. -110.
#> 3 Participant2 cond1 0.5 -92.5 -114. -78.6
#> 4 Participant2 cond2 0.5 -130. -150. -108.
#> 5 Participant3 cond1 0.5 -81.4 -101. -64.4
#> 6 Participant3 cond2 0.5 -125. -147. -98.0and to obtain the parameters
fit$par
#> # A tibble: 12 × 6
#> # Groups: participant, cond [6]
#> participant cond parn par parinf parsup
#> <chr> <chr> <chr> <dbl> <dbl> <dbl>
#> 1 Participant1 cond1 p1 -70.2 -83.4 -54.2
#> 2 Participant1 cond1 p2 78.5 65.3 93.5
#> 3 Participant1 cond2 p1 -121. -133. -110.
#> 4 Participant1 cond2 p2 61.7 49.1 70.1
#> 5 Participant2 cond1 p1 -92.5 -114. -78.6
#> 6 Participant2 cond1 p2 67.2 48.3 82.7
#> 7 Participant2 cond2 p1 -130. -150. -108.
#> 8 Participant2 cond2 p2 66.1 46.6 78.9
#> 9 Participant3 cond1 p1 -81.4 -101. -64.4
#> 10 Participant3 cond1 p2 69.2 50.0 86.8
#> 11 Participant3 cond2 p1 -125. -147. -98.0
#> 12 Participant3 cond2 p2 78.9 56.5 99.6We can use plot to plot the psychometric curves
or if we want to used different colors for the different conditions
The output plot is a ggplot and we can modify it accordingly. For example
library(ggplot2)
plot(fit, color = cond) +
labs(y = "Proportion") +
theme_minimal()
#> `height` was translated to `width`.Of course, we can also use the output data frames to create the plot ourselves
ggplot() +
facet_wrap(~ participant) +
geom_line(data = fit$curvesbootstrap,
aes(x = x, y = y, color = cond,
group=paste(sample, cond)), lwd = .2, alpha = .4) +
geom_line(data = fit$curves, aes(x = x, y = y, group = cond)) +
geom_point(data = fit$averages, aes(x = phase, y = prob), size = 2) +
geom_point(data = fit$averages, aes(x = phase, y = prob, color = cond))