R Programs Used to Study Omega-3

I recently posted that flaxseed oil affected how fast I did simple arithmetic problems (a way to measure brain function). Someone asked to see the R programs I used. Here they are.

> arithmetic.add
function (trials=100, condition = “test2″, procedure = “no mistake correction”,note=””)
{# collect new arithmetic data
#
invisible()
start.time=Sys.time()
condition=new.condition()
start.expt()
trls=sample(rep(c(“+”,”-”,”*”),times=c(34,34,34)),size=trials)#determine trial sequence
t=arithmetic[1,]for (tr in 1:trials){
t2=c(current(),condition=condition,procedure=procedure,trial=tr,arithmetic.new.line(func=trls[tr]),note=note)
t=rbind(t,t2)
}
arithmetic<<-rbind(arithmetic,t[-1,])
save.ws()
arithmetic.plot()
cat(“total time”,round(difftime(Sys.time(),start.time, unit=”mins”)),”minutes\n”)
}

> new.condition
function (conditions.so.far = arithmetic$condition)
{# get new condition name
#
# conditions.so.far vector of conditions so far
#
cat(“most recent condition”,last(conditions.so.far),”\nthis condition”)
condition=scan(nlines=1,what=”character”,quiet=TRUE, sep=”!”)
condition
}

> start.expt
function ()
{#wait for Enter to start data collection
#
cat(“press Enter to start “); scan(quiet=TRUE)
}

> arithmetic.new.line
function (func=”+”)
{# give addition, subtraction, or multiplication problem
#
# func function
#
b=sample(0:9,2)
if(func==”+”) answ=b[1]+b[2]if(func==”-”) {b[1]=b[1]+b[2];answ=b[1]-b[2]}
if(func==”*”) answ=b[1]*b[2]start.time=Sys.time()
cat(b[1],func,b[2],”= “)
a=scan(n=1, what = “integer”, quiet = TRUE)
msec=as.integer(1000*difftime(Sys.time(),start.time,unit=”sec”))
correct=a==answ
if(!correct) print(correct)
list(type=func,first.num=b[1],second.num=b[2], msec=msec, answer=as.integer(a),correct=correct)
}

function (vec, n = 1, drop = 0)
{#return last n elements of vec
#
# vec vector
# n number of elements
# drop elements at end to drop before taking n elements
#
le=length(vec)
okay.le=le-drop
vec=vec[1:okay.le]le=length(vec)
ind=(1+le-n):le
vec[ind]}

If you would like help using these programs, please contact me.

3 thoughts on “R Programs Used to Study Omega-3

  1. Wow, to somebody like me who doesn’t know much about statistics, this looks like a very impressive formula. My 16-year old daughter wants to study psychology, but doesn’t like maths. I’m not sure whether I should show her it or not.

  2. I can’t get this to work. There is a function called last() that is called by new.condition() that I don’t have on my build of R. Is this a function you forgot to include?

  3. oops. here it is:

    function (vec, n = 1, drop = 0)
    {#return last n elements of vec
    #
    # vec vector
    # n number of elements
    # drop elements at end to drop before taking n elements
    #
    le=length(vec)
    okay.le=le-drop
    vec=vec[1:okay.le]
    le=length(vec)
    ind=(1+le-n):le
    vec[ind]
    }

Leave a Reply

Your email address will not be published. Required fields are marked *