[R] deriv; loop

jim holtman jholtman at gmail.com
Tue Jul 31 01:08:48 CEST 2007


for question 1, is this what you want (BTW allocate 'result' to the
size you want - the example keeps extending it which is OK for small
numbers, but for larger size preallocate):

> result <- numeric(0)
> for (i in 1:6) result[i] <- i
> result
[1] 1 2 3 4 5 6
> prod(result)
[1] 720


On 7/29/07, francogrex <francogrex at mail.com> wrote:
>
> Hi, 2 questions:
>
> Question 1: example of what I currently do:
>
> for(i in 1:6){sink("temp.txt",append=TRUE)
> dput(i+0)
> sink()}
> x=scan(file="temp.txt")
> print(prod(x))
> file.remove("C:/R-2.5.0/temp.txt")
>
> But how to convert the output of the loop to a vector that I can manipulate
> (by prod or sum etc), without having to write and append to a file?
>
> Question 2:
>
> > deriv(~gamma(x),"x")
>
> expression({
>    .expr1 <- gamma(x)
>    .value <- .expr1
>    .grad <- array(0, c(length(.value), 1), list(NULL, c("x")))
>    .grad[, "x"] <- .expr1 * psigamma(x)
>    attr(.value, "gradient") <- .grad
>    .value
> })
>
> BUT
>
> > deriv3(~gamma(x),"x")
> Error in deriv3.formula(~gamma(x), "x") : Function 'psigamma' is not in the
> derivatives table
>
> What I want is the expression for the second derivative (which I believe is
> trigamma(x), or psigamma(x,1)), how can I obtain that?
>
> Thanks
> --
> View this message in context: http://www.nabble.com/deriv--loop-tf4166283.html#a11853456
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>


-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list