非线性回归与对数模型

问题描述 投票:0回答:1
library(ggplot2)

dat <- structure(list(y = c(52L, 63L, 59L, 58L, 57L, 54L, 27L, 20L, 15L, 27L, 27L, 26L, 70L, 70L, 70L, 70L, 70L, 70L, 45L, 42L, 41L, 55L, 45L, 39L, 51L, 
                        64L, 57L, 39L, 59L, 37L, 44L, 44L, 38L, 57L, 50L, 56L, 66L, 66L, 64L, 64L, 60L, 55L, 52L, 57L, 47L, 57L, 64L, 63L, 49L, 49L, 
                        56L, 55L, 57L, 42L, 60L, 53L, 53L, 57L, 56L, 54L, 42L, 45L, 34L, 52L, 57L, 50L, 60L, 59L, 52L, 42L, 45L, 47L, 45L, 51L, 39L, 
                        38L, 42L, 33L, 62L, 57L, 65L, 44L, 44L, 39L, 46L,  49L, 52L, 44L, 43L, 38L), 
                  x = c(122743L, 132300L, 146144L, 179886L, 195180L, 233605L, 1400L, 1400L, 3600L, 5000L, 14900L, 16000L, 71410L, 85450L, 106018L, 
                        119686L, 189746L, 243171L, 536545L, 719356L, 830031L, 564546L, 677540L, 761225L, 551561L, 626799L, 68618L, 1211267L, 1276369L,
                        1440113L, 1153720L, 1244575L, 1328641L, 610452L, 692624L, 791953L, 4762522L, 5011232L, 5240402L, 521339L, 
                        560098L, 608641L, 4727833L, 4990042L, 5263899L, 1987296L, 2158704L, 2350927L, 7931905L, 8628608L, 8983683L, 2947957L, 3176995L, 3263118L, 
                        55402L, 54854L, 55050L, 52500L, 72000L, 68862L, 1158244L, 1099976L, 1019490L, 538146L, 471219L, 437954L, 863592L, 661055L, 
                        548097L, 484450L, 442643L, 404487L, 1033728L, 925514L, 854793L, 371420L, 285257L, 260157L, 2039241L, 2150710L, 1898614L, 
                        1175287L, 1495433L, 1569586L, 2646966L, 3330486L, 3282677L, 745784L, 858574L, 1119671L)), 
                class = "data.frame", row.names = c(NA, -90L))

 ggplot(dat, aes(x = x, y = y)) + geom_point()

enter image description here

这种关系似乎是一种非线性的关系。因此,我将安装一个我记录y和x的模型

mod.lm <- lm(log(y) ~ log(x), data = dat)
ggplot(dat, aes(x = log(x), y = log(y))) + geom_point() + geom_smooth(method = "lm") 

enter image description here

但是,我可以看到,对于较低的值,对数转换会产生较大的差异,如残差所示。然后我转向非线性最小二乘法。我以前没用过这个,但是使用这篇文章

Why is nls() giving me "singular gradient matrix at initial parameter estimates" errors?

  c.0 <- min(dat$y) * 0.5
  model.0 <- lm(log(y - c.0) ~ x, data = dat)
  start <- list(a = exp(coef(model.0)[1]), b = coef(model.0)[2], c = c.0)
  model <- nls(y ~ a * exp(b * x) + c, data = dat, start = start)

  Error in nls(y ~ a * exp(b * x) + c, data = dat, start = start) : 
    step factor 0.000488281 reduced below 'minFactor' of 0.000976562

任何人都可以告诉我这个错误意味着什么以及如何将nls模型与上述数据相匹配?

r exponential curve-fitting nls
1个回答
1
投票

在你的情况下,nls因为你的起始值不好而遇到问题,你引入了系数c,它不是线性化的形式。为了适应你的nls你可以通过以下方式做到这一点,更好的凝视值并删除系数c:

mod.glm <- glm(y ~ x, dat=dat, family=poisson(link = "log"))
start <- list(a = coef(mod.glm)[1], b = coef(mod.glm)[2])
mod.nls <- nls(y ~ exp(a + b * x), data = dat, start = start)

我建议使用glm,如上所示,而不是使用nls来查找系数。

如果线性化模型(mod.lm)的估计值不应该有偏差,则需要对其进行调整。

mod.lm <- lm(log(y) ~ log(x), data = dat)
mean(dat$y) #50.44444
mean(predict(mod.glm, type="response")) #50.44444
mean(predict(mod.nls)) #50.44499
mean(exp(predict(mod.lm))) #49.11622 !
f <- log(mean(dat$y) / mean(exp(predict(mod.lm)))) #bias corection for a
mean(exp(coef(mod.lm)[1] + f + coef(mod.lm)[2]*log(dat$x))) #50.44444

如果您想在自己的评论中获得James Phillips给出的系数,您可以尝试:

mod.nlsJP <- nls(y ~ a * (x^(b*x)) + offset, data=dat, start=list(a=-30, b=-5e-6, offset=50))
© www.soinside.com 2019 - 2024. All rights reserved.