IQSS / Amelia

Amelia: A Package for Missing Data

Home Page:http://gking.harvard.edu/amelia

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error in as.POSIXct.numeric(value) : 'origin' must be supplied

englianhu opened this issue · comments

Y2015W1_m1_NA <- Y2015W1_m1 %>% 
  dplyr::select(index, BidOpen, BidHigh, BidLow, BidClose, AskOpen, AskHigh, AskLow,  AskClose) %>% 
  prodNA(noNA = 0.01)
> Y2015W1_m1_NA
# A tibble: 7,200 x 9
   index               BidOpen BidHigh BidLow BidClose AskOpen AskHigh AskLow AskClose
   <dttm>                <dbl>   <dbl>  <dbl>    <dbl>   <dbl>   <dbl>  <dbl>    <dbl>
 1 2015-01-04 22:00:00    120.    121.   120.     121.    121.    121.   121.     121.
 2 2015-01-04 22:01:00    121.    121.   120.     121.    121.    121.   121.     121.
 3 2015-01-04 22:02:00    121.    121.   121.     121.    121.    121.   121.     121.
 4 2015-01-04 22:03:00    121.    121.   121.     121.    121.    121.   121.     121.
 5 2015-01-04 22:04:00    121.    121.   120.     121.    121.    121.   121.     121.
 6 2015-01-04 22:05:00    121.    121.   120.     120.    121.    121.   121.     121.
 7 2015-01-04 22:06:00    120.    121.   120.     121.    121.    121.   120.     121.
 8 2015-01-04 22:07:00    121.    121.   121.     121.    121.    121.   121.     121.
 9 2015-01-04 22:08:00    121.    121.   121.     121.    121.    121.   121.     121.
10 2015-01-04 22:09:00    121.    121.   121.     121.    121.    121.   121.     121.
# ... with 7,190 more rows
> Y2015W1_m1_NA %>% str
Classestbl_df’, ‘tbland 'data.frame':	7200 obs. of  9 variables:
 $ index   : POSIXct, format: "2015-01-04 22:00:00" "2015-01-04 22:01:00" "2015-01-04 22:02:00" ...
 $ BidOpen : num  120 121 121 121 121 ...
 $ BidHigh : num  121 121 121 121 121 ...
 $ BidLow  : num  120 120 121 121 120 ...
 $ BidClose: num  121 121 121 121 121 ...
 $ AskOpen : num  121 121 121 121 121 ...
 $ AskHigh : num  121 121 121 121 121 ...
 $ AskLow  : num  121 121 121 121 121 ...
 $ AskClose: num  121 121 121 121 121 ...
> Y2015W1_m1_NA %>% amelia(ts = 'index')
-- Imputation 1 --

  1  2
Error in as.POSIXct.numeric(value) : 'origin' must be supplied
> Y2015W1_m1_NA %>% amelia(idvars = 'index')
-- Imputation 1 --

  1  2
Error in as.POSIXct.numeric(value) : 'origin' must be supplied

By refer to https://cran.r-project.org/web/packages/Amelia/vignettes/amelia.pdf, I tried to impute the missing value for tibble format data frame but the system prompt me the date origin error.

> Y2015W1_m1_NA[-1] %>% amelia(m = 1)
-- Imputation 1 --

  1  2


Amelia output with 1 imputed datasets.
Return code:  1 
Message:  Normal EM convergence. 

Chain Lengths:
--------------
Imputation 1:  2

> Y2015W1_m1_NA %>% 
+     mutate(index = as_date(index)) %>% 
+     amelia(ts = 'index', m = 1)
-- Imputation 1 --

  1  2
Error in as.Date.numeric(value) : 'origin' must be supplied
> Y2015W1_m1_NA %>% 
+     mutate(index = as_date(index)) %>% 
+     amelia(ts = 'index', m = 1, origin = '1970-01-01')
-- Imputation 1 --

  1  2
Error in as.Date.numeric(value) : 'origin' must be supplied
> Y2015W1_m1_NA %>% 
+     mutate(index = as_date(index)) %>% 
+     amelia(ts = ymd('index', origin = '1970-01-01'), m = 1)
Error in Ops.Date(vars, 1) : %% not defined for "Date" objects
In addition: Warning message:
 1 failed to parse. 
