6

I have a logistic regression using glm and I would like to add a term of the form

c1(k+ac2)/(t+c2)

where k and t are columns in a data frame, a is a constant. I would like R to find best-fit values for c1 and c2. Is this possible?

If I only wanted a fixed value, say c2 = 2,

c1(k+2a)/(t+2)

I could just write

glm( model$y ~ I((model$k + 2*a)/(model$t + 2)) + model$otherterms,
  family = binomial(logit) )

which is similar to what I am doing now. But I don't think that 2 is optimal and iterating 'manually' is very time-consuming.

Heather Turner
  • 3,264
  • 23
  • 30
Charles
  • 11,269
  • 13
  • 67
  • 105

1 Answers1

4

You can use function gnm from package gnm.

gnm(y~Mult(1, # c1
           offset(k)+1,# c3=a*c2 
           Inv(offset(t)+1)) # c2
           +other terms, 
    family=binomial, 
    data=models)

EDIT (solution for constrained coefficients)

term_fun <- function(predLabels, varLabels){
                     paste0(predLabels[1],"*(",varLabels[1],
                            "+",predLabels[2],"*3)/(", # a=3 for example
                            varLabels[2],"+", predLabels[3],")")}

  Ratio <- function(t,x){
   list(predictors = list(C1 = 1, C2 = 1),
        variables = list(substitute(t), substitute(x)),
        term = term_fun)
  }
  class(Ratio) <- "nonlin"

  fit <- gnm(Y~Ratio(k,t), data=models, family=binomial)
Wojciech Sobala
  • 7,431
  • 2
  • 21
  • 27