I'm trying to forecasts time-series using a VECM with one endogenous and one exogenous variable (ideally both would be ex. but this doesn't seem to be possible), but keep running into errors. I've got 169 time-series and I'm trying to use the first 140 weeks to predict the last 29. See my code below:
#VECM with exogenous F
#read F and W matrix
Ffactor <- read_excel("FFactor.xlsx", col_names = FALSE)
Wfactor <- read_excel("Wmatrix.xlsx", col_names = FALSE)
Sales<-read_excel("SALES.xlsx")
Wfactor$mean <- rowMeans(Wfactor)
KC_model <- cbind(Sales$UnitSalesKC[1:169], Wfactor$mean)
F140 <- as.data.frame(Ffactor[1:140,])
for (i in 1:1000)
{
colnames(F140)[i]<-"f"
}
KC140 <- as.data.frame(KC_model[1:140,])
vecm <- ca.jo(KC140,type = "eigen", ecdet = "const", K = 2, dumvar = F140[1:140,1])
varr <- vec2var(vecm, r=1)#should be 2 because of 2nd order cointegration but it had to be 1
dumvar <- Ffactor[141:169,]
dumvar1 <- as.matrix(dumvar)
predictions <- predict(varr, dumvar=dumvar1[1:29], n.ahead=29)
predictions <- predict(varr, dumvar=dumvar1[1:29], n.ahead=29)
Running the first predict gives: Error in predict.vec2var(varr, dumvar = dumvar[1:29, 1], n.ahead = 29) :
Column names of 'dumvar' do not match with column names in 'object$datamat'.
Running the second one gives: Error in predict.vec2var(varr, dumvar = dumvar1[1:29, 1], n.ahead = 29) :
Number of rows of 'dumvar' is not equal to 'n.ahead'.
However, calling nrow(dumvar) yields 29.
I've previously gotten error telling me dumvar should be a matrix, numeric or vector which is why I converted it into a matrix. Here's a sample of all the data needed to run the above code.
#head of KC_model
1 125489.0 -0.427423755
2 115475.0 -0.590675518
3 124298.0 -0.561800218
4 128443.0 -0.785542240
5 120610.0 -0.750909402
#head of FFactor
1 -0.251
2 -0.0169
3 -0.0434
4 -0.0643
5 0.0215
Try this:
predictions <- predict(varr, dumvar=as.matrix.data.frame(F140[1:140,1]), n.ahead=29)
It worked for me.
Related
I'm new to programming, and I don't understand why this code returns the binary number of the input.
x = int(input())
while x > 0:
print(x % 2, end='')
x = x // 2
For example, when I enter 6, I expect it to return 0 (6 % 2 = 0, 0 // 2 = 0), but it returns 011.
I stated contemplating this because, in an assignment, we're supposed to use this coding to help us reverse the output (while using a string) but I don't know how to reverse it when I don't know why it works.
The answer of 011 is correct for your code, just trace it thru. You start with 6%2 so you get a 0. Then 6 floor 2 is 3, which is then the x for the next iteration so it becomes 3%2 which is 1 and the final iteration creates the last 1. Remember that you're not changing the value of x in the print statement, it only changes in the last line.
What I have:
series = ['foo', 'bar', 'baz', 'foo', 'baz', 'foo' ]
column = [1, 2, -3, -4, 5, -6]
list = [column[function(x)].count() for x in series]
list:
foo = 3
bar = 1
baz = 2
Works fine, each instance in series is counted.
Want only positive number instances counted as well, so:
list = [column[function(x)].count() for x in series if (x := function(x)) >= 0]
list:
foo = 1
bar = 1
baz = 1
Discovered Walrus Operator, but x in my case is a string, perhaps the core problem?
I do get a syntax error with Walrus portion of code.
I need both total & positive number counts, creating say a "total" & "positive totals" columns in function seems clunky, is there a way to do this with list comprehension.
Thank you in advance for your assistance.
Since you tagged pandas:
pd.Series(column).gt(0).groupby(series).agg({'count','sum'})
Output:
count sum
bar 1 1
baz 2 1
foo 3 1
You are calling function(x) where x is the result of function(x) already. Try:
vals = [column[y].count() for x in series if (y := function(x)) >= 0]
Notes:
Use a different variable name than x so that it is less confusing (this is probably also the source of your syntax error).
list is a type name, choose a different name for the list of values.
This question already has answers here:
How to get the cartesian product of multiple lists
(17 answers)
Closed 2 years ago.
I have the following two lists:
x = [1,2]
y = [4,5,6]
I want to iterate x by z.
I have a variable called code set to NONE and another variable called value also set to NONE. Here is the output I am aiming for:
1st iteration, code = 1 and value = 4
2nd iteration, code = 1 and value = 5
3rd iteration, code = 1 and value = 6
4th iteration, code = 2 and value = 4
5th iteration, code = 2 and value = 5
6th iteration, code = 2 and value = 6
Here is what I have tried:
x = [1, 2]
y = [4, 5, 6]
code = None
value = None
for x_ids, y_ids in zip(x, y):
code = x_ids
value = y_ids
print("c", code)
print("v", value)
output:
c 1
v 4
c 2
v 5
Can anyone suggest how to get the output described above?
This is one way to achieve what you're looking for:
x = [1, 2]
y = [4, 5, 6]
code = None
value = None
iter_count = 0
for x_ids in x:
code = x_ids
for y_ids in y:
iter_count += 1
value = y_ids
print('{} iteration, code = {} and value = {}'.format(iter_count, code, value))
#print(str(iter_count) + ' iteration, code = ' + str(code) + 'and value = ' + str(value))
Like discussed in the comments, this code iterates through all elements of y for every element in x. In your original code, you were iterating through both lists all at ones, using zip. Since you want to print the number of iteration too, there is a new variable, iter_count, that counts and stores those.
The code has two print statements, they print the same messages. The commented out one concatenates strings, and numbers converted to strings. The uncommented one may be less intuitive but it is often more useful and cleaner. It's worth looking at it, you can find an introduction here.
Last thing, if you need that too - to print numbers in 1st, 2nd etc. format you can use some of these approaches.
I am trying to convert a list of integers in Python into a single integer say for example [1,2,3,4] to 1234(integer). In my function, I am using following piece of code:
L = [1,2,3,4]
b = int(''.join(map(str, L)))
return b
The compiler throws a ValueError. Why so? How to rectify this issue?
You can do this like this also if that cause problems:
L = [1,2,3,4]
maxR = len(L) -1
res = 0
for n in L:
res += n * 10 ** maxR
maxR -= 1
print(res)
1234
another solution would be
L = [1,2,3,4]
digitsCounter = 1
def digits(num):
global digitsCounter
num *= digitsCounter
digitsCounter *= 10
return num
sum(map(digits, L[::-1]))
the digits() is a non pure function that takes a number and places it on place value depending on the iteration calling digits on each iteration
1. digits(4) = 4 1st iteration
2. digits(4) = 40 2nd iteration
3. digits(4) = 400 3rd iteration
when we sum up the array returned by map from the inverted list L[::-1] we get 1234 since every digit in the array is hoisted to it place value
if we choose not no invert L array to L[::-1] then we would need our digits function to do more to figure out the place value of each number in the list so we use this to take adv of language features
I am trying to write a cell array to an Excel spreadsheet in Octave using the xlswrite from the io package in Octave (3.8.0, io 2.0.2 loaded, using Windows 7 64 bit).
The cell array looks like this:
>> pump_backlash(1:3,:)
ans =
{
[1,1] = Machine #
[2,1] = Machine_01
[3,1] = Machine_02
[1,2] = Station #
[2,2] = 1
[3,2] = 1
[1,3] = Pump channel #
[2,3] = 1
[3,3] = 2
[1,4] = Backlash
[2,4] =
57 65 62
[3,4] =
58 49 50
}
Except it's got many more rows. The first row consists of "headings" (strings), and then after that the first column is a string relating to the machine ID, the second and third columns are integers (scalars), and the fourth column of the cell array are 1x3 vectors of integers (although cells in the 4th column are sometimes empty if the test/measurement failed for whatever reason).
I try to write to Excel using the following command:
>> xlswrite('Pump_cal_results.xlsx',pump_backlash)
and the error message I get is as follows:
Creating file Pump_cal_results.xlsx
error: cellfun: all values must be scalars when UniformOutput = true
error: called from:
error: C:\Octave\Octave-3.8.0\share\octave\packages\io-2.0.2\private\spsh_prstype.m at line 62, column 6
error: C:\Octave\Octave-3.8.0\share\octave\packages\io-2.0.2\private\__COM_oct2spsh__.m at line 108, column 10
error: C:\Octave\Octave-3.8.0\share\octave\packages\io-2.0.2\oct2xls.m at line 189, column 18
error: C:\Octave\Octave-3.8.0\share\octave\packages\io-2.0.2\xlswrite.m at line 178, column 20
If I follow the error trail and go to line 62 of \private\spsh_prstype.m, I have:
ptr = cellfun ("isnan", obj); ## Find NaNs & set to BLANK
So it's obviously got something to do with that function call to cellfun, but I am not sure where to go from there. There are quite a few other function calls to cellfun in spsh_prstype.m.
The closest I have found by searching on the internet is this question, but there is no solution offered.
Any help/suggestions welcome.
Not having received any answers, I'll answer my own question :-)
I haven't worked out what the root cause of the problem and how to fix it, but I have found a workaround. It seems that the problems lies with the fact that the elements in the 4th column are vectors rather than scalars. It seems that to write to Excel, all elements in the cell array must be "uniform", which I take it to mean you can't mix scalars and vectors for example.
So my workaround was to re-arrange the cell array so that it now looks like:
>> pump_backlash(1:3,:)
ans =
{
[1,1] = Machine #
[2,1] = Machine_01
[3,1] = Machine_02
[1,2] = Station #
[2,2] = 1
[3,2] = 1
[1,3] = Pump channel #
[2,3] = 1
[3,3] = 2
[1,4] = Backlash #1
[2,4] = 57
[3,4] = 58
[1,5] = Backlash #2
[2,5] = 65
[3,5] = 49
[1,6] = Backlash #3
[2,6] = 62
[3,6] = 50
}
i.e. the cell array now has 6 columns instead of 4, and there's no more vectors, only scalars.
The call to xlswrite then works OK:
xlswrite('Pump_cal_results.xlsx',pump_backlash,'Backlash','','com');