Session 6 - More PsychoPy Flashcards
Last session we used Psychopy to “code” a simple experiment.
We can analyse last’s weeks data from RT experiment which is in Google sheet
in Colab
First step of analysing data from Google sheet (importing modules) - (8)
- Importing plotting library ‘matplotlib’
- Importing from authentication modules from ‘google.colab’
- Importing authorisation libaries for sheets and colab : ‘from google.auth import default’
- Importing ‘pandas’
- ‘Importing numpy as np’
- Importing ‘scipy as ‘scipy’ which is importing scientific python
- Importing from psypy a function called ‘lingress’ which does linear regression
- Importing gspread which is a toolbox for working/ accessing with google sheets. Allowa Google Colab to reference by columns, rows , by their names so can ask for first 3 rows etc…
Second step of analysing data from Google sheet (importing modules) - (4)(authorisation)
auth.authenticate_user()
This is where we authenticate to google - asking if you are ‘Gita’
If you authenticate once you do not do it again
Login in to your account (UoY)
In here, we are giving one website (Colab) the permission to access another web document (shared google spreadsheet) using your Google account information
Second step of analysing data from Google sheet (importing modules) - (2)(authorisation)
gc = gspread.authorize(cred)
This gets you a ‘google cloud’ handle/an authentication token
If you show this token ten Google will believe its ‘you’
Third step of analysing data from Google sheet (looking at the data) - (6)
Use the handle you just acquired to open the spreadsheet. Going to ask Google to open up spreadsheet of psychopy by giving them the handle to the workbook using web address of spreadsheet
- ‘wb=gc.open_by_url(‘https://docs.goog…’ : Here we get a handle to the workbook (wb) using the web address of the spreadsheet. That gc module is ‘google cloud’.
- data.wb.sheet1.get_all_values() - gets all the values from spreadsheet
-dArray = np.array(data) - turns all values of spreadsheet into numpy array which is mix of strings and numbers
- print(dArray[0,[1,2,3,4]]) - using indices [1,2,3,4] of the first row in ‘dArray’ - Access the first row of the numpy array ‘dArray’ using index 0 - print the header strings
- print(dArray[1:21,[1,2,3,4]])Print data (numbers) from rows 1 to 20 and columns 1 to 4 of ‘dArray’.
- fArray=dArray[1:21,[1,2,3,4]].astype(float) - Convert the sliced portion of ‘dArray’ containing numerical data into a numpy array of floats (fArray).
Last week we wondered if someone’s height made a difference to
RT and test for both hands
We can plot the data including a regression line and we can
Plotting the data and fitting a regression line using scipy’s linregress function for dominant and non-dominant hand
Reminder of what linear regression is - (4)
least sum of squares error) to a group of points. It has a slope (m) and a y-intercept (c).
y=mx+c
m’ represents the slope of the line, indicating the rate of change of y with respect to x.
- ‘c’ denotes the y-intercept, the value of y when x equals zero.
Code plotting the linear regression of dominant and non-dominant hand - explanation
RTs=(fArray[0:,[0,1]]) - # Extract RTs and other important variables. The variable ‘RTs’ will contain the data from columns 0 and 1 of ‘fArray’, which represent the RTs for the dominant and non-dominant hands respectively.
handedness=fArray[0:,2] - This line extracts the data from the third column of ‘fArray’, which represents handedness.
height=fArray[0:,3] This line extracts the data from the fourth column of ‘fArray’, which represents height.
for thisRTHand in range(2): - This line initializes a loop that iterates twice, once for each hand (dominant and non-dominant).
plt.subplot(1,2,thisRTHand+1) # Two subplots
plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=1, hspace=0) # Make the spacing nice
- These lines set up the subplot configuration, creating two subplots side by side.
They adjust the spacing between subplots for better visualization.
plt.scatter(height,RTs[:,thisRTHand]) - This line creates a scatter plot with height on the x-axis and RTs for the current hand (dominant or non-dominant) on the y-axis.
(m, c, rvalue, pvalue, stderr) = linregress(height,RTs[:,thisRTHand]) # height on x axis and RT on y axis - This line performs linear regression analysis to calculate the slope (m), intercept (c), correlation coefficient (rvalue), p-value (pvalue), and standard error (stderr) of the regression line -height on x axis and RT on y axis, m gradient, c offset, r value and p-value
y_pred = c + m*height # Make predictive y values based on height
plt.plot(height,y_pred, color=”red”, label=”Fit”)
These lines generate the predicted values (y_pred) using the equation of the regression line.
They plot the regression line on the scatter plot, indicating the relationship between height and RTs as a red line
plt.text(175,325,f”p={pvalue:.2f}”) # Places p-value of each scatter plot and r value
plt.text(175,340,f”r={rvalue:.2f}”) -These lines add text to the plot, displaying the p-value and correlation coefficient (r-value) with appropriate formatting - showing how much variation is explained in r value
plt.ylim(150,400) # Scale all plots to the same axes
plt.xlabel(“Height (cm)”)
plt.ylabel(“RT (s)”)
These lines set the y-axis limits for consistency across all plots and label the axes appropriately.
plt.show() - This line displays the plot to the user interface.
Output of this code:
What does this code show? - (2)
There doesn’t seem to be a general effect of height on either LH or RH reaction times. We could pool all the RTs in some way to get a more robust estimate but it is still almost certainly n.s.
One thing we might expect is that the dominant hand is faster than the non-dominant hand. To check this we can use a a t-test test.
What statistical test can you use to ask if two groups of numbers are different?
t-test .
There are several sorts of t-test. Which one is appropriate here?
One thing we might expect is that the dominant hand is faster than the non-dominant hand. To check this we can use a a t-test test.
a paired t-test (because we have two different measurements from each subject).
scipy has all these statistical tests in them like
linear regression, t-tests etc…
Before you conduct at-test you should plot the data - (3)
Plotting data from categories is often best done with a boxplot.
This gives you a nice way of comparing the data ‘side by side’ and also computes and plots measures of spread automatically.
The ‘notch’ in the boxplot is very useful: It is a measure of the confidence interval. If notches from two groups don’t overlap it’s a good indication that they have different central tendencies.
Explain this code of producing boxplot before conducting t-test:
Boxplots of for the reaction times (RTs) of dominant and non-dominant hands - (7)
‘notch=True’ adds a notch indicating the confidence interval around the median - gives estimation of CI around the centre (median)
Label the x-axis ticks as ‘Dominant’ and ‘NonDom’ corresponding to the two boxplots.
plt.show() - display graph
Perform a paired-sample t-test using scipy.stats.ttest_rel() - asks to psypy for relative value t-test
- The t-test compares the means of two paired groups to determine if there is a significant difference.
- ‘RTs[:,0]’ and ‘RTs[:,1]’ represent the RTs for dominant and non-dominant hands respectively - of first and second column
Print the results of the paired t-test including the t-value and p-value with 3 significant figures - print(f’Paired t-test t={t:.3} | p={p:.3}’)