SteffenMoritz / imputeTS

CRAN R Package: Time Series Missing Value Imputation

Home Page:http://steffenmoritz.github.io/imputeTS/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error in `optim()`: ! L-BFGS-B needs finite values of 'fn'

ningyile opened this issue · comments

Dear Steffen Moritz:

Recently I've been trying to impute my time series data in my project with the na_kalman function based on each subject (id in the dataset df).

The dataset df is:

> head(df)
        id                time value
1 30000153 2174-09-29 12:09:00    NA
2 30000153 2174-09-29 13:09:00    NA
3 30000153 2174-09-29 13:27:00   158
4 30000153 2174-09-29 14:07:00   176
5 30000153 2174-09-29 14:09:00    NA
6 30000153 2174-09-29 15:09:00    NA

In view of the na_kalman function defines that there are at least 3 observations in the time series imputation, I removed individuals with less than 3 values. Then the code for imputation with the na_kalman function based on id is:

> df_kalman = df %>% 
+   dplyr::group_by(id) %>%
+   dplyr::mutate(non_na_count = sum(!is.na(value))) %>% 
+   dplyr::filter(non_na_count >= 3) %>% 
+   dplyr::arrange(id, time) %>%
+   dplyr::group_by(id) %>%
+   dplyr::mutate(value_kalman = na_kalman(value))
Error in `dplyr::mutate()`:In argument: `value_kalman = na_kalman(value)`.In group 29: `id = 30004306`.
Caused by error in `optim()`:
! L-BFGS-B needs finite values of 'fn'
Run `rlang::last_error()` to see where the error occurred.

I got an error that was similar to the previous issue. When I tried na_kalman(value, type = 'level'), the data where id = 30004306 seem to be solved, however, I got another error with the data in different id. I guess the function cannot automatically process according to the individual data situation, so there are different errors with or without the level parameter.

My data is saved in Rdata format and uploaded here. Could you please help me to process this error at your convenience? Thank you very much!
df.Rdata.zip

Hello,

I experienced the same issue after replicating some analysis that previously did not return any error message. Also, when running na_kalman on other datasets I encountered the same issue:

Error in mutate(., impt_mvpa = as.integer(na_kalman(mvpa, model = "StructTS"))) :In group 76: `id = "0720"`.
Caused by error in `optim()`:
! L-BFGS-B needs finite values of 'fn'

However, when trying to simulate the error message with a simple time series, it doesn't return any error:

library(tidyverse)
library(imputeTS)
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo

set.seed(10)
tibble(id = 'id', value = sample(c(500:10000, rep(NA, 1000)), 100)) %>% 
  mutate(value_kalman = na_kalman(value, model = 'StructTS'))
#> # A tibble: 100 × 3
#>    id    value value_kalman
#>    <chr> <int>        <dbl>
#>  1 id      990         990 
#>  2 id     4220        4220 
#>  3 id     6079        6079 
#>  4 id     2107        2107 
#>  5 id     1961        1961 
#>  6 id     8090        8090 
#>  7 id       NA        4224.
#>  8 id     4939        4939 
#>  9 id     7938        7938 
#> 10 id     6786        6786 
#> # ℹ 90 more rows

Any input is much appreciated!
Thank you

Created on 2023-05-22 with reprex v2.0.2