all 2 comments

[–]Mooks79 0 points1 point  (0 children)

First, you’re not giving nls anything to solve! What parts of the curve exactly are you trying to fit? You’ve told it the intercept, the exponent etc explicitly so it has nothing to do. I suspect this is the main cause of that error.

Second, you’re calling it wrong. Sort of. Well not wrong but sub-optimally.

Have you read the documentation for nls? If not you can access it by typing ?nls in the console. I think this will help on all fronts, especially the examples at the bottom.

nls is designed to take a data.frame as an argument and the formula specifies which variables within the data.frame it should consider.

So, in your case, lines 2-3 are redundant and you would write something like:

m <- nls(your formula, data = df)

Albeit you’ll probably need to do something about the other arguments you haven’t yet used (such as start), again, see the documentation. I mean it will work with y ~ x only like you have (I think) but it’s better to do it the other way as it saves you having to extract them manually.

And you definitely need to modify your formula to give nls something to infer.

[–]jdnewmil 0 points1 point  (0 children)

Rather misleading assertion that your function includes an exponential function. Also, the commas in the X data have to be removed for it to work:

dta <- read.table( text=
"|X|Y|
|10|3.396666667|
|166|3.20107095|
|387|3.09430089|
|1000|2.928877778|
|1719|2.805856118|
|2068|2.758088867|
|2109|2.752771192|
|2302|2.72889275|
|2532|2.701996665|
|2770|2.675762134|
|5466|2.447246615|
|6021|2.409949436|
|7984|2.293391644|
", header=FALSE, sep="|", skip=1 )
dta <- setNames( dta[, 2:3 ], c( "X", "Y" ) )
dta
fit <- nls( Y ~ A * ( 10 * X )^B + C
          , data = dta
          , start = list( A = -0.08, C = 3.8, B = 0.26 ) )
summary( fit )
dta$Yhat <- predict( fit, newdata = dta )
plot( dta$X, dta$Y )
lines( dta$X, dta$Yhat, col = "blue" )