Zero149 Zero149 - 1 month ago 6
R Question

how build a nonlinear regression and find the error of approximation

There was a problem in constructing the approximation of the diode voltage-ampere characteristic(code below).

library("ggplot2")
mat <- read.table(file='vah_p_1',header =TRUE)
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


The vah_p_1 is located

x y
0 4
0.25 5
0.27 6
0.29 7
0.31 8
0.33 10
0.34 13
0.36 16
0.37 20
0.38 23
0.39 28
0.4 37
0.41 43
0.42 55
0.43 67
0.44 81
0.45 94
0.46 118
0.47 143
0.48 187
0.49 225
0.5 272
0.51 340
0.52 430
0.53 510
0.54 626
0.55 900
0.56 1020
0.57 1220
0.58 1640
0.59 1850
0.6 2360
0.61 2930
0.62 3570
0.63 4290
0.64 5000
0.65 6570
0.66 7390
0.67 9230
0.68 9960


Approximation line is not being built !!! What is the problem? How to find the approximation error?

Answer Source

Good evening :)

I usually put the aes and data in to the initial ggplot for these cases. When I did that, it threw a warning. I tried the nls stand alone and there is an error in there. I'd suggest investigating this, and see if resolving the problem resolves your plot issues. Code:

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 thrown is:

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

Edit: Always check your starting points:

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

Transformation produces almost all infinites

I followed this link for style: https://plot.ly/ggplot2/stat_smooth/

Cheers, Jonny