二元运算符R NLS包的非数字参数

时间:2017-08-08 18:05:48

标签: r

我在R中使用nls包来执行非线性拟合。我已经指定了我的自变量如下:

t <- seq(1,7)

和我的因变量为P < - c(0.0246,0.2735,0.5697,0.6675,0.8655,0.9614,1)

然后我尝试过:

m <- nls(P ~ 1 / (c + q*exp(-b*t))^(1/v))

但每次我得到:

  

&#34; c + q * exp(-b * t)中的错误:二进制的非数字参数   操作员&#34;

我的每个变量都是数字。有任何想法吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

您的脚本中存在多个问题。主要问题是你不应该使用R使用的名称:t是矩阵转置,c是创建向量的常用方法,q是退出指令。 nls()不会尝试适合它们,因为它们已经定义。我建议使用更有意义且危险性更小的变量,例如Coef1Coef2,...

第二个问题是你试图将带有4个变量的模型拟合到具有7个数据的数据集......这可能会产生奇点和其他问题。

为了论证,我已将模型缩减为三个变量,并更改了一些名称:

Time <- seq(1,7)
Prob <- c(0.0246, 0.2735, 0.5697, 0.6715, 0.8655, 0.9614, 1)
plot(Time, Prob)

enter image description here

现在我们执行nls()fit:

Fit <- nls(Prob ~ 1 / (Coef1 + Coef2 * exp(-Coef3 * Time))) 
X   <- data.frame(Time = seq(0, 7, length.out = 100))
Y   <- predict(object = Fit, newdata = X)
lines(X$Time, Y)

enter image description here

结果摘要:

summary(Fit)           
# Formula: Prob ~ 1/(Coef1 + Coef2 * exp(-Coef3 * Time))
# 
# Parameters:
# Estimate Std. Error t value Pr(>|t|)    
# Coef1  1.00778    0.06113  16.487 7.92e-05 ***
# Coef2 23.43349   14.42378   1.625   0.1796    
# Coef3  1.04899    0.21892   4.792   0.0087 ** 
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# 
# Residual standard error: 0.06644 on 4 degrees of freedom
# 
# Number of iterations to convergence: 12 
# Achieved convergence tolerance: 3.04e-06

我知道这不是你想要的,但我希望它有所帮助。