Search code examples
rlogistic-regressionnon-linear-regression

Nonlinear logistic regression package in R


Is there an R package that performs nonlinear logistic regression?

In more words: I have glm, with which I can go glm (cbind (success, failure) ~ variable 1 + variable2, data = df, family = binomial (link = 'logit')), and I can use nls to go nls (y ~ a * x^2 + b * x + c, data = df).

I'd like to have some function that would take the formula cbind (success, failure) ~ int - slo * x + gap / (1 + x / sca) (where x, success, and failure are the only data and everything else are parametres) with a binomial (link = 'logit') family, i.e. combine both things. I've been scouring Google and haven't been able to find anything like that.


Solution

  • Try gnlm::bnlr(). The default link is logit and you can specify a nonlinear function of data and parameters. I include two answers depending on whether or not gap and sca are data or parameters.

    library(gnlm)
    

    In the case of gap and sca are data:

    ## if gap and sca are data:
    set.seed(1)
    dat <- data.frame(  x = rnorm(10),
                      gap = rnorm(10),
                      sca = rnorm(10),
                        y = rbinom(10,1,0.4))
    y_cbind = cbind(dat$y, 1-dat$y)
    attach(dat)
    bnlr(y=y_cbind, mu = ~ int - slo * x + gap / (1 + x / sca), pmu = c(0,0))
    

    Output:

    Call:
    bnlr(y = y_cbind, mu = ~int - slo * x + gap/(1 + x/sca), pmu = c(0, 
        0))
    
    binomial distribution
    
    Response: y_cbind 
    
    Log likelihood function:
    {
        m <- plogis(mu1(p))
        -sum(wt * (y[, 1] * log(m) + y[, 2] * log(1 - m)))
    }
    
    Location function:
    ~int - slo * x + gap/(1 + x/sca)
    
    -Log likelihood    2.45656 
    Degrees of freedom 8 
    AIC                4.45656 
    Iterations         8 
    
    Location parameters:
         estimate      se
    int    -1.077  0.8827
    slo    -1.424  1.7763
    
    Correlations:
           1      2
    1 1.0000 0.1358
    2 0.1358 1.0000
    

    In the case gap and sca are parameters:

    ## if gap and sca are parameters:
    detach(dat)
    set.seed(2)
    dat <- data.frame(  x = rbinom(1000,1,0.3),
                        y = rbinom(1000,1,0.4))
    y_cbind = cbind(dat$y, 1-dat$y)
    attach(dat)
    bnlr(y=y_cbind, mu = ~ int - slo * x + gap / (1 + x / sca), pmu = c(0,0,0,1))
    

    Output:

    Call:
    bnlr(y = y_cbind, mu = ~int - slo * x + gap/(1 + x/sca), pmu = c(0, 
        0, 0, 1))
    
    binomial distribution
    
    Response: y_cbind 
    
    Log likelihood function:
    {
        m <- plogis(mu1(p))
        -sum(wt * (y[, 1] * log(m) + y[, 2] * log(1 - m)))
    }
    
    Location function:
    ~int - slo * x + gap/(1 + x/sca)
    
    -Log likelihood    672.9106 
    Degrees of freedom 996 
    AIC                676.9106 
    Iterations         7 
    
    Location parameters:
         estimate      se
    int  -0.22189  2.1007
    slo   0.03828  3.6051
    gap  -0.20273  2.0992
    sca   0.99885  0.3956
    
    Correlations:
              1       2        3       4
    1    1.0000   1.859  -0.9993 -281.45
    2    1.8587   1.000  -1.8592  -82.06
    3   -0.9993  -1.859   1.0000  281.64
    4 -281.4530 -82.061 281.6443    1.00