Search code examples
rfor-loopforecasting

Iteratively compute drift coefficient from random walk with drift function in R, compile into list


My objective is to list the drift coefficient from a random walk with drift forecast function, applied to a set of historical data (below). Specifically I am trying to gather the drift coefficient starting from the random walk with drift model of the first year, then cumulatively to the last, recording the coefficient each time, meaning iteratively or each additional year (recording this into a list? if that is appropriate). To be clear each new random walk forecast is including all the previous years.

The data is a list of 241 consumption levels, and I am attempting to discern how the drift coefficent would change over the course of iteratively progressing from n=1 to n=241

Where for example the random walk with drift model is Y[t] = c + Y[t-1] + Z[t] where Z[t] is a normal error and c is the coefficient i am looking for. My current attempts at this involve a for loop function and extracting the c coefficient from the rwf() function from the "Forecast" package in R.

To extract this, I am doing as such

rwf(x, h = 1, drift = TRUE)$model[[1]]

which extracts the drift coefficient. The problem is, my attempts at subsetting the data within the rwf call have failed, and I also don't believe, through trial and error and research, that rwf() supports the subset argument, as an lm model does for example. In this sense my attempts at looping the function have also failed.

An example of such code is

for (i in 1:5){print((rwf(x[1:i], h = 1, drift = TRUE))$model[[1]])}

which gives me the following error

Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
  0 (non-NA) cases
In addition: Warning message:
In is.na(rows) : is.na() applied to non-(list or vector) of type 'NULL'

Any help would be much appreciated. I read SO a lot for help but this is my first time asking a question.

The data is as follows

        PCE
1   1306.7
2   1309.6
3   1335.3
4   1341.8
5   1389.2
6   1405.7
7   1414.2
8   1411.0
9   1401.6
10  1406.7
11  1425.0
12  1444.4
13  1474.7
14  1507.8
15  1536.6
16  1555.6
17  1575.2
18  1577.8
19  1583.0
20  1586.6
21  1608.4
22  1619.5
23  1622.4
24  1635.3
25  1636.1
26  1613.9
27  1627.1
28  1653.8
29  1675.6
30  1706.7
31  1732.9
32  1751.0
33  1752.9
34  1769.7
35  1792.1
36  1785.0
37  1787.4
38  1786.9
39  1813.4
40  1822.2
41  1858.7
42  1878.5
43  1901.6
44  1917.0
45  1944.2
46  1957.3
47  1976.0
48  2002.9
49  2019.6
50  2059.5
51  2095.8
52  2134.3
53  2140.2
54  2187.8
55  2212.0
56  2250.0
57  2313.2
58  2347.4
59  2353.5
60  2380.4
61  2390.3
62  2404.2
63  2437.0
64  2449.5
65  2464.6
66  2523.4
67  2562.1
68  2610.3
69  2622.3
70  2651.7
71  2668.6
72  2681.5
73  2702.9
74  2719.5
75  2731.9
76  2755.9
77  2748.4
78  2800.9
79  2826.6
80  2849.1
81  2896.5
82  2935.2
83  2991.2
84  3037.4
85  3108.6
86  3165.5
87  3163.9
88  3175.3
89  3166.0
90  3138.3
91  3149.2
92  3162.2
93  3115.8
94  3142.0
95  3194.4
96  3239.9
97  3274.2
98  3339.6
99  3370.3
100 3405.9
101 3450.3
102 3489.7
103 3509.0
104 3542.5
105 3595.9
106 3616.9
107 3694.2
108 3709.7
109 3739.6
110 3758.5
111 3756.3
112 3793.2
113 3803.3
114 3796.7
115 3710.5
116 3750.3
117 3800.3
118 3821.1
119 3821.1
120 3836.6
121 3807.6
122 3832.2
123 3845.9
124 3875.4
125 3946.1
126 3984.8
127 4063.9
128 4135.7
129 4201.3
130 4237.3
131 4297.9
132 4331.1
133 4388.1
134 4462.5
135 4503.2
136 4588.7
137 4598.8
138 4637.2
139 4686.6
140 4768.5
141 4797.2
142 4789.9
143 4854.0
144 4908.2
145 4920.0
146 5002.2
147 5038.5
148 5078.3
149 5138.1
150 5156.9
151 5180.0
152 5233.7
153 5259.3
154 5300.9
155 5318.4
156 5338.6
157 5297.0
158 5282.0
159 5322.2
160 5342.6
161 5340.2
162 5432.0
163 5464.2
164 5524.6
165 5592.0
166 5614.7
167 5668.6
168 5730.1
169 5781.1
170 5845.5
171 5888.8
172 5936.0
173 5994.6
174 6001.6
175 6050.8
176 6104.9
177 6147.8
178 6204.0
179 6274.2
180 6311.8
181 6363.2
182 6427.3
183 6453.3
184 6563.0
185 6638.1
186 6704.1
187 6819.5
188 6909.9
189 7015.9
190 7085.1
191 7196.6
192 7283.1
193 7385.8
194 7497.8
195 7568.3
196 7642.4
197 7710.0
198 7740.8
199 7770.0
200 7804.2
201 7926.4
202 7953.7
203 7994.1
204 8048.3
205 8076.9
206 8117.7
207 8198.1
208 8308.5
209 8353.7
210 8427.6
211 8465.1
212 8539.1
213 8631.3
214 8700.1
215 8786.2
216 8852.9
217 8874.9
218 8965.8
219 9019.8
220 9073.9
221 9158.3
222 9209.2
223 9244.5
224 9285.2
225 9312.6
226 9289.1
227 9285.8
228 9196.0
229 9076.0
230 9040.9
231 8998.5
232 9050.3
233 9060.2
234 9121.2
235 9186.9
236 9247.1
237 9328.4
238 9376.7
239 9392.7
240 9433.5
241 9482.1

Solution

  • You need at least two points to fit your model. Here's how I'd approach the problem after reading your data into a data.frame named x:

    library(forecast)
    drifts <- sapply(2:nrow(x), function(zz) rwf(x[1:zz,], drift = TRUE)$model$drift)
    

    I'm not sure if this is what you were expecting or not, but here's a plot of your drift values: enter image description here