13

我有一些似乎具有对数关系的数据点(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")

阴谋

现在我想找到一个适合该图的基础函数,并允许我推断其他数据点(即382)。我读过lmnls但我真的没有得到任何地方。

起初,我创建了一个我认为最像情节的函数:

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")

情节2

之后,我尝试使用以下方法生成拟合模型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

有人可以指出我从这里做什么的正确方向吗?

跟进

在阅读了您的评论并进一步搜索后,我调整了 的起始参数ab然后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)

情节3

4

3 回答 3

10

也许对您的模型使用三次规格并通过估计lm会给您一个很好的选择。

# Importing your data
dataset <- read.table(text='
    x   y
1   0 123
2   2 116
3   4 113
4  15 100
5  48  87
6  75  84
7 122  77', header=T)

# I think one possible specification would be a cubic linear model
y.hat <- predict(lm(y~x+I(x^2)+I(x^3), data=dataset)) # estimating the model and obtaining the fitted values from the model

qplot(x, y, data=dataset, geom="line") # your plot black lines
last_plot() + geom_line(aes(x=x, y=y.hat), col=2) # the fitted values red lines

# It fits good.

在此处输入图像描述

于 2012-08-07T12:42:28.240 回答
3

尝试记录您的响应变量,然后lm用于拟合线性模型:

fit <- lm(log(y) ~ x, data=mydata)

调整后的 R 平方为 0.8486,从表面上看还不错。您可以使用绘图查看拟合,例如:

plot(fit, which=2)

但也许,它毕竟不是那么合适:

last_plot() + geom_line(aes(x=x, y=exp(fit$fitted.values)))
于 2012-08-07T11:32:06.017 回答
2

查看此文档:http ://cran.r-project.org/doc/contrib/Ricci-distributions-en.pdf

简而言之,首先您需要确定模型以适合您的数据(例如指数),然后估计其参数。

以下是一些广泛使用的发行版: http ://www.itl.nist.gov/div898/handbook/eda/section3/eda366.htm

于 2012-08-07T11:34:42.983 回答