在R中绘制NLS,实际数据和估计参数

  • 本文关键字:数据 参数 绘制 NLS nls
  • 更新时间 :
  • 英文 :


我的数据集ICM_Color0具有以下结构,其中列为:

卢姆瑞多区RT.ms情况

有2599行。

存在三种亮度=[13,19,25];和两种噪声= [1,2]-> 3x2 = 6的条件。

身体状况 :

Lum   Ruido   Condicion
13     1        1
13     2        2
19     1        3
19     2        4
25     1        5
25     2        6

我的模型是:

Color0.nls <- nls(RT.ms ~ 312 + K[Condicion]/(Dist^1),     
              data = ICM_Color0, start = list(K = rep(1,6)))

> summary(Color0.nls)
Formula: RT.ms ~ RT0.0 + K[Condicion]/(Dist^n)
Parameters:
   Estimate Std. Error t value Pr(>|t|)    
K1  1.84108    0.03687   49.94   <2e-16 ***
K2  2.04468    0.03708   55.14   <2e-16 ***
K3  1.70841    0.03749   45.58   <2e-16 ***
K4  2.09915    0.03628   57.86   <2e-16 ***
K5  1.62961    0.03626   44.94   <2e-16 ***
K6  2.18235    0.03622   60.26   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 120.5 on 2593 degrees of freedom
Number of iterations to convergence: 1 
Achieved convergence tolerance: 1.711e-08

我需要绘制实际数据和参数估计。

我已经对文献进行了全面的回顾,但没有发现像我这样的模型的例子,其中模型取决于条件变量。有人能指点我吗?

Thanks to lot

从回归(非线性或非非线性)中绘制拟合线相当简单。我最常用的方法是使用predict从原始数据中计算预测值,然后将这些值绘制为数据散点图顶部的线条。

你没有给出一个可重复的例子,所以我根据这个答案做了一些非线性数据。

# Create data to fit with non-linear regression
set.seed(16)
x = seq(100)
y = rnorm(200, 50 + 30 * x^(-0.2), 1)
site = rep(c("a", "b"), each = 100)
dat = data.frame(expl = c(x, x), resp = y, site)

然后我拟合一个非线性回归,允许每个参数随分组变量site而变化。

fit1 = nls(resp ~ a[site] + b[site] * expl^(-c[site]), data = dat, 
      start = list(a = c(80, 80), b = c(20, 20),  c = c(.2, .2)))

现在我只是使用predict.nls

将拟合值添加到数据集
dat$pred = predict(fit1)

我使用ggplot2包绘制了这个。

ggplot(data = dat, aes(x = expl, y = resp, color = site)) +
    geom_point() +
    geom_line(aes(y = pred))

在这种情况下,我允许所有参数随站点而变化,看起来您可以在ggplotgeom_smooth中完成所有这些操作。我在这里找到了一个很好的例子。

下面是玩具数据集的样子。

ggplot(data = dat, aes(x = expl, y = resp, color = site)) +
    geom_point() +
    geom_smooth(aes(group = site), method = "nls", formula = "y ~ a + b*x^(-c)", 
              start = list(a = 80, b = 20,  c = .2), se = FALSE)

最新更新