r - 如何找到非线性回归模型的起始值?
问题描述
我正在尝试将非线性树直径高度模型(Max & Burkhart,1976)拟合到我的数据集(包括 D,胸高直径(cm);H,总树高(m);hi 截面高度来自地面水平,高水平的di直径等)在R中。
我在拟合模型时遇到了麻烦。我认为这是因为方程的起始参数值。我收到“NaNs 产生”错误。我试图调整起始参数。错误数量减少到 1 但不是零。所以我需要找到一种方法来估计非线性回归模型的起始参数。我搜索了自启动模型,但由于方程的复杂性和我缺乏知识,无法应用于我的方程。我将在这里添加我所有的数据集,以便你们可以告诉我一个方法。
顺便说一句,我不确定我是否可以将文件附加到我的问题中,因此我将为想要查看或下载的任何人提供我的数据集的链接。我将我的数据上传到谷歌驱动器,链接是 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)
到这里为止一切都好。在这里之后我遇到了南错误!
# 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 错误。我确定问题出在起始值上,因为此代码块与另一个数据集完美配合。当我更改数据时,我得到了这个错误。先感谢您。
解决方案
您可以尝试使用包nls.multstart
,它是为了简化对起始值的估计。
您基本上可以指定起始参数的范围,并且将根据AIC分数使用最佳参数进行回归。
推荐阅读
- python - python plotnine:颜色啤酒还不够
- javascript - 如何从隐藏的数据预加载元素中查找值?
- java - SOLR 完全导入失败
- python - scipy stats beta 在适合数据集时返回负值 alpha 和 beta
- python - 我试图在 python 中使用时间作为命令的触发器
- vb.net - 在 VB 中更改文本选项
- php - PHP var_dump() 如果不清理,为什么字符串计数更大?
- mysql - MYSQL 现在选择日期 - 1 天,时间和日期之间
- angular - 如何完成 Material Stepper Step,并继续下一步?
- cakephp - 如何将数据插入两个不关联的表中 cakephp