0

I want to fit to a 1 - exp(x) function to a data set , but giving higher weight to the first values. However, the following code is not working in such way:

x <-sqrt((0.05)^2+(0.05)^2)*seq(from = 1, to = 20, by = 1)
y <- c(11,20,27,32,35,36,36.5,25,16,9,4,1,7.87e-16,2.07e-15,-9.36e-16,1.61e-15,-3.81e-16,3.92e-16,7.65e-16,-8.26e-16)
temp <- data.frame(cbind(x,y))
we <- 1/(log1p(seq_along(x)))
# fit non-linear model
mod <- nls(y ~ (1 - exp(a + b * x)), data = temp, start = list(a = 0, b = 0), weights = we)

#add fitted curve
lines(temp$x, predict(mod, list(x = temp$x)))

Here is the output:

double-beep
  • 5,031
  • 17
  • 33
  • 41
mountainlake
  • 39
  • 1
  • 4

1 Answers1

0

Your specification of weights is correct. The bad fit you obtained is due to your faulty model assumption. You assumed:

y ~ 1 - exp(a + b * x)

Note that exp() gives strictly positive values, so y will be no larger than 1. However, y values in your data range up to 35.

My idea is not perfect, but it might give you a better starting point. Consider:

y ~ a * x * exp(b * x * x + c * x)

Using your data:

x <- c(0, sqrt((0.05)^2+(0.05)^2)*seq(from = 1, to = 20, by = 1))
y <- c(0, 11,20,27,32,35,36,36.5,25,16,9,4,1,7.87e-16,2.07e-15,-9.36e-16,1.61e-15,-3.81e-16,3.92e-16,7.65e-16,-8.26e-16)
fit <- nls(y ~ a * x * exp(b * x * x + c * x), start = list(a = 30, b= -1, c = -1))
plot(x, y)
lines(x, predict(fit, list(x)))

enter image description here

Zheyuan Li
  • 71,365
  • 17
  • 180
  • 248