简体   繁体   English

R错误:参数无起始值

[英]R Error: parameters without starting value

Non linear real world data, n=2,600 非线性现实世界数据,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

I manually graphed a formula for a starting point, 我手动绘制了一个公式作为起点,

formula:  y = b/x^2+a
manual:   y = 800000/x^2-39.5
sum of residuals = 185
correlation forecast to actual =0.79 

Using the nls formula in R, I get an error message: 在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

Not sure what I am missing here. 不知道我在这里想念的是什么。

I can reproduce your error. 我可以重现您的错误。 nls function is missing the argument data in it. 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

Now data df is created and passed into nls function. 现在,将创建数据df并将其传递给nls函数。 Make sure, the insulated expression in I() is the intended one. 确保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

Read the formula man page for insulated expression. 阅读公式手册页以了解绝缘表达式。

?formula

The man page of formula says that 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 例如(a + b + c)^ 2与(a + b + c)*(a + b + c)相同,后者又扩展为包含a,b和c的主要效应以及它们的第二个效应的公式有序互动

Also it suggests using I() to prevent ambiguity between formula operators and arithmetic operators. 它还建议使用I()来防止公式运算符和算术运算符之间的歧义。

Here is the another quote from formula man page 这是公式手册页中的另一句话

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. 为避免这种混淆,可以使用函数I()将模型公式的那些部分括在其中,以算术意义使用运算符。 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. 例如,在公式y〜a + I(b + c)中,术语b + c将解释为b和c的总和。

Also this man page is worth reading 另外这个手册页也值得一读

 ?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. 术语。公式将其解释为符号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM