Arithmetic Test R Code (part 1)

[Below is some of the R code that runs the arithmetic test that I use to measure my brain function. This function (newmath2.add) is the top-level function -- the function I actually call when I run the test. Later posts will give the subroutine code. The variable newmath2 is the database -- the variable (a data frame) that holds the data.]

function (trials = 32, note=”", wait.range=c(1000,2000), num.possible = 9)
{#Like newmath but with separated trials.
#
#Collect data with arithmetic-like task. Simple arithmetic problems with
#the answer being a single digit. 1, 2, 3, 4, 7, 8, 9, 0 equally likely.
#If the answer is two digits (e.g., 12) it is truncated to the last digit
#(e.g., 2). The trials during each session are sampled without replacement
#from all possible problems.
#
#Trials where the response is incorrect are repeated with a new problem.
#Trials can be aborted; this is noted.
#
#               trials            trials in a session
#               note              comment for each trial
#               wait.range        range of wait times (msec)
#               num.possible      number of possible wait times
#
# 2009.07.11 Fixation symbol changed from + to o. Repetition of
# answer from one question to the next no longer allowed.
# 2009.07.15 Feedback now based only on answers to the same question
# More sophisticated computation of percentile.
# 2009.08.07 Can end session from Press Space screen
# 2009.10.23 fixed abort session and abort trial
#
invisible()
condition=new.condition(newmath2$condition)
start.time=Sys.time()
n=nrow(newmath2.problems)
problems=newmath2.problems[sample(n),]
okay=c(TRUE,!problems[1:(n-1),2]==problems[2:n,2])
problems=problems[okay,]
tr=1
pr=1
while(tr< =trials){
t=newmath2.trial(trial=tr, total.trials = trials, wait.range=wait.range, num.possible = num.possible, problem = problems[pr,], condition=condition, note=note)
if(t[1]==”end session”) break
if(t$status!=”okay”) {
if(t$status==”abort trial”) this.trial.note=”trial aborted”
if(t$status==”abort session”) this.trial.note=”session aborted”
t$include=FALSE
}
else this.trial.note=note
new.line1=c(current(),condition,tr,t$wait.msec, problems[pr,1])
new.line2=c(t$answer.msec,t$actual.answer,t$correct,t$include,this.trial.note)
newmath2< <-rbind(newmath2,c(new.line1,new.line2))
newmath2.set.types()
pr=pr+1
if(t$status==”abort session”) break
if(t$status==”abort trial”) next
if(!t$correct) next
tr=tr+1
}
msg=paste(“total time”,round(difftime(Sys.time(),start.time, unit=”mins”),1),”minutes\n”)
paint(above = “all done”, below=msg, duration = 3)
save.ws()
newmath2.plot()
}

9 Responses to “Arithmetic Test R Code (part 1)”

  1. Andrew Gelman Says:

    Wow–this looks more like Fortran than any R code I’ve ever seen!

  2. seth Says:

    And I thought these posts would receive no comments…

  3. Evelyn Says:

    There are quite a few formatting changes in the code, because of the blog publishing software.

    Could you publish it to a pastebin?

    http://pastebin.org/

    And Andrew’s comment made me laugh out loud.

  4. seth Says:

    Evelyn, I cut-and-pasted the code from my blog into R without any problem. The formatting changes in the blog (differences between how it looks in R and how it looks in my blog) disappeared when it was back in R. Before I cut-and-pasted it, I made the type on my blog relatively small to make sure that there were no inserted line breaks.

  5. Bryce Says:

    Seth, it may have worked for you, but because of character encoding weirdness there are some issues when pasting the code in some environments (for me pasting from chrome on OS X into my iterm emacs -nw session). R sees some of the quotes as \342, as well as some additional significant white space.

    I’ve cleaned it up a little bit and pasted it here:

    http://pastebin.org/146455

  6. jimmy Says:

    hi seth, can you give some context? why are you running these tests to measure your brain function? like do you want to make sure you are at your sharpest before operating heavy machinery?

  7. seth Says:

    Jimmy, I want to understand what makes my brain work better and worse — so I can make it work better. See my earlier posts about “mysterious mental improvement” for details.

  8. Peter Says:

    Like others I’m intrigued by this code. I couldn’t get it to run without knowing the code for the paint function. Any chance you could share that with us?

  9. Seth Roberts and Citizen Science « My Year of Data Says:

    [...] on a standardized test of simple arithmetic questions that he takes every day.  (He has made R code available for his daily math [...]