非线性最小二乘法:在 R 中使用 nls 函数时如何将模型参数解析为向量

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

我正在处理生物数据,比如生长对温度变化或营养浓度的反应等等。我的目标是建立一个自动化例程来加载数据,将不同的简单非线性模型拟合到同一数据集并将输出(即模型系数)写入对象,以便我可以事后判断哪个模型最适合。 为此已写入以下代码:

#make up some example data
data<-data.frame(x=c(0,1,2,3,4,5,6,7,8,9,10),
                 y=c(0,2,4,5.5, 6.2, 6.4, 6.5, 6.6, 6.6,6.65,6.65))

#plot data
ggplot() + 
  geom_point(data = data, aes(x = x, y = y))+
  scale_colour_discrete(type = getOption("ggplot2.discrete.colour"))

##########
# For this example, i want to fit the Michaelis-Menten formula: y=((Vmax*x)/(Km+x)

#First, i try to fit the data with the bare function:
MiMefit<-nls(y~(Vmax*x)/(Km+x),
             data=data,
             start = c(Vmax=5, Km=2))

#This seems to work, and I can predict & visualize modelled values
MiMefit
data$fit<-predict(MiMefit)

ggplot() + 
  geom_point(data = data, aes(x = x, y = y))+
  geom_line(data = data, aes(x = x, y = fit), color="red")+
  scale_colour_discrete(type = getOption("ggplot2.discrete.colour"))
##########

#Since i ´do not want to write every model by hand in every fitting procedure, i deposit the model #in an object  and define the Michaelis-Menten equation as "standalone" model

MiMe <- function(x, Vmax, Km) {
  
    return((Vmax*x)/(Km+x))
}

#test for functionality is positive
MiMe(x=4, Vmax = 100, Km = 9)

#Now i can do the fitting by calling the model name instead of writing the function:
#MiMefit2<-nls(y~MiMe(x = data$x, Vmax, Km),
             data=data,
             start = c(Vmax=5, Km=2))

MiMefit2

##################

#Since i want to automate this, i want to call the model by a *generic* name, that will later
#(in a for loop) "encode" other models with other equations.

#I make up an object that links to the MiMe formula, called "model"

model<-MiMe
#seems to have worked:
str(model)

#Now i can do the fitting with using the generic name:
MiMefit3<-nls(y~model(x = data$x, Vmax, Km),
              data=data,
              start = c(Vmax=5, Km=2))

MiMefit3
###################

#Still, i need to specify the parameters of the model (Vmax, Km) manually. Other models have other #parameter names, and diff. number of parameters. Thus i´d like to deposit the names of the #parameters in a  vector:

params<-c("Vmax","Km")
str(params)

My problem is now, that i can not manage to edit the command in such way that i can parse the 
# necessary parameter names to the function:

MiMefit4<-nls(y~I(model(x = data$x, params)),
              data=data,
              start = c(Vmax=5, Km=2))

您可以看到使用向量将参数解析为函数失败

我检查了我能在 nls 上找到的所有文章,但找不到我的问题的其他示例。好像也跟函数需要的语法有关,但是我在那里搜索也没有成功。

我已经尝试了好几天来改写命令,例如

MiMefit4<-nls(y~I(model(x = data$x, params[1], params[2])),
              data=data,
              start = c(Vmax=5, Km=2))

MiMefit5<-nls(y~I(model(as.list(noquote(paste("x = data$x","," ,noquote(params[1]),",",noquote(params[2])))))),
              data=data,
              start = c(Vmax=5, Km=2))

MiMefit5

没有成功。我试过 noquote 命令:noquote(params[1]);我尝试了多种 as.xxxx 函数,试图使其与 as.formula、as.function、as.list 等一起使用。 我认为应该以某种方式期望一份清单,但这没有用。

此外,理想情况下,带有参数的向量被调用一次,将所有包含的参数插入模型参数列表中,因为在未来,如前所述,我想指定具有不同数量参数的模型。

一如既往,感谢您花时间和精力帮助我解决问题!! 塞巴斯蒂安

r model curve nls
© www.soinside.com 2019 - 2024. All rights reserved.