Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8921891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:40:43+00:00 2026-06-15T06:40:43+00:00

Using apply (or sapply) on an mts object removes its time series properties when

  • 0

Using apply (or sapply) on an mts object removes its time series properties when sending to function. How should I apply same function (with ts input and ts output) on each of times series in an mts object and return it (preferably as mts) [I mean besides using for loops]?

For example suppose I write a function that returns the trend of a time series (using stl)

myfunc <- function(x) {
      return(stl(x,"per")$time.series[,2])
}

Now for a sample mts

z <- ts(matrix(rnorm(90), 30, 3), start=c(1961, 1), frequency=4)
class(z)

Sending only one of the time series works correct:

myfunc(z[,1]) # works correctly, returns the trend of first series

My function is not designed for multiple time series so:

myfunc(z) # will not work returning the error below

Error in stl(x, "per") : only univariate series are allowed

Using apply on the mts object send each of the time series as a vector, not preserving its time series properties (tsp):

apply(z,2,myfunc) # will not work returning the error below

Error in stl(x, "per") : 
series is not periodic or has less than two periods
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T06:40:45+00:00Added an answer on June 15, 2026 at 6:40 am

    A simple way around this, is to work with the indices instead of a clean apply :

    sapply(seq_len(ncol(z)),function(i) myfunc(z[,i]))
    

    apply puts clean vectors inside the function, because it first converts an object to a matrix. By using the [ function defined for time series objects, you are sure that you extract a valid time series each time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to apply regression function progressively to a time series data (vector time
I recentely asked a question about using an apply function over two lists. Each
When using english the following URL should apply: http://example.org/services/myservice/ When using another language, the
I'm trying to invoke addEventListener() using apply() method. The code is like: function rewrite(old){
I am using by to apply a function to a range columns of a
Sometimes I see people using this to call function within a widget this.myFunction.apply(this, arguments);
I want to convert variables into factors using apply() : a <- data.frame(x1 =
I am using http://uniformjs.com for my html controls. We can apply this using code
I have a SqlDump.sql file that works just fine when I apply it using
Using Delphi XE 2 I've trying to identify the zoom direction to apply the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.