Greetings R members.
I have a few vectors that denote the 'steps' of different step functions
v1=c(3,4,5,1,2,3,4,5)
v2=c(5,6,2,4,7,3,2,5)
v3=c(1,2,4,7,3,1,3,5)
Here v1,v2,v3 are considered as the steps for the f1,f2,f3 step functions.
For example f1 looks like that (step size is always same and fixed)
f1= 3 (x>=-3,x<-2)
f1= 4 (x>=-2,x<-1)
f1= 5 (x>=-1,x<0)
f1= 1 (x>=0, x<1)
and so on.
What I only have are these vectors that are interpreted as functions (as shown above, x (step is 1 here). I would like to ask your help of how I can create some function that reads one of the vector v1,v2,v3.... and returns results that are acceptable by integrate() function.
Usually integrate wants a pre-defined function like
myfunc(x)<-function{ x^2 }
but this is not the case here.
Could you please give me some hints how I can proceed?
I would like to thank u in advance for your help
Regards
Alex
From vector to a step function
6 messages · Alaios, jim holtman, Greg Snow +1 more
try this:
f.main <- function(vec){
+ breaks <- seq(-3, by = 1, length = length(vec) + 1L)
+ function(x){
+ indx <- findInterval(x, breaks)
+ vec[indx]
+ }
+ }
f1 <- f.main(c(3,4,5,1,2,3,4,5)) f2 <- f.main(c(5,6,2,4,7,3,2,5)) f3 <- f.main(c(1,2,4,7,3,1,3,5)) f1(c(-2.5,-2,-.5,0,.5,1.5,2.5))
[1] 3 4 5 1 1 2 3
f2(c(-2.5,-2,-.5,0,.5,1.5,2.5))
[1] 5 6 2 4 4 7 3
f3(c(-2.5,-2,-.5,0,.5,1.5,2.5))
[1] 1 2 4 7 7 3 1
On Mon, Jan 10, 2011 at 11:42 AM, Alaios <alaios at yahoo.com> wrote:
Greetings R members.
I have a few vectors that denote the 'steps' of different step functions
v1=c(3,4,5,1,2,3,4,5)
v2=c(5,6,2,4,7,3,2,5)
v3=c(1,2,4,7,3,1,3,5)
Here v1,v2,v3 are considered as the steps for the f1,f2,f3 step functions.
For example f1 looks like that (step size is always same and fixed)
f1= 3 (x>=-3,x<-2)
f1= 4 (x>=-2,x<-1)
f1= 5 (x>=-1,x<0)
f1= 1 (x>=0, x<1)
and so on.
What I only have are these vectors that are interpreted as functions (as shown above, x (step is 1 here). I would like to ask your help of how I can create some function that reads one of the vector v1,v2,v3.... and returns results that are acceptable by integrate() function.
Usually integrate wants a pre-defined function like
myfunc(x)<-function{ x^2 }
but this is not the case here.
Could you please give me some hints how I can proceed?
I would like to thank u in advance for your help
Regards
Alex
______________________________________________ R-help at 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.
Jim Holtman Data Munger Guru What is the problem that you are trying to solve?
?approxfun
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Alaios
> Sent: Monday, January 10, 2011 9:43 AM
> To: r-help at r-project.org
> Subject: [R] From vector to a step function
>
> Greetings R members.
>
> I have a few vectors that denote the 'steps' of different step
> functions
> v1=c(3,4,5,1,2,3,4,5)
> v2=c(5,6,2,4,7,3,2,5)
> v3=c(1,2,4,7,3,1,3,5)
>
> Here v1,v2,v3 are considered as the steps for the f1,f2,f3 step
> functions.
>
> For example f1 looks like that (step size is always same and fixed)
>
> f1= 3 (x>=-3,x<-2)
> f1= 4 (x>=-2,x<-1)
> f1= 5 (x>=-1,x<0)
> f1= 1 (x>=0, x<1)
> and so on.
>
> What I only have are these vectors that are interpreted as functions
> (as shown above, x (step is 1 here). I would like to ask your help of
> how I can create some function that reads one of the vector
> v1,v2,v3.... and returns results that are acceptable by integrate()
> function.
>
> Usually integrate wants a pre-defined function like
> myfunc(x)<-function{ x^2 }
> but this is not the case here.
>
> Could you please give me some hints how I can proceed?
>
> I would like to thank u in advance for your help
>
> Regards
> Alex
>
> ______________________________________________
> R-help at 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.
Dear Jim Holtman, I would like to thank you for your help. Just to update also the community that worked fine :) Best Regards Alex
--- On Mon, 1/10/11, jim holtman <jholtman at gmail.com> wrote:
From: jim holtman <jholtman at gmail.com> Subject: Re: [R] From vector to a step function To: "Alaios" <alaios at yahoo.com> Cc: r-help at r-project.org Date: Monday, January 10, 2011, 5:44 PM try this:
f.main <- function(vec){
+? ???breaks <- seq(-3, by = 1,
length = length(vec) + 1L)
+? ???function(x){
+? ? ? ???indx <-
findInterval(x, breaks)
+? ? ? ???vec[indx]
+? ???}
+ }
f1 <- f.main(c(3,4,5,1,2,3,4,5)) f2 <- f.main(c(5,6,2,4,7,3,2,5)) f3 <- f.main(c(1,2,4,7,3,1,3,5)) f1(c(-2.5,-2,-.5,0,.5,1.5,2.5))
[1] 3 4 5 1 1 2 3
f2(c(-2.5,-2,-.5,0,.5,1.5,2.5))
[1] 5 6 2 4 4 7 3
f3(c(-2.5,-2,-.5,0,.5,1.5,2.5))
[1] 1 2 4 7 7 3 1
On Mon, Jan 10, 2011 at 11:42 AM, Alaios <alaios at yahoo.com> wrote:
Greetings R members. I have a few vectors that denote the 'steps' of
different step functions
v1=c(3,4,5,1,2,3,4,5) v2=c(5,6,2,4,7,3,2,5) v3=c(1,2,4,7,3,1,3,5) Here v1,v2,v3 are considered as the steps for the
f1,f2,f3 step functions.
For example f1 looks like that (step size is always
same and fixed)
f1= 3 (x>=-3,x<-2) f1= 4 (x>=-2,x<-1) f1= 5 (x>=-1,x<0) f1= 1 (x>=0, x<1) and so on. What I only have are these vectors that are
interpreted as functions (as shown above, x (step is 1 here). I would like to ask your help of how I can create some function that reads one of the vector v1,v2,v3.... and returns results that are acceptable by integrate() function.
Usually integrate wants a pre-defined function like
myfunc(x)<-function{ x^2 }
but this is not the case here.
Could you please give me some hints how I can
proceed?
I would like to thank u in advance for your help Regards Alex
______________________________________________ R-help at 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.
-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve?
Dear all. I would like to use legendre polynomials which is something pretty easy in R. x<-legendre.polynomials(2)[[3]]
x
-0.5 + 1.5*x^2
str(x)
Class 'polynomial' num [1:3] -0.5 0 1.5 As you can see from the code above str(x) returns that x is of class polynomial. I want to use that polynomial as a function. The reason for that is that I would be grateful if I can feed that kind of function inside integrate(f,lower=,upper=) Could you please inform if it is possible to do that in R? I would like to thank you in advance for your help Best Regards Alex
Alaios wrote:
x<-legendre.polynomials(2)[[3]]
x
-0.5 + 1.5*x^2
str(x)
Class 'polynomial' num [1:3] -0.5 0 1.5 As you can see from the code above str(x) returns that x is of class polynomial. I want to use that polynomial as a function. The reason for that is that I would be grateful if I can feed that kind of function inside integrate(f,lower=,upper=)
You can use the ?as.function? function. But if you?re just going to integrate the polynomial anyway, why don?t you just use the ?integral? function? It?s much more accurate than using numerical integration. BTW, to see which functions handle ?polynomial? objects, use methods(class="polynomial") Output: [1] as.character.polynomial* as.function.polynomial* coef.polynomial* [4] deriv.polynomial* GCD.polynomial* integral.polynomial* [7] LCM.polynomial* lines.polynomial* Math.polynomial* [10] Ops.polynomial* plot.polynomial* points.polynomial* [13] predict.polynomial* print.polynomial* solve.polynomial* [16] summary.polynomial* Summary.polynomial*
Karl Ove Hufthammer