data converted into time series format but not able to perform analysis

the data frame gdp should be in time series for running BBQ function of BCDating package. the class is showing ts but i am geting this error.
Please help.
Thanks in advance

code

df<- read.csv("gdp_us.csv")
tseries <- read.zoo(df)
tseries_ts <- as.ts(tseries)
gdp <- na.remove(tseries_ts)
dat <- BBQ(gdp, name="Dating Business Cycles of USA")
show(dat)
summary(dat)
plot(dat)
plot(dat,gdp_ts)

output

> tseries <- read.zoo(df)
> tseries_ts <- as.ts(tseries)
> gdp <- na.remove(tseries_ts)
> dat <- BBQ(gdp, name="Dating Business Cycles of USA")
Error in BCDating.peakstroughs(start = start(y), end = end(y), freq = frequency(y),  : 
  frequency must be 12 (monthly dates) or 4 (quarterly dates)
> class(gdp)
[1] "ts"

sample data

|date|gdp|
|---|---|
|01-01-1947|1.24|
|01-04-1947|1.14|
|01-07-1947|1.45|
|01-10-1947|3.91|
|01-01-1948|2.26|
|01-04-1948|2.5|
|01-07-1948|2.37|
|01-10-1948|0.42|
|01-01-1949|-1.94|
|01-04-1949|-1.36|
|01-07-1949|0.56|
|01-10-1949|-0.84|
|01-01-1950|3.63|
|01-04-1950|3.29|
|01-07-1950|5.77|
|01-10-1950|3.69|
|01-01-1951|4.78|
|01-04-1951|2.35|
|01-07-1951|2.08|
|01-10-1951|1.35|
|01-01-1952|1.01|
|01-04-1952|0.34|
|01-07-1952|1.81|
|01-10-1952|3.44|
|01-01-1953|1.85|
|01-04-1953|0.96|
|01-07-1953|-0.15|
|01-10-1953|-1.35|
|01-01-1954|-0.16|
|01-04-1954|0.2|
|01-07-1954|1.25|
|01-10-1954|2.19|
|01-01-1955|3.23|
|01-04-1955|2.01|
|01-07-1955|2.02|
|01-10-1955|1.57|
|01-01-1956|0.6|
|01-04-1956|1.4|
|01-07-1956|1.15|
|01-10-1956|2.01|
|01-01-1957|1.98|
|01-04-1957|0.48|
|01-07-1957|1.56|
|01-10-1957|-0.97|
|01-01-1958|-1.57|
|01-04-1958|0.94|
|01-07-1958|2.85|
|01-10-1958|2.75|
|01-01-1959|2.11|
|01-04-1959|2.36|
|01-07-1959|0.45|
|01-10-1959|0.67|
|01-01-1960|2.59|
|01-04-1960|-0.29|
|01-07-1960|0.83|
|01-10-1960|-1|
|01-01-1961|0.88|

The error message says it all& dash;dail or weekly data is not acceptable to the BBQ() function. To proceed re-think about what output you are trying to derive from the input or refactor the input to aggregate. If the later use ts() and work from there.

dat <- BBQ(gdp_ts, name="Dating Business Cycles of USA")
show(dat)
Peaks Troughs Duration
1 1948Q4 1949Q4 4
2 1953Q2 1954Q1 3
3 1957Q3 1958Q1 2
4 2008Q3 2009Q2 3
5 2019Q4 2020Q2 2
summary(dat)
Phase ]Start ;End] Duration LevStart LevEnd Amplitude
1 Expansion 1948Q4 NA NA 280 NA
2 Recession 1948Q4 1949Q4 4 280 271 9.7
3 Expansion 1949Q4 1953Q2 14 271 392 121.1
4 Recession 1953Q2 1954Q1 3 392 385 6.4
5 Expansion 1954Q1 1957Q3 14 385 479 94.1
6 Recession 1957Q3 1958Q1 2 479 468 11.9
7 Expansion 1958Q1 2008Q3 202 468 14899 14431.5
8 Recession 2008Q3 2009Q2 3 14899 14381 517.8
9 Expansion 2009Q2 2019Q4 42 14381 21694 7313.2
10 Recession 2019Q4 2020Q2 2 21694 19477 2217.0
11 Expansion 2020Q2 NA 19477 NA NA

      Amplitude Duration

