basics

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")) 

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] "x"                    "k"                    "n"                   
#>  [4] "guess"                "lapses"               "averages"            
#>  [7] "groups"               "funname"              "log"                 
#> [10] "psyfunguesslapses"    "limits"               "pariniset"           
#> [13] "parini"               "optimization"         "par"                 
#> [16] "pariniset"            "ypred"                "curves"              
#> [19] "sse"                  "thresholds"           "logliks"             
#> [22] "loglikssaturated"     "deviance"             "avbootstrap"         
#> [25] "parbootstrap"         "logliksboot"          "logliksbootsaturated"
#> [28] "devianceboot"         "aic"                  "parcomparisons"      
#> [31] "curvesbootstrap"      "thresholdsbootstrap"  "thresholdcomparisons"

To obtain the thresholds, for example, we can do

fit$thresholds
#>   participant cond      thre prob   threinf   thresup
#> 1          aa   -1 147.35236  0.5 138.47536 159.01520
#> 2          aa    1 114.58814  0.5 102.56245 125.78226
#> 3          bb   -1 162.26353  0.5 152.67441 175.19374
#> 4          bb    1 145.74502  0.5 138.18880 154.73451
#> 5          cc   -1  83.85067  0.5  70.38967  96.75973
#> 6          cc    1  91.49326  0.5  81.37286 102.96594

and to obtain the parameters

fit$par
#> # A tibble: 12 × 6
#> # Groups:   participant, cond [6]
#>    participant  cond parn    par parinf parsup
#>    <fct>       <int> <chr> <dbl>  <dbl>  <dbl>
#>  1 aa             -1 p1    147.   138.   159. 
#>  2 aa             -1 p2    107.    97.8  119. 
#>  3 aa              1 p1    115.   103.   126. 
#>  4 aa              1 p2    106.    96.3  116. 
#>  5 bb             -1 p1    162.   153.   175. 
#>  6 bb             -1 p2     85.7   77.3   95.2
#>  7 bb              1 p1    146.   138.   155. 
#>  8 bb              1 p2     69.3   62.0   76.8
#>  9 cc             -1 p1     83.9   70.4   96.8
#> 10 cc             -1 p2    139.   126.   151. 
#> 11 cc              1 p1     91.5   81.4  103. 
#> 12 cc              1 p2    139.   123.   150.

We can use plot to plot the psychometric curves

plot(fit)
#> Warning in geom_errorbarh(data = qp$thresholds, height = 0.03, aes_string(x =
#> "threinf", : Ignoring unknown aesthetics: x

or if we want to used different colors for the different conditions

plot(fit, color = cond)
#> Warning in geom_errorbarh(data = qp$thresholds, height = 0.03, aes_string(x =
#> "threinf", : Ignoring unknown aesthetics: x

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()
#> Warning in geom_errorbarh(data = qp$thresholds, height = 0.03, aes_string(x =
#> "threinf", : Ignoring unknown aesthetics: x

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))