我有两个表格,数据非常相似,我想要一个模型:
###first data
x1 <- c(0.271237802,0.253595465,0.299072793,0.355537802,
0.335295465,0.365922793,0.476437802,0.464095465,0.482172793)
y1 <- c(0.039937,0.044174,0.062574,0.124286,
0.108702,0.131217,0.213418,0.216699,0.253712)
####second data
x2 <- c(0.285180641,0.289818303,0.27255962,0.373530641,
0.356768303,0.34930962,0.463880641,0.471668303,0.46330962)
y2 <- c(0.0499,0.063764,0.05343,0.147753,
0.14148,0.135757,0.220635,0.245013,0.236258)
####nls.model
fo1 = nls(y1~A*(x1-v)^k, start=list(A=1, v=0.15, k=1))
coef(fo1)
summary(fo1)
fo2 = nls(y2~A*(x2-v)^k, start=list(A=1, v=0.15, k=1))
coef(fo2)
summary(fo2)
####plotting data
s <- seq(from = 0, to = 1, length = 50)
plot(y1~x1, ylab = "D/D0", xlab = "LP(%)", pch = 16, xlim=c(0,0.6), ylim=c(0,0.4), col="blue")
lines(s, predict(fo1, list(x1 = s)), col = "blue")
par(new=T)
plot(y2~x2, ylab = "D/D0", xlab = "LP(%)", pch = 16, xlim=c(0,0.6), ylim=c(0,0.4), axes=F,col="red")
在第一种情况下,该模型运作良好,但在第二种情况下,它很脆弱并给出了消息:
fo2 = nls(y2~A*((x2-v)^(k)), start=list(A=1, v=0.15, k=1))
Error in numericDeriv(form[[3L]], names(ind), env) :
Missing value or an Infinity produced when evaluating the model
虽然数据显示几乎是线性关系,但我想使用nls和建议的函数,因为线性并不总是正确的。
我知道,它可能与起始值有关,但是我无法解决这个问题。有人有线索吗?
答案 0 :(得分:3)
x2-v
必须是非负面的,所以只要无约束的最小值满足v
小于min(x2)
,请尝试避免错误。
nls(y2 ~ A * pmax(x2-v, 0)^k, start = list(A = 1, v = 0.15, k = 1))
如果v
在最佳状态下不小于min(x2)
,那么问题是修订后的模型是否仍然可以接受。
另一种可能性是将v
限制为小于min(x2)
。例如:
nls(y2 ~ A * pmax(x2-v, 0)^k + 1000 * (v > min(x2)), start = list(A = 1, v = 0.15, k = 1))
或使用约束优化例程。