[R] Fucntion scope question. General non-linear solution help.
Prof Brian Ripley
ripley at stats.ox.ac.uk
Mon Aug 18 08:32:48 CEST 2008
Looks like you want to solve pbinom(k-1, N, p) = 0.5 for p. That is easy:
testit <- function(k, N)
fn <- function(p, k, N) pbinom(k-1, N, p) - 0.5
uniroot(fn, c(0,1), k=k, N=N)
On Sun, 17 Aug 2008, rkevinburton at charter.net wrote:
> 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
That's not what you have below: I presume that you want the sum to be 0.5.
The sum is 1 - pbinom(k-1, N, MR).
> 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.
> Thank you.
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help