[R] obtaining output from an evaluated expression

2008-12-19 Thread Jack Bowden

Hi

I am trying to use the deriv and eval functions to obtain the value of a 
function , say  "xi-(alpha0+alpha1*gi)" , differentiated with respect to 
alpha0 and alpha1, in the following way


# for gi = 0

> dU1dtheta  <- deriv(~ xi-(alpha0+alpha1*gi), c("alpha0","alpha1")) 
> eval(dU1dtheta)
(Intercept) 
-0.2547153 
attr(,"gradient")

alpha0 alpha1
[1,] -1  0

I want to extract the output gradient values of -1 and 0 but I don't 
know how to access them. The only thing I can access is the intercept  
term via.


> eval(dU1dtheta)[1].

I'm sorry if this is too basic a question for the list, but any help 
would be greatly appreciated


Jack

--
Dr Jack Bowden
MRC Biostatistics Unit
Institute of Public Health
Forvie Site
Robinson Way
Cambridge
CB2 0SR

Tel: (01223) 330385

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] obtaining output from an evaluated expression

2008-12-19 Thread Dieter Menne
Jack Bowden  mrc-bsu.cam.ac.uk> writes:

>  > dU1dtheta  <- deriv(~ xi-(alpha0+alpha1*gi), c("alpha0","alpha1")) 
>  > eval(dU1dtheta)
> (Intercept) 
>  -0.2547153 
> attr(,"gradient")
>  alpha0 alpha1
> [1,] -1  0
> 
> I want to extract the output gradient values of -1 and 0 but I don't 
> know how to access them. The only thing I can access is the intercept  
> term via.
> 
>  > eval(dU1dtheta)[1].
> 


It probably easiest to ask for a function to be returned using function.arg, as
the example in deriv shows. Having been bitten by deriv in the past, I prefer to
use it to give me the function or expression, and paste it explicitly into my
code, doing some sanity check first. It's not very general, but tells you better
what is happening.

Dieter
 
## function with defaulted arguments:
(fx <- deriv(y ~ b0 + b1 * 2^(-x/th), c("b0", "b1", "th"),
 function(b0, b1, th, x = 1:7){} ) )
fx(2,3,4)
attr(fx(2,3,4),"gradient")

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] obtaining output from an evaluated expression

2008-12-19 Thread Prof Brian Ripley

On Fri, 19 Dec 2008, Dieter Menne wrote:


Jack Bowden  mrc-bsu.cam.ac.uk> writes:


> dU1dtheta  <- deriv(~ xi-(alpha0+alpha1*gi), c("alpha0","alpha1"))
> eval(dU1dtheta)
(Intercept)
 -0.2547153
attr(,"gradient")
 alpha0 alpha1
[1,] -1  0

I want to extract the output gradient values of -1 and 0 but I don't
know how to access them. The only thing I can access is the intercept
term via.

> eval(dU1dtheta)[1].


It probably easiest to ask for a function to be returned using function.arg, as
the example in deriv shows. Having been bitten by deriv in the past, I prefer to
use it to give me the function or expression, and paste it explicitly into my
code, doing some sanity check first. It's not very general, but tells you better
what is happening.


But he is stil going to need to learn about attr():

attr(eval(dU1dtheta)[1], "gradient")

does the job (I presume, we don't have all the data in this message, nor I 
think in the previous one although you inexplicably silently removed the 
line that said gi = 0).




Dieter

## function with defaulted arguments:
(fx <- deriv(y ~ b0 + b1 * 2^(-x/th), c("b0", "b1", "th"),
function(b0, b1, th, x = 1:7){} ) )
fx(2,3,4)
attr(fx(2,3,4),"gradient")


--
Brian D. Ripley,  rip...@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.