Naive_Natural2511
Naive_Natural2511

Reputation: 717

How to solve set of equations for two unknowns using R?

I have two equations. They are as follows:

( 1 - 0.25 ^ {1/alpha} ) * lambda = 85

( 1 - 0.75 ^ {1/alpha} ) * lambda = 11

I would like to compute the values of alpha and lambda by solving the above two equations. How do I do this using R?

Upvotes: 1

Views: 2128

Answers (2)

Naive_Natural2511
Naive_Natural2511

Reputation: 717

@olooney's answer is best.

Another way to solve these equations is to use uniroot function. We can cancel the lambda values and can use the uniroot to find the value of alpha. Then substitute back to find lambda.

f <- function(x) {

  (11/85) - ((1 - (0.75) ^ (1/x)) / (1 - (0.25) ^ (1/x)) )

}

f_alpha <- uniroot(f, lower = -10, upper = -1, extendInt = "yes")

f_lambda <- function(x) {

  11 - ((1 - (0.75) ^ (1/f_alpha$root)) * x)

}

lambda = uniroot(f_lambda, lower = -10, upper = -2, extendInt = "yes")$root

sprintf("Alpha equals %f", f_alpha$root)
sprintf("Lambda equals %f", lambda)

results in

[1] "Alpha equals -1.287978"
[1] "Lambda equals -43.952544"

Upvotes: 1

olooney
olooney

Reputation: 2483

One approach is to translate it into an optimization problem by introducing an loss function:

loss <- function(X) {
  L = X[1]
  a = X[2]
  return(sum(c(
    (1 - 0.25^(1/a))*L - 85, 
    (1 - 0.75^(1/a))*L - 11
  )^2))
}

nlm(loss, c(-1,-1))

If the result returned from nlm() has a minimum near zero, then estimate will be a vector containing lambda and alpha. When I tried this, I got an answer that passed the sniff test:

> a = -1.28799
> L = -43.95321
> (1 - 0.25^(1/a))*L
[1] 84.99999
> (1 - 0.75^(1/a))*L
[1] 11.00005

Upvotes: 3

Related Questions