user1946911
user1946911

Reputation: 97

How to get the results from nls

My data are as follows:

df<-read.table (text=" cup size
12  30.76923077
13  38.46153846
14  53.84615385
15  76.92307692
16  92.30769231
17  100", header=TRUE)

I have used the following codes to get the results

y=as.vector(df$cup)
x=as.vector(df$size)
fit <- nls(1/y ~ 1 + exp(-b*(x-c)), start = list(b = 0.01, c = 12), alg = "plinear")

I want to extract values b, c, .lin separately like this:

b= 0.01552
c= 5.98258
lin= 0.04887

I have used fit[[2]], but it does not work for me.

Upvotes: 2

Views: 1003

Answers (2)

Manu
Manu

Reputation: 1120

This should work:

df<-read.table (text=" cup size
12  30.76923077
13  38.46153846
14  53.84615385
15  76.92307692
16  92.30769231
17  100", header=TRUE)

y=as.vector(df$cup)
x=as.vector(df$size)
fit <- nls(1/y ~ 1 + exp(-b*(x-c)), start = list(b = 0.01, c = 12), alg = "plinear")
myvector <- coef(fit)
b <- myvector[1]
c <- myvector[2]
lin <- myvector[3]

Upvotes: 2

Ian Campbell
Ian Campbell

Reputation: 24770

As suggested by @user20650, coef is the function used to extract the values from the model.

If you refer to the source code you'll see it's hidden in the m element in the function getAllPars()

fit$m$getAllPars()
         b          c       .lin 
0.01552340 5.98262729 0.04887368 

Upvotes: 2

Related Questions