如何修复nls模型上的“未使用的参数”错误

时间:2019-12-27 15:29:10

标签: r function arguments curve-fitting

A <- c(100, 200, 300, 400, 500, 600)
B <- c(60, 50, 40, 30, 20, 10)
data <- data.frame(A,B)

sample_plot <- function(A,B)
{
(w * ((x * A)^2) / (y/z)
}

control <- nls.control(maxiter = 1000)
sample_model <- nls(B, w, x, y, z), control = control, start = list(w = 62.2060, x = 0.0438, y = 0.9692, z = 0.8693))

plot(A, predict(sample_model), type = "1", col = "blue")
points(A, B, col = "red")
summary(sample_model)

每次我运行模型的代码时,即使我提供了x,y和z的值,它也会提供一个关于未使用参数的错误,指出“ Error in sample_model(B, w, x, y, z) : unused arguments (x, y, z)”。我也尝试将sample_plot <- function(A,B)更改为sample_plot <- function(A,B,...),但是它不起作用。有什么帮助吗?谢谢。

1 个答案:

答案 0 :(得分:1)

正如@ user20650所指出的那样,该模型过于参数化,无法给出与模型B〜w * A ^ 2相比的任何不同预测。这是因为所有其他参数都可以吸收到w中。此外,正如@ Dave2e指出的那样,这种模型无法以任何合理的方式拟合数据。

对数据进行绘制,B在A中似乎是线性的,因此应该使用lm,并且由于残差平方和实际上为零,因此可以给出完美的拟合。

fm <- lm(B ~ A, data)
fm

## Call:
## lm(formula = B ~ A, data = data)
##
## Coefficients:
## (Intercept)            A  
##        70.0         -0.1  

deviance(fm) # residual sum of squares
## [1] 1.390367e-28

plot(B ~ A, data)
abline(fm)

screenshot