Yes this is the way to go. Don't pass the functions as strings, but as
functions. Recall that in R EVERYTHING is an object including any function
you define so you can pass them just as you pass scalars, matrices, etc.
jens
From: gov2001-l-bounces at
lists.fas.harvard.edu
[mailto:gov2001-l-bounces at
lists.fas.harvard.edu] On Behalf Of Brett Logan
Carter
Sent: Saturday, February 23, 2008 2:04 AM
To: gov2001-l at
lists.fas.harvard.edu
Subject: Re: [gov2001-l] Converting strings to... not strings?
John,
As you said, the terms are a bit confusing, but I think I did what you're
attempting to. My function begins:
bisection <- function(fn, a, b, tol, iter.max){
fx.i <- fn((a+b)/2)
where fx.i is simply the function fn - which is taken as an argument -
evaluated at the midpoint of the interval [a,b], as slide 12 from section
suggests. The input arguments are then stipulated as:
fn <- function(x){12*(x^3) - 12*(x^2) - 24*x}
tol <- .00001
iter.max <- 100000
a <- -3
b <- -.5
bisection(fn, a, b, tol, iter.max)
So - and I think this is what you're getting at - the bisection function
takes any f(x), which I define as fn <- etc. Apologies if I misunderstood
your email.
Best,
Brett
_____
From: gov2001-l-bounces at
lists.fas.harvard.edu
[mailto:gov2001-l-bounces at
lists.fas.harvard.edu] On Behalf Of John Sheffield
Sent: Saturday, February 23, 2008 1:47 AM
To: gov2001-l at
lists.fas.harvard.edu
Subject: [gov2001-l] Converting strings to... not strings?
Terminology got confusing, so I refer to R functions as functions and
functions of variables as f(x).
Does anyone know how to take a string (eg"x^3+4" or something like that),
and change it to an ordinary variable? I want to be able to send whatever
f(x) we want to the bisection function without having to write it as an R
function, i.e. to send f(x) as a parameter to a function, which I can't do
without defining x (and making f(x) just a discrete value). My solution, if
anyone can tell me how to do it, is to send f(x) as a string and then
convert it to an R function within the bisection function. Are there any
solutions to the string question, or suggestions to make it easier?
Thanks,
J
(If you're afraid of letting the list know that you're doing Gov2001 at 2am
on a Friday night, I'll take a personal email and promise not to tell.)