【问题标题】:Using R to fit a curve to a dataset using a specific equation使用 R 使用特定方程将曲线拟合到数据集
【发布时间】:2015-10-23 17:04:40
【问题描述】:

我正在使用 R。 我想使用一个特定的方程来拟合我的一个数据集的曲线(附加)

> dput(data)
structure(list(Gossypol = c(1036.331811, 4171.427741, 6039.995102, 
5909.068158, 4140.242559, 4854.985845, 6982.035521, 6132.876396, 
948.2418407, 3618.448997, 3130.376482, 5113.942098, 1180.171957, 
1500.863038, 4576.787021, 5629.979049, 3378.151945, 3589.187889, 
2508.417927, 1989.576826, 5972.926124, 2867.610671, 450.7205451, 
1120.955, 3470.09352, 3575.043632, 2952.931863, 349.0864019, 
1013.807628, 910.8879471, 3743.331903, 3350.203452, 592.3403778, 
1517.045807, 1504.491931, 3736.144027, 2818.419785, 723.885643, 
1782.864308, 1414.161257, 3723.629772, 3747.076592, 2005.919344, 
4198.569251, 2228.522959, 3322.115942, 4274.324792, 720.9785449, 
2874.651764, 2287.228752, 5654.858696, 1247.806111, 1247.806111, 
2547.326207, 2608.716056, 1079.846532), Treatment = structure(c(2L, 
3L, 4L, 5L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 5L, 1L, 2L, 3L, 4L, 5L, 
1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 
3L, 4L, 5L, 1L, 2L, 3L, 1L), .Label = c("C", "1c_2d", "3c_2d", 
"9c_2d", "1c_7d"), class = "factor"), Damage_cm = c(0.4955, 1.516, 
4.409, 3.2665, 0.491, 2.3035, 3.51, 1.8115, 0, 0.4435, 1.573, 
1.8595, 0, 0.142, 2.171, 4.023, 4.9835, 0, 0.6925, 1.989, 5.683, 
3.547, 0, 0.756, 2.129, 9.437, 3.211, 0, 0.578, 2.966, 4.7245, 
1.8185, 0, 1.0475, 1.62, 5.568, 9.7455, 0, 0.8295, 2.411, 7.272, 
4.516, 0, 0.4035, 2.974, 8.043, 4.809, 0, 0.6965, 1.313, 5.681, 
3.474, 0, 0.5895, 2.559, 0)), .Names = c("Gossypol", "Treatment", 
"Damage_cm"), row.names = c(NA, -56L), class = "data.frame")

等式是:y~yo+a*(1-b^x) 在哪里: y = Gossypol(来自我的数据集) x = Damage_cm(来自我的数据集)

其他3个参数未知: yo = Intercepta = asymptoteb = slope

我想我必须使用包nls2。到目前为止,我编写了以下代码:

data<-read.csv("Regression_exp2.csv",header=T, sep = ",")
library(nls2)
attach(data)
m<-nls(Gossypol~Y+A*(1-B^Damage_cm),data=data,start = list(Y=1700,A=4000,B=1))

这给了我错误信息:

nlsModel(formula, mf, start, wts) 中的错误:奇异梯度矩阵 在初始参数估计时

最后我想用方程画一条曲线(带SE区间,我一般用ggplot2)

此外,我想知道 R2 和 p 值。 我也会对参数 yoab 感兴趣

我以前从未这样做过,如果有人可以帮助我或给我提示如何在 R 中执行此操作,我将不胜感激?我想我必须使用非线性方法 (glm(...))

非常感谢, 迈克

【问题讨论】:

  • 我认为你只需要提高你的起始值。我绘制了您的数据和方程式,并稍微接近了,并且使用Y=1000,A=3000,B=0.7做得很好。
  • 另外两个 cmets:1. 您加载了 nls2 包,但您使用的是(默认)stats 包中的常规 nls 函数。如果你想使用nls2,你需要2。 2. 一般来说,我建议不要使用attach。它可以在这里和那里节省一点打字,但也可能导致很难找到的错误。由于nls 采用data 参数,因此使用attach() 无需输入此代码。
  • 另外,我认为默认情况下nls 对象不会出现标准错误。 This should get you started on simulating standard errors.

标签: r curve-fitting model-fitting


【解决方案1】:

你必须稍微调整一下你的起始值:

> data
    Gossypol Treatment Damage_cm
1  1036.3318     1c_2d    0.4955
2  4171.4277     3c_2d    1.5160
3  6039.9951     9c_2d    4.4090
4  5909.0682     1c_7d    3.2665
5  4140.2426     1c_2d    0.4910
...
54 2547.3262     1c_2d    0.5895
55 2608.7161     3c_2d    2.5590
56 1079.8465         C    0.0000

