如何找到非线性回归模型的起始值?

时间:2019-04-26 11:51:56

标签: r nls non-linear-regression

我正在尝试将非线性树木直径高度模型(Max&Burkhart,1976)拟合到我的数据集中(由D表示,乳房高度直径(cm); H表示树木总高度(m);嗨)距地面的截面高度,高处的直径di等)

我在拟合模型时遇到麻烦。我认为这是因为方程式的起始参数值。我收到“ NaN产生”错误。我试图调整启动参数。错误数量减少到1,但不为零。因此,我需要找到一种方法来估计非线性回归模型的起始参数。我搜索了“自我启动”模型,但由于方程的复杂性和缺乏知识,因此无法应用于我的方程。我将在此处添加所有数据集,以便你们向我展示一种方法。

顺便说一句,我不确定是否可以将文件附加到我的问题中,所以我将为想要查看或下载的任何人提供指向我的数据集的链接。我将数据上传到了Google云端硬盘,链接为 https://drive.google.com/file/d/1q7W1bUcx4sK2G2QPte7ZtCudSLfBxpet/view?usp=sharing

# Function to compute Max & Burkhart (1976) equation
ComputeDi.MaxBurkhart <- function(hi, d, h, b1, b2, b3, b4, a1, a2){
    x <- hi / h
    x1 <- x - 1 
    x2 <- x ^ 2 - 1
    di <- d * sqrt(b1 * x1 + b2 * x2 + b3 * (a1 - x) ^ 2 * ((a1 - x) >= 0.0) + b4 * (a2 - x) ^ 2 * ((a2 - x) >= 0.0))
    return(di)
}

# Set the working directory
setwd("../Data")

# Load data and rename some variables
sylvestris <- read.csv("mydata.csv")

# Global fitting
nlmod.fp.di <- nls(di ~ ComputeDi.MaxBurkhart(hi, d, h, b1, b2, b3, b4, a1, a2), data = sylvestris, start = c(b1 = -2.53, b2 = 1.2, b3 = -1.5, b4 = 22, a1 = 0.72, a2 = 0.15

), control = nls.control(tol = 1e-07))
summary(nlmod.fp.di, correlation = T)

一切正常,直到这里。我在这之后得到了Nan错误!

# Set seed and select names of trees
trees <- unique(sylvestris$tree) 
set.seed(15)
result.list <- list()
i <- 1
while(length(trees) > 0){
    tree.smp <- sample(trees, 10, replace = F)
    sylvestris.smp <- sylvestris[sylvestris$tree %in% tree.smp, ]
    fitting.ols <- try(nls(di ~ ComputeDi.MaxBurkhart(hi, d, h, b1, b2, b3, b4, a1, a2), data = sylvestris.smp, start = c(b1 = -2.53, b2 = 1.2, b3 = -1.5, b4 = 22, a1 = 0.72, a2 = 0.15

), control = nls.control(tol = 1e-07)), silent = T)
    if(class(fitting.ols)[1] == "try-error"){
            fit.smp <- data.frame(trees = paste(tree.smp, collapse = "_"), 
t(rep(NA, 8)))
            names(fit.smp) <- c("trees", "b1", "b2", "b3", "b4", "a1", 
"a2", "NS", "RSE")
    } else {
            nlmod.ols <- fitting.ols
            fit.smp <- data.frame(trees = paste(tree.smp, collapse = "_"), t(coef(fitting.ols)), NS = sum(summary(fitting.ols)$parameters[, 4] > 0.05), RSE = summary(fitting.ols)$sigma)
    }
    result.list[[i]] <- fit.smp
    i <- i + 1
    trees <- trees[!trees %in% tree.smp]        
}     

我期望参数估计有效,而不会出现NaN错误。我确定问题在于起始值,因为此代码块可与另一个数据集完美配合。当我更改数据时,出现此错误。预先谢谢你。

1 个答案:

答案 0 :(得分:0)

您可以尝试使用软件包nls.multstart,该软件包可以简化初始值的估算。

您基本上可以指定起始参数的范围,然后根据AIC得分使用最佳参数进行回归。

相关问题