> Y2015W1_m1_NA %>% 
+     mutate(index = as_date(index)) %>% 
+     amelia(idvars = ymd('index', origin = '1970-01-01'), m = 1)
Error in `-.Date`(idvars) : unary - is not defined for "Date" objects
In addition: Warning message:
 1 failed to parse. 

I noticed that when I removed the time series parameter only will do.

> Y2015W1_m1_NA[-1] %>% amelia(m = 1) %>% .$imputation %>% .$`imp1` %>% cbind(Y2015W1_m1_NA[1], .)
-- Imputation 1 --

  1  2

                   index  BidOpen  BidHigh   BidLow BidClose  AskOpen  AskHigh   AskLow AskClose
1    2015-01-04 22:00:00 120.4660 120.5420 120.4420 120.5020 120.5340 120.6380 120.5200 120.6080
2    2015-01-04 22:01:00 120.5020 120.5700 120.4840 120.5380 120.6080 120.6710 120.5660 120.6100
3    2015-01-04 22:02:00 120.5380 120.5880 120.5330 120.5680 120.6100 120.6780 120.5880 120.6120
4    2015-01-04 22:03:00 120.5680 120.6060 120.5080 120.5390 120.6120 120.7080 120.5660 120.6680
5    2015-01-04 22:04:00 120.5390 120.5940 120.4720 120.5760 120.6680 120.7140 120.5060 120.5980
6    2015-01-04 22:05:00 120.5760 120.5760 120.4920 120.4970 120.5980 120.6290 120.5060 120.6050
7    2015-01-04 22:06:00 120.4970 120.5620 120.4540 120.5220 120.6050 120.6160 120.4920 120.5460
8    2015-01-04 22:07:00 120.5220 120.5740 120.5070 120.5280 120.5460 120.5990 120.5240 120.5740
9    2015-01-04 22:08:00 120.5280 120.5820 120.5120 120.5280 120.5740 120.6050 120.5460 120.5960
10   2015-01-04 22:09:00 120.5280 120.5880 120.5170 120.5450 120.5960 120.6060 120.5540 120.5600
11   2015-01-04 22:10:00 120.5450 120.5500 120.4990 120.4990 120.5600 120.5760 120.5460 120.5460
12   2015-01-04 22:11:00 120.4990 120.5110 120.4990 120.5020 120.5460 120.5750 120.5460 120.5710
13   2015-01-04 22:12:00 120.5020 120.5060 120.4950 120.5040 120.5710 120.5710 120.5380 120.5550
14   2015-01-04 22:13:00 120.5040 120.5040 120.4740 120.4970 120.5380 120.5720 120.5005 120.5280
15   2015-01-04 22:14:00 120.4970 120.5460 120.4600 120.4620 120.5280 120.5760 120.5040 120.5420
16   2015-01-04 22:15:00 120.4620 120.4620 120.4570 120.4580 120.5420 120.5420 120.4820 120.4960
17   2015-01-04 22:16:00 120.4580 120.4610 120.3020 120.3200 120.4960 120.5180 120.3720 120.4050
18   2015-01-04 22:17:00 120.3200 120.4150 120.3200 120.3660 120.4050 120.4610 120.3900 120.4100
19   2015-01-04 22:18:00 120.3660 120.3710 120.3560 120.3560 120.4100 120.4450 120.3740 120.3960
20   2015-01-04 22:19:00 120.3560 120.4000 120.3450 120.3490 120.3960 120.4150 120.3720 120.3900
21   2015-01-04 22:20:00 120.3490 120.4060 120.3460 120.3980 120.3900 120.4270 120.3580 120.4140
22   2015-01-04 22:21:00 120.3980 120.4220 120.3560 120.4220 120.4140 120.4460 120.3780 120.4360
23   2015-01-04 22:22:00 120.4220 120.4340 120.4200 120.4270 120.4360 120.4530 120.4280 120.4400
24   2015-01-04 22:23:00 120.4270 120.4290 120.4160 120.4240 120.4400 120.4520 120.4260 120.4360
25   2015-01-04 22:24:00 120.4240 120.4240 120.4140 120.4200 120.4360 120.4430 120.4180 120.4340
26   2015-01-04 22:25:00 120.4200 120.4247 120.4110 120.4120 120.4340 120.4430 120.4280 120.4430
27   2015-01-04 22:26:00 120.4120 120.4180 120.3570 120.3600 120.4430 120.4430 120.3760 120.3780
28   2015-01-04 22:27:00 120.3600 120.4230 120.3570 120.4120 120.3780 120.4450 120.3720 120.4260
29   2015-01-04 22:28:00 120.4120 120.4140 120.3960 120.4141 120.4260 120.4360 120.4000 120.4280
30   2015-01-04 22:29:00 120.4040 120.4360 120.4040 120.4180 120.4280 120.4600 120.4120 120.4600
31   2015-01-04 22:30:00 120.4180 120.4800 120.4180 120.4640 120.4600 120.5120 120.4440 120.4740
32   2015-01-04 22:31:00 120.4640 120.4820 120.4500 120.4800 120.4740 120.5020 120.4560 120.4960
33   2015-01-04 22:32:00 120.4800 120.4990 120.4590 120.4690 120.4960 120.5340 120.4770 120.5340
34   2015-01-04 22:33:00 120.4690 120.5180 120.4690 120.5180 120.5340 120.5520 120.5240 120.5430
35   2015-01-04 22:34:00 120.5180 120.5610 120.5020 120.5190 120.5430 120.5910 120.5180 120.5830
36   2015-01-04 22:35:00 120.5190 120.5190 120.5040 120.5040 120.5830 120.5830 120.5740 120.5750
37   2015-01-04 22:36:00 120.5040 120.5130 120.4960 120.4960 120.5750 120.5750 120.5220 120.5220
38   2015-01-04 22:37:00 120.4960 120.5110 120.4340 120.4910 120.5220 120.5420 120.4940 120.5330
39   2015-01-04 22:38:00 120.4910 120.5560 120.4750 120.5500 120.5330 120.5830 120.5330 120.5710
40   2015-01-04 22:39:00 120.5500 120.5790 120.5260 120.5750 120.5710 120.6100 120.5430 120.6010
41   2015-01-04 22:40:00 120.5750 120.5840 120.5730 120.5810 120.6010 120.6130 120.5970 120.6110
42   2015-01-04 22:41:00 120.5810 120.5830 120.5220 120.5240 120.6110 120.6170 120.5460 120.5530
43   2015-01-04 22:42:00 120.5240 120.5790 120.5130 120.5750 120.5530 120.6090 120.5360 120.6070
44   2015-01-04 22:43:00 120.5750 120.5750 120.5150 120.5150 120.6070 120.6070 120.5470 120.5470
45   2015-01-04 22:44:00 120.5150 120.5560 120.5140 120.5560 120.5470 120.5980 120.5450 120.5920
46   2015-01-04 22:45:00 120.5560 120.5970 120.5560 120.5840 120.5920 120.6200 120.5880 120.6110
47   2015-01-04 22:46:00 120.5840 120.5840 120.5640 120.5650 120.6110 120.6110 120.5910 120.5940
48   2015-01-04 22:47:00 120.5650 120.5740 120.5650 120.5690 120.5940 120.6030 120.5940 120.5990
49   2015-01-04 22:48:00 120.5690 120.5690 120.5480 120.5690 120.5990 120.6010 120.5840 120.5930
50   2015-01-04 22:49:00 120.5690 120.5900 120.5640 120.5790 120.5930 120.6100 120.5860 120.6030
51   2015-01-04 22:50:00 120.5790 120.5840 120.5710 120.5810 120.6030 120.6050 120.5890 120.6050
52   2015-01-04 22:51:00 120.5810 120.5870 120.5750 120.5750 120.6050 120.6070 120.5910 120.6010
53   2015-01-04 22:52:00 120.5750 120.5950 120.5670 120.5860 120.6010 120.6110 120.5910 120.5990
54   2015-01-04 22:53:00 120.5860 120.5870 120.5660 120.5700 120.5990 120.6030 120.5890 120.5930
55   2015-01-04 22:54:00 120.5700 120.5720 120.5640 120.5680 120.5930 120.5960 120.5850 120.5920
56   2015-01-04 22:55:00 120.5680 120.5750 120.5640 120.5660 120.5920 120.5990 120.5780 120.5790
57   2015-01-04 22:56:00 120.5660 120.5690 120.5230 120.5250 120.5790 120.5850 120.5490 120.5510
58   2015-01-04 22:57:00 120.5250 120.5390 120.4980 120.5050 120.5510 120.5620 120.5130 120.5280
59   2015-01-04 22:58:00 120.5050 120.5220 120.5050 120.5090 120.5280 120.5420 120.5270 120.5350
60   2015-01-04 22:59:00 120.5090 120.5360 120.5090 120.5360 120.5311 120.5560 120.5350 120.5560
61   2015-01-04 23:00:00 120.5360 120.5380 120.5220 120.5340 120.5560 120.5560 120.5260 120.5480
62   2015-01-04 23:01:00 120.5340 120.5510 120.5280 120.5400 120.5480 120.5630 120.5290 120.5540
63   2015-01-04 23:02:00 120.5400 120.5500 120.5370 120.5480 120.5540 120.5640 120.5490 120.5620
64   2015-01-04 23:03:00 120.5480 120.5530 120.5360 120.5400 120.5620 120.5670 120.5470 120.5520
65   2015-01-04 23:04:00 120.5400 120.5680 120.5380 120.5670 120.5520 120.5800 120.5500 120.5780
66   2015-01-04 23:05:00 120.5670 120.5750 120.5460 120.5470 120.5780 120.5820 120.5560 120.5560
67   2015-01-04 23:06:00 120.5470 120.5520 120.5290 120.5420 120.5560 120.5640 120.5410 120.5540
68   2015-01-04 23:07:00 120.5420 120.5430 120.5080 120.5110 120.5540 120.5550 120.5200 120.5200
69   2015-01-04 23:08:00 120.5110 120.5140 120.5010 120.5120 120.5200 120.5240 120.5110 120.5245
70   2015-01-04 23:09:00 120.5120 120.5130 120.5020 120.5130 120.5220 120.5220 120.5070 120.5220
71   2015-01-04 23:10:00 120.5130 120.5130 120.5000 120.5020 120.5220 120.5220 120.4980 120.5032
72   2015-01-04 23:11:00 120.5020 120.5020 120.4990 120.5000 120.5060 120.5110 120.4990 120.5060
73   2015-01-04 23:12:00 120.5000 120.5010 120.4790 120.4790 120.5060 120.5090 120.4890 120.4890
74   2015-01-04 23:13:00 120.4790 120.4800 120.4420 120.4610 120.4890 120.4890 120.4490 120.4720
75   2015-01-04 23:14:00 120.4610 120.4650 120.4430 120.4470 120.4720 120.4760 120.4520 120.4570
76   2015-01-04 23:15:00 120.4470 120.4850 120.4460 120.4660 120.4570 120.4950 120.4550 120.4800
77   2015-01-04 23:16:00 120.4660 120.4670 120.4430 120.4500 120.4800 120.4800 120.4530 120.4620
78   2015-01-04 23:17:00 120.4500 120.4530 120.4390 120.4520 120.4620 120.4630 120.4500 120.4590
79   2015-01-04 23:18:00 120.4470 120.4590 120.4430 120.4450 120.4590 120.4680 120.4490 120.4520
80   2015-01-04 23:19:00 120.4450 120.4460 120.4090 120.4110 120.4520 120.4520 120.4200 120.4220
81   2015-01-04 23:20:00 120.4110 120.4810 120.4110 120.4810 120.4220 120.4930 120.4210 120.4930
82   2015-01-04 23:21:00 120.4810 120.4810 120.4560 120.4610 120.4930 120.4930 120.4672 120.4740
83   2015-01-04 23:22:00 120.4610 120.4650 120.4504 120.4630 120.4740 120.4770 120.4670 120.4770
84   2015-01-04 23:23:00 120.4630 120.4630 120.4330 120.4330 120.4770 120.4770 120.4400 120.4410
85   2015-01-04 23:24:00 120.4330 120.4430 120.4200 120.4220 120.4410 120.4520 120.4280 120.4310
86   2015-01-04 23:25:00 120.4220 120.4319 120.4080 120.4230 120.4310 120.4400 120.4150 120.4290
87   2015-01-04 23:26:00 120.4230 120.4410 120.4200 120.4380 120.4290 120.4500 120.4290 120.4440
88   2015-01-04 23:27:00 120.4380 120.4390 120.4170 120.4260 120.4440 120.4510 120.4200 120.4310
89   2015-01-04 23:28:00 120.4260 120.4270 120.4000 120.4170 120.4310 120.4350 120.4080 120.4220
90   2015-01-04 23:29:00 120.4170 120.4240 120.4130 120.4160 120.4220 120.4300 120.4190 120.4250
91   2015-01-04 23:30:00 120.4160 120.4190 120.3920 120.3920 120.4250 120.4280 120.4010 120.4010
92   2015-01-04 23:31:00 120.3920 120.3990 120.3800 120.3910 120.4010 120.4080 120.3870 120.4010
93   2015-01-04 23:32:00 120.3910 120.4040 120.3900 120.4010 120.4010 120.4110 120.3970 120.4050
94   2015-01-04 23:33:00 120.4010 120.4060 120.3960 120.4020 120.4050 120.4130 120.4030 120.4120
95   2015-01-04 23:34:00 120.4020 120.4040 120.3980 120.4040 120.4120 120.4130 120.4070 120.4100
96   2015-01-04 23:35:00 120.4040 120.4040 120.3980 120.4000 120.4100 120.4110 120.4060 120.4080
97   2015-01-04 23:36:00 120.4000 120.4170 120.3990 120.4150 120.4080 120.4250 120.4080 120.4210
98   2015-01-04 23:37:00 120.4150 120.4430 120.4150 120.4410 120.4210 120.4450 120.4210 120.4440
99   2015-01-04 23:38:00 120.4410 120.4530 120.4320 120.4500 120.4440 120.4600 120.4380 120.4560
100  2015-01-04 23:39:00 120.4500 120.4590 120.4210 120.4210 120.4560 120.4650 120.4300 120.4300
101                 <NA> 120.4210 120.4240 120.3720 120.3790 120.4300 120.4340 120.3790 120.3860
102  2015-01-04 23:41:00 120.3790 120.4090 120.3790 120.4080 120.3860 120.4190 120.3840 120.4170
103  2015-01-04 23:42:00 120.4080 120.4150 120.4060 120.4120 120.4170 120.4240 120.4160 120.4200
104  2015-01-04 23:43:00 120.4120 120.4180 120.4050 120.4150 120.4200 120.4230 120.4137 120.4230
105  2015-01-04 23:44:00 120.4150 120.4311 120.4130 120.4280 120.4230 120.4390 120.4230 120.4350
106  2015-01-04 23:45:00 120.4280 120.4450 120.4160 120.4420 120.4350 120.4530 120.4270 120.4490
107  2015-01-04 23:46:00 120.4420 120.4480 120.4350 120.4460 120.4490 120.4550 120.4440 120.4540
108  2015-01-04 23:47:00 120.4460 120.4510 120.4420 120.4420 120.4540 120.4590 120.4480 120.4490
109  2015-01-04 23:48:00 120.4420 120.4740 120.4400 120.4670 120.4490 120.4780 120.4470 120.4750
110  2015-01-04 23:49:00 120.4670 120.4730 120.4370 120.4390 120.4750 120.4810 120.4440 120.4480
111  2015-01-04 23:50:00 120.4390 120.4730 120.4390 120.4720 120.4480 120.4750 120.4460 120.4750
 [ reached getOption("max.print") -- omitted 7089 rows ]

Is that because of the datetime parameter cannot be imputed?

There has another error when I knit the RMarkdown file.

- error: chol(): decomposition failed
- terminate called after throwing an instance of 'std::runtime_error'
-   what():  chol(): decomposition failed

Sorry for the delay. This is an issue with imputing POSIX variables. I actually think it's a bad idea to do it through the coercion in amelia. Better to manually create a new variable that is numeric and encodes the right structure for time as it relates to your data. So we created an error message that tries to convey that and errors our before imputation. Thanks for reporting!

Sorry for the delay. This is an issue with imputing POSIX variables. I actually think it's a bad idea to do it through the coercion in amelia. Better to manually create a new variable that is numeric and encodes the right structure for time as it relates to your data. So we created an error message that tries to convey that and errors our before imputation. Thanks for reporting!