然后你可以调用:

m<-nls(data$Gossypol~Y+A*(1-B^data$Damage_cm),data=data,start = list(Y=1000,A=3000,B=0.5))

打印m给你:

> m
Nonlinear regression model
  model: data$Gossypol ~ Y + A * (1 - B^data$Damage_cm)
   data: data
        Y         A         B 
1303.4450 2796.0385    0.4939 
 residual sum-of-squares: 1.03e+08

现在你可以根据拟合得到数据了:

fitData <- 1303.4450 + 2796.0385*(1-0.4939^data$Damage_cm)

绘制数据以比较拟合和原始数据:

plot(data$Damage_cm, data$Gossypol, col='black')
par(new=T)
plot(data$Damage_cm,fitData, col='red', ylim=c(0,8000), axes=F, ylab='')

给你:

如果你想使用nls2,请确保它已经安装,如果没有,你可以使用

install.packages('nls2')

这样做。

library(nls2)
m2<-nls2(data$Gossypol~Y+A*(1-B^data$Damage_cm),data=data,start = list(Y=1000,A=3000,B=0.5))

它为您提供与nls 相同的值:

> m2
Nonlinear regression model
  model: data$Gossypol ~ Y + A * (1 - B^data$Damage_cm)
   data: structure(list(Gossypol = c(1036.331811, 4171.427741, 6039.995102, 5909.068158, 4140.242559, 4854.985845, 6982.035521, 6132.876396, 948.2418407, 3618.448997, 3130.376482, 5113.942098, 1180.171957, 1500.863038, 4576.787021, 5629.979049, 3378.151945, 3589.187889, 2508.417927, 1989.576826, 5972.926124, 2867.610671, 450.7205451, 1120.955, 3470.09352, 3575.043632, 2952.931863, 349.0864019, 1013.807628, 910.8879471, 3743.331903, 3350.203452, 592.3403778, 1517.045807, 1504.491931, 3736.144027, 2818.419785, 723.885643, 1782.864308, 1414.161257, 3723.629772, 3747.076592, 2005.919344, 4198.569251, 2228.522959, 3322.115942, 4274.324792, 720.9785449, 2874.651764, 2287.228752, 5654.858696, 1247.806111, 1247.806111, 2547.326207, 2608.716056, 1079.846532), Treatment = structure(c(2L, 3L, 4L, 5L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 1L), .Label = c("C", "1c_2d", "3c_2d", "9c_2d", "1c_7d"), class = "factor"), Damage_cm = c(0.4955, 1.516, 4.409, 3.2665, 0.491, 2.3035, 3.51, 1.8115, 0, 0.4435, 1.573, 1.8595, 0, 0.142, 2.171, 4.023, 4.9835, 0, 0.6925, 1.989, 5.683, 3.547, 0, 0.756, 2.129, 9.437, 3.211, 0, 0.578, 2.966, 4.7245, 1.8185, 0, 1.0475, 1.62, 5.568, 9.7455, 0, 0.8295, 2.411, 7.272, 4.516, 0, 0.4035, 2.974, 8.043, 4.809, 0, 0.6965, 1.313, 5.681, 3.474, 0, 0.5895, 2.559, 0)), .Names = c("Gossypol", "Treatment", "Damage_cm"), row.names = c(NA, -56L), class = "data.frame")
        Y         A         B 
1303.4450 2796.0385    0.4939 
 residual sum-of-squares: 1.03e+08

Number of iterations to convergence: 2 
Achieved convergence tolerance: 4.936e-06

如果你喜欢ggplot2:

ggplot(data, aes(x = Damage_cm, y = Gossypol)) +
     geom_point() +
     geom_smooth(method = "nls",
                 formula = y ~ Y + A * (1 - B^x),
                 start = c(Y=1000, A=3000, B=0.5), se = F)

虽然我担心标准错误必须在ggplot之外进行模拟。

【讨论】:

  • @Gregor:好点子,我更新了剧情。已经很晚了... ;)。既然你在 R 方面很有经验:还有什么可以改进的吗?
  • 好吧,如果你不介意我可以编辑一些 ggplot 代码来补充你的答案。否则,我觉得很好看! (虽然我也想说,既然 OP 发布了数据,你可能不需要回应它。)
  • 我不介意,随意添加一些ggplot的东西!编辑:太棒了!这样我也可以学到新的东西,谢谢!
  • 哇太棒了。非常感谢 Cleb 和 Gregor。在短短几天内,我学到了很多关于 R 的知识。我很感激!
猜你喜欢
  • 1970-01-01
  • 2016-02-02
  • 1970-01-01
  • 2016-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多