Skip to contents

Apply a function periodically

Usage

pcApply(object, ...)

# S4 method for numeric
pcApply(object, nseasons, FUN, ...)

# S4 method for matrix
pcApply(object, nseasons, FUN, ...)

# S4 method for PeriodicTS
pcApply(object, FUN, ...)

# S4 method for PeriodicMTS
pcApply(object, FUN, ...)

Arguments

object

an object for which periodic mean makes sense.

nseasons

number of seasons.

FUN

a function, as for apply.

...

further arguments for FUN.

Details

For univariate periodic time series, pcApply applies FUN to the data for each season. For multivariate periodic time series, this is done for each variable.

The methods for "numeric" and "matrix" are equivalent to those for "PeriodicTS" and "PeriodicMTS", respectively. The difference is that the latter two don't need argument nseasons and take the names of the seasons from object.

Argument "..." is for further arguments to FUN. In particular, with many standard R functions argument na.rm = TRUE can be used to omit NA's, see the examples.

In the univariate case, when length(object) is an integer multiple of the number of seasons the periodic mean is equivalent to apply(matrix(object, nrow = nseasons), 1, FUN, ...).

Value

numeric or matrix for the methods described here, see section Details.

Author

Georgi N. Boshnakov

Methods

signature(object = "matrix")

signature(object = "numeric")

signature(object = "PeriodicMTS")

signature(object = "PeriodicTS")

See also

Examples

pcApply(pcts(presidents), mean, na.rm = TRUE)
#> Quarter_1 Quarter_2 Quarter_3 Quarter_4 
#>  58.44828  56.43333  57.22222  53.07143 
pcMean(pcts(presidents), na.rm = TRUE) # same
#> Quarter_1 Quarter_2 Quarter_3 Quarter_4 
#>  58.44828  56.43333  57.22222  53.07143 

pcApply(pcts(presidents), median, na.rm = TRUE)
#> Quarter_1 Quarter_2 Quarter_3 Quarter_4 
#>      63.0      60.5      61.0      55.5 
pcApply(pcts(presidents), var, na.rm = TRUE)
#> Quarter_1 Quarter_2 Quarter_3 Quarter_4 
#>  274.5419  239.7713  218.2564  251.6243 
pcApply(pcts(presidents), sd, na.rm = TRUE)
#> Quarter_1 Quarter_2 Quarter_3 Quarter_4 
#>  16.56931  15.48455  14.77350  15.86267 

pcfr2to4 <- pcts(dataFranses1996)[2:4]
pcApply(pcfr2to4, median, na.rm = TRUE)
#>           USTotalIPI CanadaUnemployment GermanyGNP
#> Quarter_1      74.60              652.0      309.4
#> Quarter_2      76.80              542.5      314.5
#> Quarter_3      76.65              515.0      328.7
#> Quarter_4      76.75              528.5      329.5
pcApply(pcfr2to4, sd, na.rm = TRUE)
#>           USTotalIPI CanadaUnemployment GermanyGNP
#> Quarter_1   22.65126           387.2952   79.87538
#> Quarter_2   22.65155           388.1247   78.92102
#> Quarter_3   23.81635           384.9629   75.67722
#> Quarter_4   22.80446           370.5678   83.80192