Exp=]T;P] 5490.0 68.0
Rec=]P;T] 552.6 2.8

> dat <- BBQ(gdp_ts, name="Dating Business Cycles of USA")
> show(dat)
   Peaks Troughs Duration
1 1948Q4  1949Q4        4
2 1953Q2  1954Q1        3
3 1957Q3  1958Q1        2
4 2008Q3  2009Q2        3
5 2019Q4  2020Q2        2
> summary(dat)
       Phase ]Start  ;End] Duration LevStart LevEnd Amplitude
1  Expansion   <NA> 1948Q4       NA       NA    280        NA
2  Recession 1948Q4 1949Q4        4      280    271       9.7
3  Expansion 1949Q4 1953Q2       14      271    392     121.1
4  Recession 1953Q2 1954Q1        3      392    385       6.4
5  Expansion 1954Q1 1957Q3       14      385    479      94.1
6  Recession 1957Q3 1958Q1        2      479    468      11.9
7  Expansion 1958Q1 2008Q3      202      468  14899   14431.5
8  Recession 2008Q3 2009Q2        3    14899  14381     517.8
9  Expansion 2009Q2 2019Q4       42    14381  21694    7313.2
10 Recession 2019Q4 2020Q2        2    21694  19477    2217.0
11 Expansion 2020Q2   <NA>       NA    19477     NA        NA

          Amplitude Duration
Exp=]T;P]    5490.0     68.0
Rec=]P;T]     552.6      2.8

by

d <- data.frame(date = c(
  "01-01-1947", "01-04-1947", "01-07-1947",
  "01-10-1947", "01-01-1948", "01-04-1948", "01-07-1948", "01-10-1948",
  "01-01-1949", "01-04-1949", "01-07-1949", "01-10-1949", "01-01-1950",
  "01-04-1950", "01-07-1950", "01-10-1950", "01-01-1951", "01-04-1951",
  "01-07-1951", "01-10-1951", "01-01-1952", "01-04-1952", "01-07-1952",
  "01-10-1952", "01-01-1953", "01-04-1953", "01-07-1953", "01-10-1953",
  "01-01-1954", "01-04-1954", "01-07-1954", "01-10-1954", "01-01-1955",
  "01-04-1955", "01-07-1955", "01-10-1955", "01-01-1956", "01-04-1956",
  "01-07-1956", "01-10-1956", "01-01-1957", "01-04-1957", "01-07-1957",
  "01-10-1957", "01-01-1958", "01-04-1958", "01-07-1958", "01-10-1958",
  "01-01-1959", "01-04-1959", "01-07-1959", "01-10-1959", "01-01-1960",
  "01-04-1960", "01-07-1960", "01-10-1960", "01-01-1961"
), gdp = c(
  1.24,
  1.14, 1.45, 3.91, 2.26, 2.5, 2.37, 0.42, -1.94,
  -1.36, 0.56, -0.84, 3.63, 3.29, 5.77, 3.69, 4.78,
  2.35, 2.08, 1.35, 1.01, 0.34, 1.81, 3.44, 1.85,
  0.96, -0.15, -1.35, -0.16, 0.2, 1.25, 2.19, 3.23,
  2.01, 2.02, 1.57, 0.6, 1.4, 1.15, 2.01, 1.98,
  0.48, 1.56, -0.97, -1.57, 0.94, 2.85, 2.75, 2.11,
  2.36, 0.45, 0.67, 2.59, -0.29, 0.83, -1, 0.88
))


ind <- c(rep(4, floor(dim(d)[1]/4)), floor(dim(d)[1]%%4))
ind <- rep(1:length(ind), times = ind)

head(apply(d[2], 2, function(x) tapply(x, ind, sum))) |>
  ts(start = c(1947,1), frequency =  4) 
#>       Qtr1  Qtr2  Qtr3  Qtr4
#> 1947  7.74  7.55 -3.58 16.38
#> 1948 10.56  6.60

Created on 2023-02-19 with reprex v2.0.2

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.