使用第一个例子?nls
按照我逐行指出的示例,可以实现以下目标:
#This is just our data frame
DNase1 <- subset(DNase, Run == 1)
DNase1$lconc <- log(DNase1$conc)
#Fit the model
fm1DNase1 <- nls(density ~ SSlogis(lconc, Asym, xmid, scal), DNase1)
#Plot the original points
# first argument is the x values, second is the y values
plot(DNase1$lconc,DNase1$density)
#This adds to the already created plot a line
# once again, first argument is x values, second is y values
lines(DNase1$lconc,predict(fm1DNase1))
The predict
的方法nls
参数自动返回拟合的y
价值观。或者,您添加一个步骤并执行
yFitted <- predict(fm1DNase1)
并通过yFitted
在第二个参数中lines
反而。结果如下:
![enter image description here](https://i.stack.imgur.com/y5kFt.png)
或者,如果您想要一条“平滑”曲线,您要做的就是简单地重复此操作,但在更多点上评估函数:
r <- range(DNase1$lconc)
xNew <- seq(r[1],r[2],length.out = 200)
yNew <- predict(fm1DNase1,list(lconc = xNew))
plot(DNase1$lconc,DNase1$density)
lines(xNew,yNew)