r of random errors.
>
>Any suggestion is very appreciated.
>
>thanks and best regards,
>Simone
>
>From: Rui Barradas
>Sent: Saturday, December 22, 2012 8:29 PM
>To: Simone Gogna
>Cc: r-help@r-project.org
>Subject: Re: [R] creating a function
>
>Hello,
>
>I
as
Sent: Saturday, December 22, 2012 8:29 PM
To: Simone Gogna
Cc: r-help@r-project.org
Subject: Re: [R] creating a function
Hello,
It's a bad idea to name a function and one of it's arguments Y, use Yfun and Y.
What does summation(x(Y.t, Y.t-1, ...)) mean? Is there a multiplication sign
be
agged values of Y_t+1 from Y_t to Y_t-n. Epsilon_t is, as You
said a vector of random errors.
Any suggestion is very appreciated.
thanks and best regards,
Simone
From: Rui Barradas
Sent: Saturday, December 22, 2012 8:29 PM
To: Simone Gogna
Cc: r-help@r-project.org
Subject: Re: [R] creating a f
Hello,
It's a bad idea to name a function and one of it's arguments Y, use Yfun
and Y.
What does summation(x(Y.t, Y.t-1, ...)) mean? Is there a multiplication
sign between x and (Y.t, ...)?
And is epsilon a vector of errors, one for each Y.t?
If so, the following might do it.
Yfun <- function(
Then what about:
fun<-function(a,b,c,data) {
ifelse(data > c, (a*(data-c)^0.5)+(b*(data-c)), 0)
}
y=c(100,210,320,130,170,120,220,90,55,45)
fun(1,0.2,150,data=y)
> fun(1,0.2,150,data=y)
[1] 0.00 77471.67 130418.05 0.00 44725.36 0.00 83680.00
[8] 0.00 0
Try this -- this uses the vectorization which you need to read up on
in the Intro to R:
> #set the function
> fun<-function(a,b,c,data)
+ {
+ N <- numeric(length(data)) # initialize to zero
+ indx <- data > c # determine which data is greater than c
+ N[indx] <- (a * (data[indx] - c) ^ 0.5) + (b
thank you. yes I want to loop over the elements of the data. I want the
output to be corresponded to the input by unchanging index. at the same
time, the data that is more than c value should follow the
function (a*(data-c)^0.5)+(b*(data-c), and the rest (same or less than c)
should return a zero.
N=as.vector()
This doesn't mean anything in R: you can read the help using
?as.vector
to see what the required arguments are.
Do you want to loop over the elements of data? That's not what your
loop is doing. Instead it is looping over each element of the sequence
from the min to the max *value*
8 matches
Mail list logo