Home > Software design >  Optimize / solve equation for unknown exponent
Optimize / solve equation for unknown exponent

Time:07-09

I have dataframe with the following variables: a and b as predictors and c as outcome. My formula is: c = (a^x) / (a^x b^x) How to solve for x?

Example data:

dat <- data.frame(a = runif(5, 1, 100), b = runif(5, 10, 20), c = runif(5, 0, 1))

Reply to comment:

What is your expected output? A single x-value from least squares fitting, or a column x?

The whole column (sum of all row errors). I want to minimize the error for every row.

CodePudding user response:

You can use the following code

library(minpack.lm)

dataset = data.frame(a = runif(5, 1, 100), b = runif(5, 10, 20), c = runif(5, 0, 1)) 

fun <- as.formula(c ~ a^x/(a^x   b^x))

#Fitting model using minpack.lm package
nls.out1 <- nlsLM(fun, 
                  data = dataset,
                  start=list(x=1),
                  algorithm = "LM",
                  control = nls.lm.control(maxiter = 500))

summary(nls.out1)
  •  Tags:  
  • r
  • Related