Info

As I said in our meeting. This is very useful material - but you will also be able to make it successfully through your project if you don’t learn programming…

Getting R

To play around with R, I recommen using RStudio which you can get for free here: https://www.rstudio.com/products/rstudio/download/

It will work on Windows, Mac, Linux, … which is great.

You’ll also have to install R itself - there is a link on the page.

Tutorials on R

Psychophysics data in R

Walk through of the code so far

You need to make sure you have the appropriate libraries / packages installed.

install.packages("tidyverse")
install.packages("quickpsy")

Then load them in for the current session

library(tidyverse) # reorganise tables, etc.
library(quickpsy) # fitting curves

Then we are going to read in the data file (you will use your own data later, but here we have a canned example data set)

# set a placeholder that contains the filename
filename <- "sampledata.csv"
# read in the data file using the function read_csv()
data <- read_csv(filename)
## Parsed with column specification:
## cols(
##   ori = col_integer(),
##   response = col_integer()
## )
head(data) # show the first few rows of the table
## # A tibble: 6 x 2
##     ori response
##   <int>    <int>
## 1     0        2
## 2     3        2
## 3     1        2
## 4     3        2
## 5     1        1
## 6     5        2

You will see that the data isn’t in the correct form for us yet. Every row in the table contains information about one trial: at a particular ori we get a particular response (which can have the value 1 or 2).

We want a table that summarises all the trials at a particular orientation. To do this, we can use some clever functions from a package called dplyr which is part of the tidyverse package we have loaded in.

# store the result in ``data_summary``
data_summary <- data %>%
  group_by(ori) %>%  # make groups by orientation
  summarise(prop_right = mean(response == 2), # proportion s said  "right"
            n_right = sum(response == 2), # also number of times 
            n = n()) %>%  # and how many trials
  arrange(ori)  # then sort the table by orientations, so it's nice looking

# and how the first few rows
head(data_summary)
## # A tibble: 6 x 4
##     ori prop_right n_right     n
##   <int>      <dbl>   <int> <int>
## 1   -10          0       0     2
## 2    -9          0       0     2
## 3    -8          0       0     3
## 4    -7          0       0     1
## 5    -6          0       0     2
## 6    -5          0       0     2

To make a plot

We can use the plotting library to make a nice looking plots. If this lo

# use ggplot2
data_plot <- ggplot( data = data_summary, 
                     aes(x = ori, y = prop_right)) +  # say what's X and Y in plot
  geom_point() +  # say that we want to use dots
  geom_line(color='blue') + # could also add a blue line connecting the dots ?!
  theme_minimal()

# and show it
data_plot

Now fit the data

Using the second library we have loaded. The following line of code runs a fit and returns lots of information in the fit variable.

fit <- quickpsy(data_summary, ori, n_right, n) 
summary(fit) # display a summary
##   parn       par     parinf    parsup
## 1   p1 0.2448263 -0.5905940 0.8817992
## 2   p2 1.1454366  0.1440642 1.8954914
## NULL

Now plot the data and fit together

You can now see why the stored the plot in a variable called data_plot. We can just reuse it and add another layer to the plot:

data_plot +
  geom_line(data= fit$curves, aes(x = x, y = y), color='red', size=2, alpha=0.5)

Voilà!

Area under the curve

On 16 Nov, we also taked briefly about what connects the normal distribution to the S-shaped curve we are trying to fit to the data… the words to look for if you want to read up about this are normal distribution and cumulative normal distribution (area under the curve).