Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with nonlinear least square fit nls non-numeric argument with binary operator

Tags:

r

nls

I just started working with R and would like to get a Nonlinear least square fit nls(...) to the formula y=A(1-exp(-bL))+R. I define my function g by

> g<-function(x,y,A,b,R) {
    y~A(1-exp(-bx))+R
    }

and want to perform nls by

>nls((y~g(x,y,A,b,R)),data=Data, start=list(A=-2,b=0,R=-5))

And I end with the following error message.

>Error in lhs - rhs : non-numeric argument to binary operator

I guess it's just a stupid basic mistake by another beginner, but I'd be extremely glad if anyone could help me out.

Next question would be, whether I can implement the fitted curve into my graph

>plot(x,y,main="VI.20.29")

Thanks to everyone taking time to read and hopefully answer my question!

Detailed information: I have a table with the x values (Light.intensity) and y values (e.g. VI.20.29)

> photo.data<-read.csv("C:/X/Y/Z.csv", header=T)
    > names(photo.data)
     [1] "Light.intensity" "SR.8.6"          "SR.8.7"         
     [4] "SR.8.18"         "SR.8.20"         "VI.20.1"        
     [7] "VI.20.5"         "VI.20.20"        "VI.20.29"       
    [10] "DP.19.1"         "DP.19.15"        "DP.19.33"       
    [13] "DP.19.99"       
    > x<-photo.data$Light.intensity
    > x
    [1]    0   50  100  200  400  700 1000 1500 2000
    > y<-photo.data$VI.20.29
    > y
    [1] -2.76 -2.26 -1.72 -1.09  0.18  0.66  1.47  1.48  1.63
    > plot(x,y,main="VI.20.29")
    > Data<-data.frame(x,y)
    > Data
         x     y
    1    0 -2.76
    2   50 -2.26
    3  100 -1.72
    4  200 -1.09
    5  400  0.18
    6  700  0.66
    7 1000  1.47
    8 1500  1.48
    9 2000  1.63
    > g<-function(x,y,A,b,R) {
    +   y~A(1-exp(-bx))+R
    +   }
    > nls((y~g(x,y,A,b,R)),data=Data, start=list(A=-2,b=0,R=-5))
    Error in lhs - rhs : non-numeric argument to binary operator
like image 202
user2007991 Avatar asked Dec 12 '25 01:12

user2007991


1 Answers

The problem is that you're calling a function within a function. You're saying y~g(...), when the function g(...) itself calls y~(other variables). It's kind of 'double counting' in a way.

Just do:

nls(y~A*(1-exp(-b*x))+R, data=Data, start=list(A=-2,b=0,R=-5))
like image 156
Señor O Avatar answered Dec 14 '25 14:12

Señor O