zoo

Calculating reinvestment of dividends with a cumulative approach

隐身守侯 提交于 2019-12-24 06:43:00
问题 This is what my data.table looks like. The A:E columns are just to draw comparison to excel. Column NewShares is my desired column. I DO NOT have that column in my data. A B C D E F dt<-fread(' InitialShares Level Price Amount CashPerShare NewShares 1573.333 0 9.5339 13973.71 0 1573.333 0 1 10.2595 0 .06689 1584.73 0 1 10.1575 0 .06689 1596.33 0 1 9.6855 0 .06689 1608.58') I am trying to calculate NewShares with the assumption that new shares are added to InitialShares by reinvesting

interpolating in R yearly time series data with quarterly values

荒凉一梦 提交于 2019-12-24 05:19:07
问题 I have a data set that has a list of IDs, year, and income. I am trying to interpolate the yearly values to quarterly values. id = c(2, 2, 2, 3, 3, 3,4,4,4,5,5) year = c(2000, 2001, 2002, 2000,2001,2002, 2000,2001,2002,2000,2002) income = c(20, 24, 26, 30,34,36, 40,46,48,53,56) df = data.frame(id, year, income) For e.g., I am looking to get the values of (interpolated) income for year-quarter 2000Q1, 2000Q2, 2000Q3, 2000Q4, 2001Q1, ... , 2001Q4. So the dataframe would be id,year-quarter,

VaR calculation with complete missing column

我只是一个虾纸丫 提交于 2019-12-24 04:34:03
问题 I need to calculate rolling VaR of stock returns. From this post: Using rollapply function for VaR calculation using R , I understand that columns having complete missing cases will give error. But since the starting date and end date of stock returns for various firms are different, it creates missing values when data is converted from long to wide format. Estimation can be done using only rows with no missing values but this leads to serious loss of data. Thus, is there any way to perform

Using Rollapply on two columns

痴心易碎 提交于 2019-12-24 03:08:40
问题 I'm trying to do something similar I was asking for here and unfortunately I cannot work it out. This is my data frame (data), a time series of prices: Date Price Vol 1998-01-01 200 0.3 1998-01-02 400 0.4 1998-01-03 600 -0.2 1998-01-04 100 0.1 ... 1998-01-20 100 0.1 1998-01-21 200 -0.4 1998-01-21 500 0.06 .... 1998-02-01 100 0.2 1998-02-02 200 0.4 1998-02-03 500 0.3 1998-02-04 100 0.1 etc. I would like to tell R, to take the 1st value of "Vol" and divide it by the 20th value of "Price", then

Using Rollapply on two columns

十年热恋 提交于 2019-12-24 03:08:21
问题 I'm trying to do something similar I was asking for here and unfortunately I cannot work it out. This is my data frame (data), a time series of prices: Date Price Vol 1998-01-01 200 0.3 1998-01-02 400 0.4 1998-01-03 600 -0.2 1998-01-04 100 0.1 ... 1998-01-20 100 0.1 1998-01-21 200 -0.4 1998-01-21 500 0.06 .... 1998-02-01 100 0.2 1998-02-02 200 0.4 1998-02-03 500 0.3 1998-02-04 100 0.1 etc. I would like to tell R, to take the 1st value of "Vol" and divide it by the 20th value of "Price", then

Rolling regression on irregular time series

混江龙づ霸主 提交于 2019-12-24 01:23:57
问题 Summary (tldr) I need to perform a rolling regression on an irregular time series (i.e. the interval may not even be periodic and go from 0, 1, 2, 3... to ...7, 20, 24, 28... ) that's simple numeric and does not necessarily require date/time, but the rolling window needs be by time. So if I have a timeseries that is irregularly sampled for 600 seconds and the window is 30, the regression is performed every 30 seconds, and not every 30 samples. I've read examples, and while I could replicate

R Impute NA's by Linear Increase Depending on Time Interval

北慕城南 提交于 2019-12-23 22:24:20
问题 PROBLEM I neeed to impute the NA's in my data frame that comes from a repeated measures study. On this particular outcome, I need to impute the NA's with the last observed non-NA value +1 by each +52 week interval starting from the last observed value. EXAMPLE An example data frame with the target imputation goal included. df <- data.frame( subject = rep(1:3, each = 12), week = rep(c(8, 10, 12, 16, 20, 26, 32, 44, 52, 64, 78, 104),3), value = c(112, 97, 130, 104, NA, NA, NA, NA, NA, NA, NA,

How to calculate the average slope within a moving window in R

允我心安 提交于 2019-12-23 16:41:26
问题 My dataset contains 2 variables y and t [05s] . y was measured every 05 seconds. I am trying to calculate the average slope within a moving 20-second-window , i.e. after calculating the first 20-second slope value the window moves forward one time unit (05 seconds) and calculates the next 20-second-window, producing successive 20-second slope values at 05-second increments . I thought that calculating a rolling regression with rollapply (zoo package) would do the trick, but I get the same

Carry Last Observation Forward by ID in R

≡放荡痞女 提交于 2019-12-23 12:20:04
问题 I have daily observations with lots of missing values and am trying to propagate the first non-missing value through a vector for each individual. In the searching that I have done so far, I discovered the na.locf function in the zoo package; however, I now need to condition this function based on the id variable in my data frame. Is ddply the right function for this? If so, can someone help me please figure out how to get the output to be included in a new variable called result in the same

How can I alter a time series (XTS or ZOO) in R?

时光总嘲笑我的痴心妄想 提交于 2019-12-23 07:28:33
问题 I am new to stackoverflow and fairly new to R but have searched long and hard and cannot find an answer to the following question. I have a number of data files that are temperature against a time series. I am importing the CSV as a ZOO object then converting to XTS. A correct file looks like this, with readings on the hour and the half hour: >head(master1) S_1 2010-03-03 00:00:00 2.8520 2010-03-03 00:30:00 2.6945 2010-03-03 01:00:00 2.5685 2010-03-03 01:30:00 2.3800 2010-03-03 02:00:00 2