简体   繁体   中英

Nonlinear term with unknown in R

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

c 1 (k+ac 2 )/(t+c 2 )

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

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

c 1 (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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM