Search code examples
rplotnlscoefficientsmosaic

Constraining coef output using fit model or nls function


I have data on the photosynthetic activity of microalgae under increasing light. PAR: is the different light exposures, (x, independent variable) ETR: is the photosynthetic activity (y, dependent variable).

PAR ETR
1   0.409090909
46  18.81818182
126 51.54545455
234 93.6
404 173.1428571
656 246
816 272

i was able to plot the point for a sample and get the curve. But i want to fit these points to a specific non-linear model using this formula: ETR= PAR/((aPAR^2)+(bPAR)+c) I used the Nls function as part of the mosaic package that uses the nls function as shown in the code:

TABLE=read.table("sample 2.txt", header = TRUE)
TABLE
plotPoints(ETR~PAR, data= TABLE)
f<-fitModel(ETR ~ PAR/((a*PAR^2)+(b*PAR)+c),data = TABLE, start = list(a=0, b=0.004, c=1))
coef(f)
plotFun(f, add=TRUE)

My results:

> TABLE=read.table("sample 2.txt", header = TRUE)
> TABLE
  PAR         ETR
1   1   0.4090909
2  46  18.8181818
3 126  51.5454546
4 234  93.6000000
5 404 173.1428571
6 656 246.0000000
7 816 272.0000000
> plotPoints(ETR~PAR, data= TABLE)
> f<-fitModel(ETR ~ PAR/((a*PAR^2)+(b*PAR)+c),data = TABLE, start = list(a=0, b=0.0035, c=1))
> coef(f)
            a             b             c 
 2.921397e-06 -2.027561e-03  2.718527e+00 
> plotFun(f, add=TRUE)

The problem is i do not want to have negative values for coef a,b, and c. This is because i am going to need these coefficients to get biological factors ETRmax= 1/b+2ac, Ek= c/b+2ac, alfa= 1/c which cannot be negative. I also need to use the Gauss-newton least square method and not the other algorithms where i can specify lower and upper bounds. I have tried to use the port algorithm to specify lower bounds of 0, but the problem is that b was 0, and i do not want b to be 0, i want all my variables to be greater than 0. I have tried many things but I can't solve this problem. I hope that you can help me with this, thank you.


Solution

  • Specify lower bounds with the lower = c(0, 0, 0), alg = "port" :

    fitModel(ETR ~ PAR/((a*PAR^2)+(b*PAR)+c), data = TABLE, 
      start = list(a = 0, b = 0.0035, c = 1), lower = c(0, 0, 0), alg = "port")