Andreas Wittmann
2007-Sep-19 06:40 UTC
[R] certain number of equations in function depending on parameter
Hello everybody,
i have the following problem to write a function which recognizes depending
on the parameter-inputs how many equations for the calculation in the function
are needed.
Here is an example of my problem:
"myfun" <- function(a, b, c, d)
{
k <- length(a)
#here d = 3 for example, but how can i dynamically controll
#my function and tell her to build equations eq1 to eq5 if d = 5?
"eq1" <- function(a, b, y)
{
c[k-1] <- a[k-1] + b * y
}
"eq2" <- function(a, b, y)
{
c[k-2] <- a[k-2] + b * y
}
"eq3" <- function(a, b, y)
{
c[k-3] <- a[k-3] + b * y
}
"eq4" <- function(a, b, z)
{
1 - sum(c(eq1(z), eq2(z), eq3(z), z))
}
sol <- uniroot(eq4, lower=0, upper=1)
}
I hope my problems is explained clear enough. I would be very happy
if you can give me some advice.
best regards
Andreas
--
Moshe Olshansky
2007-Sep-25 00:36 UTC
[R] certain number of equations in function depending on parameter
Hi Andreas, One possible way would be to create a file containing all your equations (which are written in a loop), then source that file and then use uniroot. Regards, Moshe. --- Andreas Wittmann <andreas_wittmann at gmx.de> wrote:> Hello everybody, > > i have the following problem to write a function > which recognizes depending > on the parameter-inputs how many equations for the > calculation in the function > are needed. > > Here is an example of my problem: > > "myfun" <- function(a, b, c, d) > { > k <- length(a) > #here d = 3 for example, but how can i > dynamically controll > #my function and tell her to build equations eq1 > to eq5 if d = 5? > > "eq1" <- function(a, b, y) > { > c[k-1] <- a[k-1] + b * y > } > > "eq2" <- function(a, b, y) > { > c[k-2] <- a[k-2] + b * y > } > > "eq3" <- function(a, b, y) > { > c[k-3] <- a[k-3] + b * y > } > > "eq4" <- function(a, b, z) > { > 1 - sum(c(eq1(z), eq2(z), eq3(z), z)) > } > > sol <- uniroot(eq4, lower=0, upper=1) > } > > I hope my problems is explained clear enough. I > would be very happy > if you can give me some advice. > > > best regards > > Andreas > -- > > ______________________________________________ > 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. >