Search code examples
rnonlinear-functionsnlsnon-linear-regression

Fitting non-linear Langmuir Isotherm in R


I want to fit Isotherm models for the following data in R. The simplest isotherm model is Langmuir model given here model is given in the bottom of the page. My MWE is given below which throw the error. I wonder if there is any R package for Isotherm models.

X <- c(10, 30, 50, 70, 100, 125)
Y <- c(155, 250, 270, 330, 320, 323)
Data <- data.frame(X, Y)
LangIMfm2 <- nls(formula = Y ~ Q*b*X/(1+b*X),  data = Data, start = list(Q = 1, b = 0.5), algorith = "port")

Error in nls(formula = Y ~ Q * b * X/(1 + b * X), data = Data, start = list(Q = 1,  : 
  Convergence failure: singular convergence (7)

Edited

Some nonlinear models can be transform to linear models. My understanding is that there might be one-to-one relationship between the estimates of nonlinear model and its linear model form but their corresponding standard errors are not related to each other. Is this assertion true? Are there any pitfalls in fitting Nonlinear Models by transforming to linearity?


Solution

  • I am not aware of such packages and personally I don't think that you need one as the problem can be solved using a base R.

    nls is sensitive to the starting parameters, so you should begin with a good starting guess. You can easily evaluate Q because it corresponds to the asymptotic limit of the isotherm at x-->Inf, so it is reasonable to begin with Q=323 (which is the last value of Y in your sample data set).

    Next, you could do plot(Data) and add a line with an isotherm that corresponds to your starting parameters Q and b and tweak b to come up with a reasonable guess.

    The plot below shows your data set (points) and a probe isotherm with Q = 323 and b = 0.5, generated by with(Data,lines(X,323*0.5*X/(1+0.5*X),col='red')) (red line). It seemed a reasonable starting guess to me, and I gave it a try with nls:

    LangIMfm2 <- nls(formula = Y ~ Q*b*X/(1+b*X),  data = Data, start = list(Q = 300, b = 1), algorith = "port")
    # Nonlinear regression model
    #   model: Y ~ Q * b * X/(1 + b * X)
    #    data: Data
    #        Q        b 
    # 366.2778   0.0721 
    #  residual sum-of-squares: 920.6
    # 
    # Algorithm "port", convergence message: relative convergence (4)
    

    and plotted predicted line to make sure that nls found the right solution:

    lines(Data$X,predict(LangIMfm2),col='green')
    

    enter image description here

    Having said that, I would suggest to use a more effective strategy, based on the linearization of the model by rewriting the isotherm equation in reciprocal coordinates:

    enter image description here

    z <- 1/Data
    plot(Y~X,z)
    abline(lm(Y~X,z))
    M <- lm(Y~X,z)
    
    Q <- 1/coef(M)[1]
    # 363.2488 
    
    b <- coef(M)[1]/coef(M)[2]
    # 0.0741759 
    

    As you could see, both approaches produce essentially the same result, but the linear model is more robust and doesn't require starting parameters (and, as far as I remember, it is the standard way of the isotherm analysis in the experimental physical chemistry).