Arithmetic Test R Code (part 4)

>newmath2.trial
function (trial = 1, total.trials = 5, problem=newmath2.problems[1,],condition= “testing”, wait.range=c(1500,2500), num.possible=9, note = “”)
{#give one trial. returns list with components wait, answer.msec, etc.
#(“okay” or “aborted”) and results.
#
#              trial             trial number
#              total.trials      trials per session
#              problem           problem, answer (characters)
#              condition
#              wait.range        range of wait times (msec)
#              num.possible      number of possible wait times
#              note
#
tn=paste(“trial”,trial,”of”,total.trials)
msg=press.space.to.start(below=tn, col = “brown”)
if(msg==”end session”) return(“end session”)
wait.msec=newmath2.foreperiod(wait.range=wait.range, num.possible = num.possible)
t=newmath2.problem(problem=problem)
newmath2.feedback(problem=problem[1],answer.msec=t$answer.msec,correct=t$correct,status=t$status)
list(wait.msec=wait.msec, answer.msec=t$answer.msec,actual.answer=t$actual.answer, correct=t$correct,include=t$include,status=t$status)
}

> press.space.to.start
function (msg = “press space”, prompt = “to start”, below=”", beepf=FALSE, bottom =”press letter to end session”, col = “red”, text.size=5)
{#wait for Enter to start data collection with getGraphicsEvent
#
#              beepf     beep after input?
#
paint(center=prompt, above = msg, below=below, bottom = bottom, text.size=text.size, col = col)
msg=”get answer”
while(msg==”get answer”) {
t=getGraphicsEvent(prompt=”",onKeybd=get.key)
if(beepf) beep()
if (t==” “) msg=”okay”
if (t %in% strsplit(alphabet,split=”")[[1]]){
msg = “end session”
paint(“ending session”)
}
}
msg
}
> newmath2.foreperiod
function (wait.range=c(1000,2000), num.possible = 9)
{#delay for interval randomly selected within given range. Returns
#wait in msec
#
#          wait.range      lower and upper possible delays (msec)
#          num.possible    number of possible waits (which are equally spaced)
#
#
possible.waits=seq(from = wait.range[1],to = wait.range[2], length.out = num.possible)
wait.msec=sample(possible.waits,1)
paint(“|”,duration = wait.msec/1000)
wait.msec
}
> newmath2.problem
function (problem = c(“3+4″,”7″),status = “okay”)
{#show new arithmetic problem. Returns list of latency,
#answer, right/wrong, and note.
#
#                     problem   vector of problem and answer (both characters)
#                     status    status
#
newmath2.show(problem[1])
see.time = Sys.time()
actual.answer=getGraphicsEvent(prompt=”",onKeybd = get.key)
resp.time< <-Sys.time()
answer.msec=as.integer(1000*difftime(resp.time,see.time,unit="sec"))
if(!actual.answer %in% as.character(c(0:9))){
actual.answer=""
answer.msec=NA
correct=NA
include=FALSE
if (an=="q") status = "abort session" else status="abort trial"
}
else {
correct=actual.answer==problem[2]
include=TRUE
status="okay"
}
list(answer.msec=answer.msec,actual.answer=actual.answer,correct=correct, include=include, status = status)
}
> newmath2.feedback
function (trial = 5, problem = “3+4″, actual.answer=7, status= “”, answer.msec = 639, correct = TRUE, number.back=15)
{#give feedback on trial
#
#            trial         trial number
#            problem       problem shown
#            actual.answer actual answer (numeric)
#            correct       correct?
#            status        anything unusual?
#            answer.msec   latency of answer (msec)
#            number.back   compare this rt with how many back?
#
if(status==”abort block”){
paint(“block aborted”,duration = 1.5)
return()
}
if(status==”abort session”){
paint(“session aborted”,duration = 1.5)
return()
}
if(!correct){
paint(“wrong”,duration = 1.5)
return()
}
previous.answer.msec=newmath2$answer.msec[(problem==newmath2$problem)&newmath2$correct&newmath2$include]
ptile=newmath2.ptile(answer.msec,previous.answer.msec)
ptile.msg=paste(round(ptile),”%ile”,sep=”")
whole.msg=paste(answer.msec,”ms”,ptile.msg)
paint(whole.msg, text.size = 4, col = “blue”, duration = 1)
}

> paint
function (center=”",above=”",below=”", bottom = “”, x = 0, y = 0,xlim = c(-1,1),ylim = c(-1,1),text.size=4, small.size = 2, duration = 1, col=”red”, new = TRUE)
{# write text on graph, clearing previous
#
#                center         text for middle of graph
#                above          text above that
#                below          text below that
#                bottom         small text at bottom
#                x              x location of text
#                y              y location
#                xlim
#                ylim
#                text.size      cex value
#                small.size     cex value for small text
#                duration       wait (sec) before continuing
#                col            color
#                new            erase what was there?
#
if (new) plot(0,0,xlab=”",ylab=”",xaxt=”n”,yaxt=”n”,type=”n”, xlim=xlim, ylim = ylim)
text(center,x=x,y=y, cex = text.size, col = col)
text(above,x=x,y=y+.3, cex = text.size, col = col)
text(below,x=x,y=y-.3, cex = text.size, col = col)
text(bottom,x=x,y=y-.85, cex = small.size, col = col)
Sys.sleep(duration)
}

7 Responses to “Arithmetic Test R Code (part 4)”

  1. Justin Wehr Says:

    Thanks for posting these, Seth.

  2. Layna Says:

    It would be fantastic if someone were able to turn this into a simple app that would run on Windows. Non-programmers could use this kind of info on their performance, too!

  3. Roger Levy Says:

    Hi Seth,

    This is really cool and I’m trying to get your code to run, but it seems to be missing the paint() function. Is this your own function, or is it from an R package?

  4. seth Says:

    Roger, sorry for the omission. I have added the paint function, which I wrote.

  5. Evelyn Says:

    http://www.inside-r.org/pretty-r/tool

    A tool for formatting R code for publishing on the web.

  6. Dafydd Says:

    I’ve just spent the day learning R and trying to get this code to work.
    Final hurdle (I think) is that the function newmath2.plot() is missing.
    Any chance Seth can share?

  7. Seth Roberts Says:

    I have sent you an R workspace by email that should work.