[R] certain number of equations in function depending on parameter

Andreas Wittmann andreas_wittmann at gmx.de
Wed Sep 19 08:40:09 CEST 2007


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
--



More information about the R-help mailing list