Home > Enterprise >  I have a time series dataframe with monthly stock returns, and i want to extract and create a vector
I have a time series dataframe with monthly stock returns, and i want to extract and create a vector

Time:02-07

I have a time series dataframe with monthly stock returns, and I want to extract and create a vector for each months returns.

This is how the dataframe looks. (the MKT vector contains the monthly returns)

enter image description here

This is what I would like to accomplish (In the end) : enter image description here

By running the dput:

structure(c(0.0286195, 0.03618317, -0.01363269, 0.02977401, 0.04461314, 0.0015209, -0.03207303, -0.0079275, 0.01882991, 0.00584478, 0.02372219, 0.03299206, -0.017908, 0.02540426, 0.04163062, -0.00317315, -0.03732322, -0.0109474, 0.0147047, 0.00087712, 0.00608527826274047, 0.00495046849033236, 0.00503506482970477, 0.00481634688889247, 0.00424210936461577, 0.00358500724272255, 0.00424210936461577, 0.00480928182207086, 0.00485872460615713, 0.00487990531586144, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0), .Dim = c(10L, 6L), .Dimnames = list(NULL, c("MKT", "CAP", "RF", "dummy", "dummyJAN", "adjdummy")), index = structure(c(-36494, -36466, -36435, -36405, -36374, -36344, -36313, -36282, -36252, -36221), class = "Date"), class = "zoo")

CodePudding user response:

Please try this:

df=structure(c(0.0286195, 0.03618317, -0.01363269, 0.02977401, 0.04461314, 0.0015209, -0.03207303, -0.0079275, 0.01882991, 0.00584478, 0.02372219, 0.03299206, -0.017908, 0.02540426, 0.04163062, -0.00317315, -0.03732322, -0.0109474, 0.0147047, 0.00087712, 0.00608527826274047, 0.00495046849033236, 0.00503506482970477, 0.00481634688889247, 0.00424210936461577, 0.00358500724272255, 0.00424210936461577, 0.00480928182207086, 0.00485872460615713, 0.00487990531586144, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0), .Dim = c(10L, 6L), .Dimnames = list(NULL, c("MKT", "CAP", "RF", "dummy", "dummyJAN", "adjdummy")), index = structure(c(-36494, -36466, -36435, -36405, -36374, -36344, -36313, -36282, -36252, -36221), class = "Date"), class = "zoo") 

library(zoo)
library(foqat) 
df2=fortify.zoo(df)
df3=svri(df2, bkip = "1 day", mode = "recipes", value = "year")
df3
#   month of year         MKT         CAP          RF dummy dummyJAN adjdummy
#1              1  0.02861950  0.02372219 0.006085278     1        1        0
#2              2  0.03618317  0.03299206 0.004950468     1        0        1
#3              3 -0.01363269 -0.01790800 0.005035065     1        0        1
#4              4  0.02977401  0.02540426 0.004816347     1        0        1
#5              5  0.04461314  0.04163062 0.004242109     0        0        0
#6              6  0.00152090 -0.00317315 0.003585007     0        0        0
#7              7 -0.03207303 -0.03732322 0.004242109     0        0        0
#8              8 -0.00792750 -0.01094740 0.004809282     0        0        0
#9              9  0.01882991  0.01470470 0.004858725     0        0        0
#10            10  0.00584478  0.00087712 0.004879905     0        0        0
  •  Tags:  
  • Related