Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中拟合函数_R_Curve Fitting_Lm_Nls - Fatal编程技术网

在R中拟合函数

在R中拟合函数,r,curve-fitting,lm,nls,R,Curve Fitting,Lm,Nls,我有几个数据点(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 <

我有几个数据点(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")
有人能给我指出正确的方向吗

跟进

在阅读了你的评论并在谷歌上搜索了一会儿之后,我调整了
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)

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

fit <- lm(log(y) ~ x, data=mydata)
但也许,它毕竟不太合适:

last_plot() + geom_line(aes(x=x, y=exp(fit$fitted.values)))
查看此文档:

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

以下是一些广泛使用的发行版:

也许为您的模型使用立方规格,并通过
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.
#导入数据

数据集我认为正确的地点是向你介绍统计学101课程。您至少可以向我们展示一下您在
lm
方面的努力。我建议您阅读R手册:在您的RCONSOLE中键入
?lm
?nls
?公式
。对不起,我的懒惰-我现在有点沮丧。我添加了我使用
nls所做的步骤及其产生的错误。我知道这是一个老问题,但您是否都尝试过使用
lm(y~poly(x,3),data=mydata)
?您可以尝试不同次数的多项式,并使用
anova
比较
lm
的结果。请编辑您的问题-您现在参考未定义的
f(x,a,b,c)
。这是怎么一回事?您只有
f(x,a,b)
使用log-on响应变量意味着什么?简单地说,OP怀疑存在对数关系,因此拟合x~log(y)的线性模型是一种快速检查方法。
last_plot() + geom_line(aes(x=x, y=exp(fit$fitted.values)))
# 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.