非线性真实世界数据,n=2,600
SAMPLE
X values 71.33 74.98 80 85.35 90.03
Y values 119.17 107.73 99.72 75 54.59
我手动绘制了一个起点的公式,
formula: y = b/x^2+a
manual: y = 800000/x^2-39.5
sum of residuals = 185
correlation forecast to actual =0.79
在 R 中使用 nls 公式,我收到一条错误消息:
a_start = -39.5
b_start = 800000
m<-nls(y~b/(x^2)+a, start=list(a=a_start,b=b_start))
Error in nls(y~ b/(x^2) + a, start = list(a = a_start, b = b_start)) :
parameters without starting value in 'data': y, x
不确定我在这里缺少什么。
最佳答案
我可以重现你的错误。 nls
函数缺少参数 data
在里面。
m<-nls(y ~ b/(x^2)+a, start=list(a=a_start, b=b_start))
# Error in nls(y ~ b/(x^2) + a, start = list(a = a_start, b = b_start)) :
# parameters without starting value in 'data': y, x
现在数据
df
被创建并传递给 nls 函数。确保 I()
中的绝缘表达式是预期的。df <- data.frame(x = c(71.33, 74.98 , 80 , 85.35 , 90.03),
y = c(119.17, 107.73 , 99.72 , 75, 54.59))
a_start <- -39.5
b_start <- 800000
m <- nls(y ~ I(b/(x^2+a)), data = df, start=list(a=a_start, b=b_start))
summary(m)
# Formula: y ~ I(b/(x^2 + a))
#
# Parameters:
# Estimate Std. Error t value Pr(>|t|)
# a -1743.2 872.5 -1.998 0.1396
# b 412486.2 89981.4 4.584 0.0195 *
# ---
# Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#
# Residual standard error: 9.103 on 3 degrees of freedom
#
# Number of iterations to convergence: 6
# Achieved convergence tolerance: 4.371e-06
阅读绝缘表达式的公式手册页。
?formula
formula
的手册页说The ^ operator indicates crossing to the specified degree. For example (a+b+c)^2 is identical to (a+b+c)*(a+b+c) which in turn expands to a formula containing the main effects for a, b and c together with their second-order interactions
它还建议使用
I()
防止公式运算符和算术运算符之间的歧义。这是公式手册页的另一个引用
avoid this confusion, the function I() can be used to bracket those portions of a model formula where the operators are used in their arithmetic sense. For example, in the formula y ~ a + I(b+c), the term b+c is to be interpreted as the sum of b and c.
这个手册页也值得一读
?AsIs
In function formula. There it is used to inhibit the interpretation of operators such as "+", "-", "*" and "^" as formula operators, so they are used as arithmetical operators. This is interpreted as a symbol by terms.formula.
关于R 错误 : parameters without starting value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39113612/