我有一个似乎有一种对数关系的几个数据点(x和y)。
> mydata
x y
1 0 123
2 2 116
3 4 113
4 15 100
5 48 87
6 75 84
7 122 77
> qplot(x, y, data=mydata, geom="line")
现在我想找到一个适合的图形,让我推断其他数据点(即最基本的功能3
或82
)。 我读到lm
和nls
,但我不成气候真的。
起初,我创建了我以为它类似的情节最为一个函数:
f <- function(x, a, b) {
a * exp(b *-x)
}
x <- seq(0:100)
y <- f(seq(0:100), 1,1)
qplot(x,y, geom="line")
后来,我尝试使用,以产生一个拟合模型nls
:
> fit <- nls(y ~ f(x, a, b), data=mydata, start=list(a=1, b=1))
Error in numericDeriv(form[[3]], names(ind), env) :
Missing value or an Infinity produced when evaluating the model
有人能指出我在什么从这里做正确的方向?
跟进
阅读您的意见和周围有点谷歌搜索后,我进一步调整启动参数a
, b
和c
然后突然模型收敛。
fit <- nls(y~f(x,a,b,c), data=data.frame(mydata), start=list(a=1, b=30, c=-0.3))
x <- seq(0,120)
fitted.data <- data.frame(x=x, y=predict(fit, list(x=x))
ggplot(mydata, aes(x, y)) + geom_point(color="red", alpha=.5) + geom_line(alpha=.5) + geom_line(data=fitted.data)