Tip—Getting participants ages in decimal form & avoiding identification

If this template helps then use it. If not then just delete and start from scratch.

OS (e.g. Win10):
PsychoPy version (e.g. 1.84.x):
Standard Standalone? (y/n) If not then what?:
What are you trying to achieve?:
Just sharing a tip!

  • Usually, we want to take some record of participants’ ages, but there are a few potential problems:
    • If we ask for their date-of-birth, they could be identified, which creates potential problems with privacy, GDPR, etc.
    • We could just ask for the year-of-birth to avoid identification. But, often undergraduates will participate and there will be little variability in their year-of-birth. This means that if we express the mean age with a decimal point (e.g., 20.35 years), were giving the impression of a greater level of resolution than is true. It also means that parametric tests (e.g., a Pearson correlation between age and an experimental variable) are probably illegitimate.
    • The mid-point, which seems to avoid all of these problems, is to ask for only month and year of birth, but not date.
  • If we ask participants to enter their month and year of birth in the Experimental Info section, we’ll end up with a format that isn’t immediately useful. We’ll need to work out the age in fractional years, based on the date that data were collected.
  • This recipe adds a decimal format age, at the time of the experiment…

In Experimental Info

Month of birth
	['1', '2', '3', '4', '4', '5', '6', '7', '8', '9', '10', '11', '12']

Year of birth (e.g. 2001)

	* ['Female','Male']
  • The text in square brackets forms a drop-down menu when the participant runs.
  • I didn’t use a drop-down menu for the year-of-birth because it was potentially too long. They just enter the text, e.g., 2001.
  • I included sex, too.
    • When I used a raw text field, people used a mixture of upper- and lower case, which were all interpreted by my stats app as different levels of sex.
    • Using a drop-down menu avoids

Add a Code Component in a Routine, before the experiment starts properly…

Before Experiment import datetime

  • This gets the current date and time, which is used to work out how old the participant was when the data were collected.

Begin Experiment

# Calculate number of days old based on month & year of birth and today's date. 

# Establish the variables...
# Variable declaration with dummy values. 
MoB = ''
YoB = ''
DoB = ''
Today = ''
Age = ''

# Calculation
# https://stackoverflow.com/questions/56192936/how-do-i-calculate-year-fraction-from-datetime-objects-in-python

MoB = int(expInfo['Month of birth'])
YoB = int(expInfo['Year of birth (e.g. 2001)'])
DoB = datetime.datetime(YoB, MoB,15) #average month is 30.4 days :. 15 is midpoint. 

Today = datetime.datetime.now()
delta = Today - DoB
delta_fraction = delta.days/365.25

Age = (round(delta_fraction,2))

  • This works out the participant’s age in decimal year units.

  • It makes various assumptions about average year length, etc.

  • So far, this will create the age but it won’t be visible.

    • I used a Code Component at the “Begin Routine” tab to include the age. This was in a section of the experiment that I was recording data from. So whenever a row was added to the output file, the age was included too:

thisExp.addData('Age',Age)                  # add participant's AGE to output file.

1 Like