如何从 R 中的 nls 获取绘图?

问题描述 投票:0回答:5

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

(是的,这是 R 相关新手提出的一个非常天真的问题。)

r plot nls
5个回答
14
投票

使用

?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))

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

yFitted <- predict(fm1DNase1)

并将第二个参数中的

yFitted
传递给
lines
。结果如下所示:

enter image description here

或者,如果您想要一条“平滑”曲线,您要做的就是简单地重复此操作,但在更多点上评估函数:

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)

3
投票

coef(x) 返回回归结果 x 的系数。

model<-nls(y~a+b*x^k,my.data,list(a=0.,b=1.,k=1))
plot(y~x,my.data)
a<-coef(model)[1]
b<-coef(model)[2]
k<-coef(model)[3]
lines(x<-c(1:10),a+b*x^k,col='red')

例如。


1
投票

我知道你想要什么(我是科学家)。这不是它,但至少展示了如何使用“曲线”在任何范围内绘制拟合函数,并且曲线将是平滑的。使用与上面相同的数据集:

nonlinFit <- nls(density ~ a - b*exp(-c*conc), data = DNase1, start = list(a=1, b=1, c=1) )

适合Fnc <- function(x) predict(nonlinFit, list(conc=x))

曲线(fitFnc,从=.5,到=10)

或者,

曲线(fitFnc,从=8.2,到=8.4)

或者,

curve(fitFnc, from=.1, to=50) # 远远超出数据范围

或其他什么(无需先设置评估点序列)。

我是一名初级 R 程序员,所以我不知道如何在 Mathematica 中(优雅地)实现像 ReplaceAll ( /. ) 这样的东西,用拟合的参数来替换模型中出现的符号参数。尽管看起来很糟糕,但第一步是有效的:

我的模型<- "a - b*exp(-c*conc)"

nonlinFit <- nls(as.formula(paste("density ~", myModel)), data = DNase1, start = list(a=1, b=1, c=1) )

它给你留下了一个单独的“模型”(作为字符串),你可以使用拟合的参数......干净地(不挖出a,b,c)只需使用nonlinFit .. .不过不知道怎么办。


0
投票

提供一些背景信息:

有两种主要方法可以绘制预测值。 根据您想要控制的内容选择其中之一:

  • 指定 x 值的插值向量 (
    tt
    ) 并使用具有
    predict
    函数的模型预测 y 值
  • 从模型中拉出
    coefficients
    并在其上运行模型表达式的右侧
# Data:
x = c(0,20,40,60,80,100,120,140) # sample timepoints
C = c(147.8,78.3,44.7,29.5,15.2,7.8,3.2,3.9) # concentratios
data <- data.frame(C,x) # data we got 

# plot empirical data:
opar <- par(las = 1) # plot params to rotate ylabels 
plot(C~x, data = data, col = 2, pch=3) 

# modeling
mod <- formula(C ~ C0*exp(-k*x))
fit <- nls(mod, data, start=list(C0=100,k=0.1)) # fit model to data
# plot(profile(fit)) # profile iterations

## Plotting of the model:

# interpolation vector tt
t0 = min(data$x)
tmax = max(data$x)
niter = 301
tt <- seq(t0, tmax,length.out=niter)

#  using tt with predict to plot lines:
lines(tt, predict(fit, list(x = tt)),lty=4)

# using coefficents and curve (no tt vector nor niter needed) :
C0 <- coefficients(fit)["C0"] # extract coefficent initialC
k <-coefficients(fit)["k"] # extract coefficent k
curve(C0*exp(-k*x),t0,tmax,add = T,lty=4,col="red")

# or, use both tt and coefs with eval to get the lines:
# (this is ~more or less~ what happens under "the hood")
plot(tt,eval(C0*exp(-k*tt)),lty=4,col="blue",type = "l",add=T)

最后一行是最“简单”的R,不使用曲线或直线,并且需要

tt
向量和系数
C0
k
作为环境中的变量。


-1
投票

“曲线”功能将为您绘制函数。

© www.soinside.com 2019 - 2024. All rights reserved.