[R] Fucntion scope question. General non-linear solution help.
rkevinburton at charter.net
rkevinburton at charter.net
Mon Aug 18 06:56:38 CEST 2008
I would like to solve the equation is is the sum from k = i to N of
choose(N,k) * MR ^ k * (1 - MR) ^ (N - k) - 0.50 = 0
I want to solve for MR. This seems like a non-linear equation to me. But I am having a hard time writing the function that implements the above. I could use 'for(...) as a brute force appoarch but I would like a more "elegant" solution. The variables 'N' and 'i' are basically constant so the function has to take these from some kind of global space. So if I take t brute force apporach I came up with:
f <- function(MR)
{
k <- i:N
return sum(choose(N,k) * MR ^ k * (1 - MR) ^ (N - k)) - 0.5
}
Does this seem like a reasonable implemetation? How are 'N' and 'i' declare as "global"? For each equation N and I are constant but I want to be able to modify them. In other words solve the equantion after setting N to 6 and i to 5 then again after setting i to 4.
The next question is regarding which 'R' function would be best suited to solving this equation? I looked at 'nls' but that seems to take data as an input. I want to solve the equation. What other options do I have? There must be an 'R' function to solve a non-linear equation. I did help.search("non-linear") and the closest match was nlm. But nlm minimizes the function rather than solving it.
Ideas?
Thank you.
Kevin
More information about the R-help
mailing list