zoo

Replacing all NAs with smoothing spline

拟墨画扇 提交于 2019-11-30 17:10:12
问题 Below is the sample data (out of approximately 8000 rows of data). How can I replace all NAs with values from a smoothing spline fit to the rest of the data? Date Max Min Rain RHM RHE 4/24/1981 35.9 24.7 0.0 71 37 4/25/1981 36.8 22.8 0.0 62 40 4/26/1981 36.0 22.6 0.0 47 37 4/27/1981 35.1 24.2 0.0 51 39 4/28/1981 35.4 23.8 0.0 61 47 4/29/1981 35.4 25.1 0.0 67 43 4/30/1981 37.4 24.8 0.0 72 34 5/1/1981 NA NA NA NA NA 5/2/1981 39.0 25.3 NA NA 55 5/3/1981 35.9 23.0 0.0 68 66 5/4/1981 28.4 22.4 0.7

Subsetting winter (Dez, Jan, Feb) from daily time series (zoo)

◇◆丶佛笑我妖孽 提交于 2019-11-30 16:22:27
I have a daily zoo (xts) with a few decades of data in the following format: head(almorol) 1973-10-02 1973-10-03 1973-10-04 1973-10-05 1973-10-06 1973-10-07 183.9 208.2 153.7 84.8 52.5 35.5 and I would like to plot just winter data (the full months of December, January and February). I found the subsetting for xts so I thought I could extract all the Decembers using: x<-apply.yearly(almorol, FUN=last(almorol, "1 month")) and then do something similar for Jan and Feb, but I get the following error: Error in get(as.character(FUN), mode = "function", envir = envir) : object 'FUN' of mode

rolling regression with dplyr

China☆狼群 提交于 2019-11-30 16:10:03
I have a dataframe of "date", "company" and "return", reproducible by the code below: library(dplyr) n.dates <- 60 n.stocks <- 2 date <- seq(as.Date("2011-07-01"), by=1, len=n.dates) symbol <- replicate(n.stocks, paste0(sample(LETTERS, 5), collapse = "")) x <- expand.grid(date, symbol) x$return <- rnorm(n.dates*n.stocks, 0, sd = 0.05) names(x) <- c("date", "company", "return") With this dataframe, I can calculate the daily market average return and add that result into a new column "market.ret". x <- group_by(x, date) x <- mutate(x, market.ret = mean(x$return, na.rm = TRUE)) Now I want to

Subsetting winter (Dez, Jan, Feb) from daily time series (zoo)

て烟熏妆下的殇ゞ 提交于 2019-11-30 15:59:02
问题 I have a daily zoo (xts) with a few decades of data in the following format: head(almorol) 1973-10-02 1973-10-03 1973-10-04 1973-10-05 1973-10-06 1973-10-07 183.9 208.2 153.7 84.8 52.5 35.5 and I would like to plot just winter data (the full months of December, January and February). I found the subsetting for xts so I thought I could extract all the Decembers using: x<-apply.yearly(almorol, FUN=last(almorol, "1 month")) and then do something similar for Jan and Feb, but I get the following

rollapply with “growing” window

人盡茶涼 提交于 2019-11-30 13:04:54
Guys, normally when you do something like: tmp = zoo(rnorm(100), 1:100) rollapply(tmp, 10, function(x) quantile(x, 0.05), align="right") Quite rightly rollapply will start calculating the value from the moment 10 elements are available. Unfortunately I need something that uses as much data as possible for the fist 10 observations, essentially a growing window of data till there is enough data to use a sliding window, e.g. 1, 1:2, 1:3, 1:4, etc. till we have at least 10 elements and then slide the window as usual. Is there a better way to do this than an ugly for loop? Why not just pad the

How to analyse irregular time-series in R

人走茶凉 提交于 2019-11-30 11:36:22
问题 I have a zoo time series in R: d <- structure(c(50912, 50912, 50912, 50912, 50913, 50913, 50914, 50914, 50914, 50915, 50915, 50915, 50916, 50916, 50916, 50917, 50917, 50917, 50918, 50918, 2293.8, 2302.64, 2310.5, 2324.02, 2312.25, 2323.93, 2323.83, 2338.67, 2323.1, 2320.77, 2329.73, 2319.63, 2330.86, 2323.38, 2322.92, 2317.71, 2322.76, 2286.64, 2294.83, 2305.06, 55.9, 62.8, 66.4, 71.9, 59.8, 65.7, 61.9, 67.9, 38.5, 36.7, 43.2, 30.3, 42.4, 33.5, 48.8, 52.7, 61.2, 30, 41.7, 50, 8.6, 9.7, 10.3,

R not finding package even after package installation

时间秒杀一切 提交于 2019-11-30 10:28:39
问题 I have always worked with the zoo package, that I have installed a long time ago. Today, I created a new R script, and ran library(zoo) and got the following error: > library(zoo) Error in library(zoo) : there is no package called ‘zoo’ Odd.. Still, I tried reinstalling the package using install.packages("zoo") , and get the following: > install.packages("zoo") Installing package(s) into ‘C:/Users/U122337.BOSTONADVISORS/Documents/R/win-library/2.15’ (as ‘lib’ is unspecified) --- Please select

R/zoo: index entries in ‘order.by’ are not unique

我是研究僧i 提交于 2019-11-30 10:02:24
I have a .csv file containing 4 columns of data against a column of dates/times at one-minute intervals. Some timestamps are missing, so I'm trying to generate the missing dates/times and assign them NA values in the Y columns. I have previously done this with other .csv files with exactly the same formatting, with no issues. The code is: # read the csv file har10 = read.csv(fpath, header=TRUE); # set date har10$HAR.TS<-as.POSIXct(har10$HAR.TS,format="%y/%m/%d %H:%M") # convert to zoo df1.zoo<-zoo(har10[,-1],har10[,1]) #set date to Index # merge and generate NAs df2 <- merge(df1.zoo,zoo(,seq

Calculate rolling correlation using rollapply

我的未来我决定 提交于 2019-11-30 08:57:40
I have zoo object with 10000+ rows. > head(tt) A B 2007-01-04 0.005945924 0.0021167475 2007-01-05 -0.004201991 -0.0080020024 2007-01-08 0.001740897 0.0045804104 2007-01-09 0.000000000 -0.0008163931 2007-01-10 -0.004503531 0.0032615812 2007-01-11 -0.005841138 0.0043863282 I have tried variations of the following line, but to no avail. rollapply(tt, 21, function(x) cor(x[,1],x[,2])) Every entry gave correlation of 1, looks like it's picking up the 1 off the diagonal of the correlation matrix. 2013-11-25 1 1 2013-11-26 1 1 2013-11-27 1 1 2013-11-29 1 1 2013-12-02 1 1 2013-12-03 1 1 What I really

Convert daily to weekly/monthly data with R

我是研究僧i 提交于 2019-11-30 07:02:30
问题 I have daily prices series over a wide range of products; I want to convert to a new dataframe with weekly or monthly data. I first used xts in order to apply the to.weekly function...which works only for OHLC format. I am sure there may exist a function similar to to.weekly but for dataframe where the format is not OHLC. There a different posts already related to this as the following: Does rollapply() allow an array of results from call to function? or Averaging daily data into weekly data