1

I have a data like that I want to fit decay.

library(tidyverse)
library(broom)
t = 1:100
x=1:80
y1=sample(seq(from = 20, to = 50), size = 100, replace = TRUE)
y1<-y1 %>% jitter()
y2 = 24 + (60 - 24) * -0.01 * x %>% jitter(10)



df1 <- tibble(t = t, y = y1, sensor = 'sensor1') %>% 
  rbind(. , data.frame(t = x, y = y2, sensor = 'sensor2'))


fit <- nls(y ~ SSasymp(t, yf, y0, log_alpha), data = sensor1)
fit

# Fit the data
fitted <- df %>% 
  nest(-sensor) %>%
  mutate(
    fit = map(data, ~nls(y ~ SSasymp(t, yf, y0, log_alpha), data = .)),
    tidied = map(fit, tidy),
    augmented = map(fit, augment),
  )  

And I got :

Error in mutate(): ! Problem while computing fit = map(data, ~nls(y ~ SSasymp(t, yf, y0, log_alpha), data = .)). Caused by error in nls(): ! singular gradient

Could you please let me where was the issue.

Ali Roghani
  • 495
  • 2
  • 7
  • The sample data don't look like they can easily be fitted to this model – Allan Cameron Mar 13 '22 at 21:59
  • The code was designed for fitting exponential decays. – Ali Roghani Mar 13 '22 at 23:07
  • Yes, but if you plot the data, sensor1 looks like random, non-decaying noise, and sensor2 looks like a linear decline without an asymptote. The error is telling you that the parameters cannot be optimised based on the supplied data. – Allan Cameron Mar 13 '22 at 23:10
  • Thanks a lot. If I want to fit it if both sensor1 and sensor will be random, non-decaying noise. how should be? That helps me too. – Ali Roghani Mar 13 '22 at 23:13
  • 2
    The point is that there _isn't_ an optimal fit - at least one of the parameters has multiple (even infinite) values that give the same result. It doesn't make any sense to fit it. You would need to look at different models. – Allan Cameron Mar 13 '22 at 23:22

0 Answers0