0

I have a xts object, S1, with NA values. My goal is to first convert S1 into a zoo object then to impute the NA values using the na.StrucTS function from the zoo package. However, when attempting this, I run into the following error:

> A_na = na.StructTS(A) Error in rowSums(tsSmooth(StructTS(y))[, -2]) : 'x' must be an array of at least two dimensions

The structure of the variables I am working with are provided below. If any further information is needed please do let me know and I will provide further details. Thanks in advance for any help!

Details:

> require(xts)
> require(zoo)
> dput(S1)
structure(c(3.59, 3.36, 3.21, NA, NA, NA, NA, NA, NA, 3.2, 5.31, 
6.73), class = c("xts", "zoo"), .indexCLASS = c("POSIXlt", "POSIXt"
), .indexTZ = "GMT", .CLASS = "double", tclass = c("POSIXlt", 
"POSIXt"), tzone = "GMT", index = structure(c(1418698800, 1418702400, 
1418706000, 1418709600, 1418713200, 1418716800, 1418720400, 1418724000, 
1418727600, 1418731200, 1418734800, 1418738400), tzone = "GMT", tclass = c("POSIXlt", 
"POSIXt")), .Dim = c(12L, 1L), .Dimnames = list(NULL, "dfxts"))
> A = as.zoo(S1)
> dput(A)
structure(c(3.59, 3.36, 3.21, NA, NA, NA, NA, NA, NA, 3.2, 5.31, 
6.73), .Dim = c(12L, 1L), .Dimnames = list(NULL, "dfxts"), index = structure(list(
    sec = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), min = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), hour = 3:14, 
    mday = c(16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 
    16L, 16L), mon = c(11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
    11L, 11L, 11L, 11L), year = c(114L, 114L, 114L, 114L, 114L, 
    114L, 114L, 114L, 114L, 114L, 114L, 114L), wday = c(2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), yday = c(349L, 349L, 
    349L, 349L, 349L, 349L, 349L, 349L, 349L, 349L, 349L, 349L
    ), isdst = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L)), .Names = c("sec", "min", "hour", "mday", "mon", "year", 
"wday", "yday", "isdst"), class = c("POSIXlt", "POSIXt"), tzone = "GMT"), class = "zoo")
> A_na = na.StructTS(S1)
Error in rowSums(tsSmooth(StructTS(y))[, -2]) : 
  'x' must be an array of at least two dimensions

However, if I run fitted(StructTS(A)) I achieve the following output:

> fitted(StructTS(A))
Time Series:
Start = 1418698800 
End = 1418738400 
Frequency = 0.000277777777777778 
              level      slope
1418698800 3.590000  0.0000000
1418702400 3.360000 -0.1887519
1418706000 3.210000 -0.1563045
1418709600 3.053695 -0.1563045
1418713200 2.897391 -0.1563045
1418716800 2.741086 -0.1563045
1418720400 2.584782 -0.1563045
1418724000 2.428477 -0.1563045
1418727600 2.272173 -0.1563045
1418731200 3.200000  0.0543115
1418734800 5.310000  1.9354185
1418738400 6.730000  1.5025847
  • 1
    Please provide a minimal, complete, and reproducible example that anyone else can simply copy and paste into their R session to run. All library statements and inputs need to be included. Cut down your data to the minimum needed to illustrate the problem if it's large and if your input is `x` then show it by displaying the output of `dput(x)` in your question. See [mcve] for general advice and see [How to Make a Great R Reproducible Example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for more R oriented advice on how to ask a question on SO. – G. Grothendieck Feb 22 '16 at 21:28
  • Thank you @G.Grothendieck for the helpful links. I hope my initial question has been suitably modified to allow others to troubleshoot. If anything else is needed please let me know! – watINwatOUT Feb 22 '16 at 22:06
  • na.StructTS must satisfy the requirements of StructTS which requires that a complete cycle is represented by a time of 1. See the examples in ?StructTS. Maybe you can use na.approx or na.spline instead. – G. Grothendieck Feb 22 '16 at 22:15
  • Thanks for the reply @G.Grothendieck. I am not sure if I understand your comment regarding "...StructTS which requires that a complete cycle is represented by a time of 1.". I am able to apply `StructTS(A)` with no error. Running `fitted(StructTS(A))` provides output (please see the edits I made to the question above). Is there perhaps another source of error? – watINwatOUT Feb 22 '16 at 22:45
  • That's because StructTS does not know about zoo so it's ignoring the times. – G. Grothendieck Feb 22 '16 at 22:55
  • Is it possible to coerce the frequency to 1, apply `na.StructTS`, and then convert back to its native frequency? – watINwatOUT Feb 22 '16 at 22:55
  • This gives output but may not make any sense: `a <- coredata(A); zr <- zooreg(c(a, a), freq =3); na.StructTS(zr)` . We have made the data larger since you don't have enough for it to use with just A. – G. Grothendieck Feb 22 '16 at 23:07
  • Thanks for the solution @G.Grothendieck - I'll play around with this and see if I can apply it to a larger dataset. Cheers – watINwatOUT Feb 23 '16 at 00:09

0 Answers0