nls函数和列表中的R函数

时间:2013-10-09 17:13:06

标签: r list function environment nls

我正在使用R编写的包来将实验数据拟合到特定模型 我想写自己的模型并使用相同的包(作者声称它是可能的)。

所以我正在挖掘源文件以找到他们定义模型函数的位置。我被卡住了 他们使用nls的方式。我了解nls如何处理以下案例

x <- -(1:100)/10
y <- 100 + 10 * exp(x / 2) + rnorm(x)/2
nlmod <- nls(y ~  Const + A * exp(B * x),start=list(Const=5,A=5,B=7))

创建一个数据集并在其旁边编写模型 - 简单。

但是他们使用nls,如下所示:

>  nls(~rescomp(theta = t, d = d, currModel = currModel), 
>     data = list(d = vector(), currModel = currModel))

当我跑

>  ~rescomp(theta = t, d = d, currModel = currModel)

查看上例中的公式(Const + A * exp(B * x))。我明白了

~rescomp(theta = t, d = d, currModel = currModel) 

environment: 0x000000000a7f7530

我想了解如何查看生成的公式以及如何nls 数据设置为list \ environment时工作。有什么建议吗?

以下是rescomp

的内容
  function (theta = vector(), d = vector(), currModel = currModel, 
    currTheta = vector()) 
{
    if (length(currTheta) == 0) 
        currTheta <- getThetaCl(theta, currModel)
    groups <- currModel@groups
    m <- currModel@modellist
    resid <- clpindepX <- list()
    nexp <- length(m)
    for (i in 1:nexp) {
        clpindepX[[i]] <- if (!m[[i]]@clpdep || m[[i]]@getX) 
            getClpindepX(model = m[[i]], theta = currTheta[[i]], 
                multimodel = currModel, returnX = FALSE, rawtheta = theta, 
                dind = 0)
        else matrix()
    }
    for (i in 1:length(groups)) {
        resid[[i]] <- residPart(model = m[[1]], group = groups[[i]], 
            multimodel = currModel, thetalist = currTheta, clpindepX = clpindepX, 
            finished = currModel@finished, returnX = FALSE, rawtheta = theta)
        if (currModel@finished) {
            currModel <- fillResult(group = groups[[i]], multimodel = currModel, 
                thetalist = currTheta, clpindepX = clpindepX, 
                rlist = resid[[i]], rawtheta = theta)
        }
    }
    if (currModel@finished) {
        currModel@fit@nlsres$onls$nclp <- currModel@nclp
        if (currModel@optlist[[1]]@sumnls) {
            if (class(currModel@fit@nlsres$onls) == "nls") 
                class(currModel@fit@nlsres$onls) <- "timp.nls"
            else if (class(currModel@fit@nlsres$onls) == "nls.lm") 
                class(currModel@fit@nlsres$onls) <- "timp.nls.lm"
            else class(currModel@fit@nlsres$onls) <- "timp.optim"
            currModel@fit@nlsres$sumonls <- summary(currModel@fit@nlsres$onls, 
                currModel = currModel, currTheta = currTheta)
        }
        if (currModel@stderrclp) {
            for (i in 1:length(groups)) {
                currModel <- getStdErrClp(group = groups[[i]], 
                  multimodel = currModel, thetalist = currTheta, 
                  clpindepX = clpindepX, rlist = resid[[i]], 
                  rawtheta = theta) 
       }
        if (currModel@stderrclp) {
            for (i in 1:length(groups)) {
                currModel <- getStdErrClp(group = groups[[i]], 
                  multimodel = currModel, thetalist = currTheta, 
                  clpindepX = clpindepX, rlist = resid[[i]], 
                  rawtheta = theta)
            }
        }
    }
    if (currModel@finished && currModel@trilinear) {
        trires <- triResolve(currModel, currTheta)
        currModel <- trires$currModel
        currTheta <- trires$currTheta
    }
    if (currModel@finished && m[[1]]@mod_type == "kin") {
        if (m[[1]]@fullk) {
            for (i in 1:nexp) {
                nocolsums <- length(m[[1]]@lightregimespec) > 
                  0
                eig <- fullKF(currTheta[[i]]@kinpar, currTheta[[i]]@kinscal, 
                  m[[1]]@kmat, currTheta[[i]]@jvec, m[[1]]@fixedkmat, 
                  m[[1]]@kinscalspecial, m[[1]]@kinscalspecialspec, 
                  nocolsums)
                currTheta[[i]]@eigenvaluesK <- eig$values
            }
        }
    }
    if (currModel@finished) {
        return(list(currModel = currModel, currTheta = currTheta))
    }
    if (currModel@algorithm == "optim") 
        retval <- sum(unlist(resid))
    else retval <- unlist(resid)
    retval
}

1 个答案:

答案 0 :(得分:1)

没有公式。 nls重复调用一个函数,该函数引入'currModel'和参数(可能是theta和d),这些函数将根据rescomp函数返回的标量进行最小化。您输入“rescomp”的结果“太长而无法写在这里”的投诉只是表明a)您不理解您应该编辑您的问题而不是在评论中写出该输出,并且b)你对正在发生的事情的期望过于狭窄。

说明将第一个nls问题写成函数形式:

myfunc <- function(Const,A,B,y=y,x=x) { abs(y - ( Const + A * exp(B * x)))}

因此,您将最小化y与预测的绝对偏差:

nlmod <- nls( ~myfunc(Const,A,B,y=y,x=x) ,start=list(Const=5,A=5,B=7))
nlmod  # same results