Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.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_Regression_Exponential_Nls - Fatal编程技术网

R 如何建立指数回归模型?

R 如何建立指数回归模型?,r,regression,exponential,nls,R,Regression,Exponential,Nls,我有一个小的(txt文件) 我想得到R的指数回归 我正在使用的命令有: regression <- read.delim("C:/Users/david/OneDrive/Desktop/regression.txt") View(regression) source('~/.active-rstudio-document', echo=TRUE) m <- nls(DelSqRho ~ (1-exp(-a*(d-b)**2)), data=regression,

我有一个小的(txt文件)

我想得到R的指数回归

我正在使用的命令有:

regression <- read.delim("C:/Users/david/OneDrive/Desktop/regression.txt")
View(regression)
source('~/.active-rstudio-document', echo=TRUE)
m <- nls(DelSqRho ~ (1-exp(-a*(d-b)**2)), data=regression, start=list(a=1, b=1))
y_est<-predict(m,regression$d)
plot(x,y)
lines(x,y_est)
summary(m)

我不知道如何解决它,如何获得指数回归,请告诉我,有什么提示吗?
nls
对起始参数的值非常敏感,因此您希望选择能够合理拟合数据的值(
minpack.lm::nlsLM
可能更宽容一些)

您可以在起始值
a=1
b=1
处绘制曲线,并发现它在捕捉曲线方面做得不好

regression <- read.delim("regression.txt")
with(regression, plot(d, DelSqRho, ylim=c(-3, 1)))
xs <- seq(min(regression$d), max(regression$d), length=100)
a <- 1; b <- 1; ys <- 1 - exp(-a* (xs - b)**2)
lines(xs, ys)
(有时无法以这种方式重新排列数据,您可以尝试通过绘制不同起始参数处的曲线来大致了解参数。也可以对起始参数进行蛮力搜索或网格搜索。)

我们现在可以尝试在以下参数下估计
nls
模型:

m <- nls(DelSqRho ~ 1-exp(-a*(d-b)**2), data=regression, start=list(a=a, b=b))
coef(m)
#          a          b 
# -0.2379078  2.8868374 

m。。。在这种情况下,您可以通过拟合
start_m=lm(log(1/(1-DelSqRho))~poly(d,2,raw=TRUE),回归)
(这是重新排列的模型方程)和获得
a=coef(start_m)[3]
b=sqrt(coef(start_m)[1]/coef(start_m)[3]来获得启动参数的想法
@user20650谢谢您的回答,但我没有正确地得到它们,我尝试了
start_m=lm(log(1/(1-DelSqRho))~poly(d,2,raw=TRUE),回归);;;;;;谢谢,我尝试了所有命令,但最后一块命令不起作用,它向我提示数据中的错误。frame(d=xs):未找到对象“xs”
other.Chemist
xs
在第一个代码块中定义
start_m <- lm(log(1/(1-DelSqRho)) ~ poly(d, 2, raw=TRUE), regression)
unname(a <- coef(start_m)[3]) # as `a` is aligned with the quadratic term
# [1] -0.2345953 
unname(b <- sqrt(coef(start_m)[1]/coef(start_m)[3]))
# [1] 2.933345 
m <- nls(DelSqRho ~ 1-exp(-a*(d-b)**2), data=regression, start=list(a=a, b=b))
coef(m)
#          a          b 
# -0.2379078  2.8868374 
# note that `newdata` must be a named list or data frame 
# in which to look for variables with which to predict.
y_est <- predict(m, newdata=data.frame(d=xs))
with(regression, plot(d, DelSqRho))
lines(xs, y_est, col="red", lwd=2)