0

I'm trying to build a nonlinear model as described in the manual for gmn in R. The desired form of the model is

y = b0*x0^g0 + b1*x1^g1 + ...

This would seem to be the simplest possible form of nonlinear model to me, but for some reason (and please correct me if I'm wrong!) I have to write a custom nonlinear function to fit it in R. Very well!

df=read.csv("d:/mydataframe.csv")
require(gnm)

mypower = function(x){
  list(predictors = list(beta=1,gamma=1),
       variables = list(substitute(x)),
       term = function(predlabels,varlabels) {
       paste(predlabels[1],"*(",varlabels[1],"**",predlabels[2],")")
       }
  )
}
class(mypower) <- "nonlin"

Now when I try

fit <- gnm(formula=y ~ mypower(x1), data=df)

I get a fitted value of beta and gamma from the model. But when I try

fit <- gnm(formula=y ~ mypower(x1)+mypower(x2), data=df)

I get the error

Algorithm failed - no model could be estimated.

So, question 1: how can I solve this?

Also, when - trying to match all xs - I try

fit <- gnm(formula=PedalCycles ~ mypower(.), data=df)

I get

Error in eval(expr, envir, enclos) : object '.' not found

Is this the right way to specify a sum of all xs each raised to a power?

Sideshow Bob
  • 4,566
  • 5
  • 42
  • 79

1 Answers1

3

To estimate y = b_0*x_0^g_0 you could use gnm's built-in Exp() to estimate

Exp( 1 + I( log(x_0) ) )

This gives you coefficients:

  1. b'_0 for the intercept
  2. g'_0 for log(x_0)

Hence g'_0 is your desired g_0 (since e^log(x_0)*g'_0 = x_0^g_0) and e^b'_0 is b_0. Your model is now a sum of such terms.

Caveat: This will not work if x_0 assumes non-positive values in your data set.

coatless
  • 20,011
  • 13
  • 69
  • 84