[R] Solving Equations

Berend Hasselman bhh at xs4all.nl
Sun Jan 22 20:15:15 CET 2012


On 22-01-2012, at 19:25, Eliano wrote:

> People,
> 
> I'm researching some Bayesian statistic topics and in the midle of my study
> i found a very simple problem and i'm trying to find a simple package to
> solve this type of equations:
> 
> Lets say that i need to compute beta values for the beta distribution and i
> now for example:
> 
> E(teta)=a/(a+b) = 0,5
> Var(teta)=ab/((a+b)^2(a+b+1))=0.05
> 
> So if i want to solve this to non-linear system to find a,b for the beta
> distribution wich pack should i use?
> 
> I know that a=2 and b=2 because i calculated it. 
> 

There is a package nleqslv which can handle you system.


library(nleqslv)

eqf <- function(x) {
    a <- x[1]
    b <- x[2]
    y <- numeric(2)   
    y[1] <- a/(a+b) - 0.5
    y[2] <- a*b/((a+b)^2 * (a+b+1)) -0.05
    y
}
       
x.start <- c(1,1)

nleqslv(x.start,eqf)


Berend



More information about the R-help mailing list