如何建立非线性回归并找出近似误差

时间:2017-09-21 19:22:59

标签: r

构造二极管电压安培特性的近似值存在问题(下面的代码)。

library(ggplot2)
chart <- ggplot() + geom_point(data =  mat,aes(x = x, y = y)) + 
stat_smooth(method = 'nls', formula = 'y~a*(exp(x/b) - 1)',
         method.args = list(start=c(a=0.1646, b=9.5e-8)),se=FALSE)
chart

mat

mat <- structure(list(x = c(0, 0.25, 0.27, 0.29, 0.31, 0.33, 0.34, 0.36, 
0.37, 0.38, 0.39, 0.4, 0.41, 0.42, 0.43, 0.44, 0.45, 0.46, 0.47, 
0.48, 0.49, 0.5, 0.51, 0.52, 0.53, 0.54, 0.55, 0.56, 0.57, 0.58, 
0.59, 0.6, 0.61, 0.62, 0.63, 0.64, 0.65, 0.66, 0.67, 0.68), y = c(4L, 
5L, 6L, 7L, 8L, 10L, 13L, 16L, 20L, 23L, 28L, 37L, 43L, 55L, 
67L, 81L, 94L, 118L, 143L, 187L, 225L, 272L, 340L, 430L, 510L, 
626L, 900L, 1020L, 1220L, 1640L, 1850L, 2360L, 2930L, 3570L, 
4290L, 5000L, 6570L, 7390L, 9230L, 9960L)), .Names = c("x", "y"
), class = "data.frame", row.names = c(NA, -40L))

未建立逼近线!!!问题是什么?如何找到逼近误差?

1 个答案:

答案 0 :(得分:1)

晚上好:)

对于这些情况,我通常会将aesdata放入初始ggplot中。当我这样做时,它发出警告。我单独尝试了nls站,但那里有一个错误。我建议调查一下,看看解决问题是否可以解决你的情节问题。代码:

chart <- ggplot(data = mat, aes(x = x, y = y)) + geom_point() + 
  stat_smooth(method = 'nls', formula = 'y~a*(exp(x/b) - 1)',
              method.args = list(start=c(a=0.1646, b=9.5e-8)),se=FALSE)
chart

nls('y~a*(exp(x/b) - 1)', mat, start=c(a=0.1646, b=9.5e-8))

引发的错误是:

Error in numericDeriv(form[[3L]], names(ind), env) : 
Missing value or an infinity produced when evaluating the model

编辑:始终检查您的起点:

a=0.1646
b=9.5e-8
y <- a*(exp(mat$x/b) - 1)
y
plot(mat$x, y)

转型产生几乎所有的无限

我按照这个链接的风格: https://plot.ly/ggplot2/stat_smooth/

干杯, 强尼