Anakin Skywalker
Anakin Skywalker

Reputation: 2520

Cbind() in R gives "mts" "ts" "matrix" object instead of "mts" "ts" object

I have two ts objects.

structure(c(5.92, 5.97, 5.92, 6.04, 6.32, 6.43, 6.48, 6.04, 6.2, 
6.09, 5.29, 5.05, 5.13, 5, 4.81, 4.86, 5.42, 5.22, 5.19, 5.06, 
4.95, 4.88, 4.93, 5.03, 4.99, 4.97, 5.1, 4.89, 4.74, 4.56, 4.43, 
4.35, 4.22, 4.3, 4.71, 4.76, 4.95, 4.84, 4.84, 4.64, 4.51, 4.54, 
4.27, 4.11, 4.07, 3.99, 3.96, 3.92, 3.89, 3.95, 3.91, 3.8, 3.67, 
3.55, 3.6, 3.5, 3.38, 3.35, 3.34, 3.41, 3.53, 3.56, 3.45, 3.54, 
4.07, 4.37, 4.46, 4.49, 4.19, 4.26, 4.46, 4.43, 4.3, 4.34, 4.34, 
4.19, 4.16, 4.13, 4.12, 4.16, 4.04, 4, 3.86, 3.67, 3.71, 3.77, 
3.67, 3.84, 3.98, 4.05, 3.91, 3.89, 3.8, 3.94, 3.96, 3.87, 3.66, 
3.69, 3.6, 3.6, 3.57, 3.44, 3.44, 3.46, 3.47, 3.77, 4.2, 4.15, 
4.17, 4.2, 4.04, 4.01, 3.9, 3.97, 3.88, 3.8, 3.9, 3.92, 3.95, 
4.03, 4.33, 4.44, 4.47, 4.59, 4.57, 4.53, 4.55, 4.63, 4.83, 4.87, 
4.64, 4.46, 4.37, 4.26, 4.14, 4.07, 3.8, 3.77, 3.62, 3.6, 3.69, 
3.7, 3.72, 3.62, 3.47, 3.45, 3.31, 3.23, 3.16, 3.02, 2.94), .Tsp = c(2008.08333333333, 
2020.58333333333, 12), class = "ts")

and

structure(c(250000, 246000, 249000, 250000, 255000, 258500, 255000, 
245000, 235000, 225000, 226000, 215000, 205000, 215000, 215000, 
218000, 225000, 220300, 220000, 211000, 202000, 200000, 200000, 
196000, 187000, 200000, 201000, 207500, 220000, 210000, 216600, 
200000, 199700, 190000, 190750, 180000, 178000, 171500, 185000, 
184000, 195000, 200000, 195000, 175000, 170000, 163000, 160000, 
153000, 150000, 165000, 175000, 180500, 198000, 195000, 186000, 
177000, 162000, 166000, 165000, 153000, 149000, 164000, 179000, 
192000, 213750, 213000, 208500, 197500, 185400, 182000, 180000, 
175000, 167000, 183000, 192500, 207500, 225000, 220000, 222500, 
206000, 192500, 190000, 190000, 182000, 179000, 199000, 212000, 
220000, 228500, 224900, 218000, 210000, 203000, 198000, 199000, 
190000, 185995, 206250, 223000, 227750, 232000, 230000, 223000, 
210000, 210000, 207500, 208000, 205500, 204000, 222500, 235000, 
235000, 245000, 240000, 229700, 222000, 218000, 215000, 219875, 
215000, 217500, 230000, 239000, 240000, 249900, 243000, 233000, 
229000, 222000, 223000, 220000, 216000, 222000, 232500, 240000, 
246000, 249500, 249000, 242000, 235000, 231000, 231250, 230000, 
223000, 225000, 248000, 260000, 251000, 254000, 262000, 270000
), .Tsp = c(2008.08333333333, 2020.58333333333, 12), class = "ts")

Both are imported as ts objects.

Median_price_ts <- ts(chicago$Median_price, start = c(2008, 2), end = c(2020, 8), frequency = 12)
Average_rate_ts <- ts(mortgage_data_monthly$Average_rate, start = c(2008, 2), end = c(2020, 8), frequency = 12)

When I used cbind() to combine them, it gives me the following:

class(Median_price_ts)
class(Average_rate_ts)
foo <- cbind(Median_price_ts, Average_rate_ts) 
class(foo)

I get:

[1] "ts"
[1] "ts"
[1] "mts"    "ts"     "matrix"

and instead I just want

"mts"    "ts"

Why is this important? I am building a VAR model and apparently my data must be only in "mts" "ts" format for forecasting.

VAR works well with dataframe and "mts" "ts" "matrix".

This tutorial does cbind() and receives only "mts" "ts". I also worked with other such datasets before and abled to do forecasting with just "mts" "ts", but not "mts" "ts" "matrix".

Appreciate tips!

Upvotes: 0

Views: 770

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388982

You can force a class on foo :

class(foo) <- c('mts', 'ts')
class(foo)
#[1] "mts" "ts" 

foo
#         Median_price_ts Average_rate_ts
#Feb 2008            5.92          250000
#Mar 2008            5.97          246000
#Apr 2008            5.92          249000
#May 2008            6.04          250000
#Jun 2008            6.32          255000
#Jul 2008            6.43          258500
#Aug 2008            6.48          255000
#Sep 2008            6.04          245000
#Oct 2008            6.20          235000
#...
#...

Upvotes: 1

Related Questions