如何在R中获得nls的情节?

mur*_*ray 3 plot r nls

在RI中使用nls进行非线性最小二乘拟合.那么我如何使用拟合提供的系数值绘制模型函数?

(是的,这是R相对新手的一个非常天真的问题.)

jor*_*ran 13

使用示例中的第一个示例,?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))
Run Code Online (Sandbox Code Playgroud)

参数的predict方法nls是自动返回拟合y值.或者,您添加一个步骤并执行

yFitted <- predict(fm1DNase1)
Run Code Online (Sandbox Code Playgroud)

并传入yFitted第二个参数lines.结果如下:

在此输入图像描述

或者,如果你想要一个"平滑"的曲线,你所做的就是简单地重复这一点,但在更多点评估函数:

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)
Run Code Online (Sandbox Code Playgroud)