Dear list members,
I'm quite new to R, and though I tried to find the answer to my probably
very basic question through the available resources (website, mailing
list archives, docs, google), I've not found it.
If I try to use the "integrate" function from within my own functions,
my function
Varadhan.html
-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of theo borm
Sent: Friday, February 23, 2007 1:43 PM
To: r-help@stat.math.ethz.ch
Subject: [R] using "integrate" in a function definition
Dear list mem
theo borm wrote:
>
>> jjj<-function(www) {2*integrate(dnorm,0,www)$value}
>> kkk<-function(www) {2*(pnorm(www)-0.5)}
>> xxx<-seq(0:5)
>> yyy<-jjj(xxx)
>> zzz<-kkk(xxx)
>
> produces no errors, but:
>> yyy
> [1] 0.6826895
>> zzz
> [1] 0.6826895 0.9544997 0.9973002 0.367 0.994 1.0
Ravi Varadhan wrote:
>Your function "jjj" is not vectorized.
>
>Try this:
>
>jjj <- function(www) sapply(www, function(x)2*integrate(dnorm,0,x)$value)
>plot(jjj, 0, 5)
>
>It should work.
>
>
Yes it does. Thanks!
Thinking of it, it now starts to make some sort of sense that integrate
should ret
Hi,
Many thanks for the explanation.
Alberto Monteiro wrote:
>
>PS: fff <- function(x) 1
>integrate(fff, 0, 1) # error. why?
>
Guess: because integrate itself expects a "vectorized function" ?
> fff(1:5)
[1] 1
> ggg<-function(x) { sapply(x, function(x)1) }
> ggg(1:5)
[1] 1 1 1 1 1
> inte