我正在运行RStudio Server,无法安装该zoo软件包.我得到的错误消息如下:
安装包(S)到一个'/ home/tsajid/R /库'试图URL(如'LIB'是不确定的)' http://mirrors.nics.utk.edu/cran/src/contrib/zoo_1.7-9 .tar.gz '内容类型'应用程序/ x-gzip'长度807084字节(788 Kb)打开URL =========================== =======================已下载788 Kb
**安装源包'zoo'...
**包'zoo'成功解压缩并检查MD5总和
**libs sh:make:命令未找到错误:包'zoo'编译失败
**删除'/ home/tsajid/R/library/zoo'install.packages中的警告:
'zoo'包的安装具有非零退出状态下载的源包位于'/ tmp/RtmpsKlJWz/downloaded_packages'中
我尝试安装包存档文件,但我收到相同的错误消息.
会话信息:
R version 2.15.1 (2012-06-22)
Platform: x86_64-redhat-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=C LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] svMisc_0.9-65
loaded via a namespace (and not attached):
[1] tools_2.15.1
Run Code Online (Sandbox Code Playgroud) 我有一组2D点存储在p:
x <- c(0, 1, 2, 3, 4)
y <- c(0, 1, 2, 3, 4)
p <- cbind(x,y)
Run Code Online (Sandbox Code Playgroud)
我需要计算每个连续点之间的距离,我计算距离的函数是hypotenuse(实际上我的问题有点不同,因为我有一个经度列表,纬度值,我需要distVincentyEllipsoid从geosphere包中使用).
hypotenuse <- function(p1,p2)
{
sqrt((p1[1]-p2[1])^2+(p1[2]-p2[2])^2)
}
Run Code Online (Sandbox Code Playgroud)
我想使用,diff但在我看来,我不能传递给diff自定义"差异"功能的功能,所以我的解决方案到现在为止如下:
distances <- c()
for(i in 2:nrow(p))
{
distances <- c(distances,hypotenuse(p[i,],p[i-1,]))
}
Run Code Online (Sandbox Code Playgroud)
我读了一个类似于R中的diff的迭代和滞后函数的问题,但不仅仅是区别?我尝试以这种方式使用包中的rollapply函数zoo:
library(zoo)
rollapply(p,width=1,FUN=hypotenuse)
Run Code Online (Sandbox Code Playgroud)
但我得到了错误
FUN错误(数据[posns],...):
缺少参数"p2",没有默认值
所以在我看来,FUN不能是二元函数.
是否可以使用该rollapply功能来解决我的问题?
我有两个价格系列
require(quantmod)
require(TTR)
tickers = c("IBM","SPY")
getSymbols(tickers, from="2010-10-20", to="2014-09-22")
prices = do.call(merge, lapply(tickers, function(x) Cl(get(x))))
> head(prices)
IBM.Close SPY.Close
2010-10-20 139.07 117.87
2010-10-21 139.83 118.13
2010-10-22 139.67 118.35
2010-10-25 139.84 118.70
2010-10-26 140.67 118.72
2010-10-27 141.43 118.38
Run Code Online (Sandbox Code Playgroud)
现在我想使用 TTR 包的 SMA 函数平滑系列。
sma.IMB = SMA(prices[,1])
sma.SPY = SMA(prices[,2])
sma.prices = cbind(sma.IBM, sma.SPY)
> head(sma.prices)
IBM.Close.SMA.3 SPY.Close.SMA.3
2010-10-20 NA NA
2010-10-21 NA NA
2010-10-22 139.5233 118.1167
2010-10-25 139.7800 118.3933
2010-10-26 140.0600 118.5900
2010-10-27 140.6467 118.6000
Run Code Online (Sandbox Code Playgroud)
这在处理许多资产时非常乏味,所以我想使用 apply 缩短这个过程
sma.prices = apply(prices, …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Zoo 包在 R 中构建滚动止盈/止损检测功能。
x <- as.data.frame(rnorm(10000, 0, 1))
x$cumul <- cumsum(x[, 1])
plot(x$cumul, type = 'l')
y <- as.data.frame(x$cumul)
level_break <- function(x, n, z){
if (min(c(1:nrow(x))[x[, 1] > z]) <= n
& (min(c(1:nrow(x))[x[, 1] > z]) < min(c(1:nrow(x))[x[, 1] < -z])
| min(c(1:nrow(x))[x[, 1] < -z]) > n)){
level <- 1
}else if (min(c(1:nrow(x))[x[, 1] < -z]) <= n
& (min(c(1:nrow(x))[x[, 1] < -z]) < min(c(1:nrow(x))[x[, 1] > z])
| min(c(1:nrow(x))[x[, 1] > z]) > n)){
level <- …Run Code Online (Sandbox Code Playgroud) 我目前正在使用一个大型data.table,它具有基于2个参考列的某些组,然后有一个距离列,为每个组的第一行定义,然后每次跳过2个单元.
制作一个非常小的可重复的例子,我有:
reference1 <- c("ref1", "ref1", "ref1", "ref2", "ref2", "ref2", "ref2", "ref3", "ref3", "ref3")
reference2 <- c("fer1", "fer1", "fer1", "fer1", "fer1", "fer1", "fer1", "fer2", "fer2", "fer2")
firstdist <- c(2, NA, NA, 5, NA, NA, NA, 8, NA, NA)
df <- data.frame(ref1 = reference1,
ref2 = reference2,
dist = firstdist)
Run Code Online (Sandbox Code Playgroud)
相当于
ref1 ref2 dist
1 ref1 fer1 2
2 ref1 fer1 NA
3 ref1 fer1 NA
4 ref2 fer1 5
5 ref2 fer1 NA
6 ref2 fer1 NA
7 ref2 fer1 …Run Code Online (Sandbox Code Playgroud) 我有关于产品销售的每日时间序列,我的系列从2016年1月1日至2017年8月31日开始.
考虑到它是一个为期六天的星期(我的星期一星期一开始,星期六结束)星期日没有数据,我知道在运行Arima模型之前我需要先填写缺失值.这是我需要帮助的地方:我读过我可以用na.approx或填写缺失的值NA,但我不知道该怎么做.
你可以在这里看到我的系列:
https://drive.google.com/file/d/0BzIf8XvzKOGWSm1ucUdYUVhfVGs/view?usp=sharing
如您所见,周日没有数据.我需要知道如何填充缺失值以运行Arima模型并能够预测2017年的剩余时间.
我想用.绘制不同时间序列数据的滚动平均值ggplot2。我的数据具有以下结构:
library(dplyr)
library(ggplot2)
library(zoo)
library(tidyr)
df <- data.frame(episode=seq(1:1000),
t_0 = runif(1000),
t_1 = 1 + runif(1000),
t_2 = 2 + runif(1000))
df.tidy <- gather(df, "time", "value", -episode) %>%
separate("time", c("t", "time"), sep = "_") %>%
subset(select = -t)
> head(df.tidy)
# episode time value
#1 1 0 0.7466480
#2 2 0 0.7238865
#3 3 0 0.9024454
#4 4 0 0.7274303
#5 5 0 0.1932375
#6 6 0 0.1826925
Run Code Online (Sandbox Code Playgroud)
现在,下面的代码创建了一个图,其中时间 = 1 和时间 = 2 的线在剧集开始时不代表数据,因为value填充了 NA …
Zoo 包具有函数 na.approx (或 na.spline),它可以用 approx 或 spline 替换数据中的 NA。
一个潜在的争论是maxgap哪一个给出了最大的需要填补的空白。这工作得很好,除了如果 NA 位于向量的开头,该函数会更改向量的长度。
例如:
require(zoo)
x <- 1:20 + rnorm(20)
x1 <- x
x1[5] <- x1[6] <- NaN
length(na.approx(x1, maxgap = 1)) == length(x) # TRUE
x2 <- x
x2[1] <- x2[2] <- NaN
length(na.approx(x2, maxgap = 1)) == length(x) # FALSE
Run Code Online (Sandbox Code Playgroud)
当将此函数与数据框一起使用时,这是有问题的。例如:
require(dplyr)
df1 <- tibble(A = 1:20, B = x1)
df1 %>%
mutate(B_fill = na.approx(B, maxgap = 1))
df2 <- tibble(A = 1:20, B = x2) …Run Code Online (Sandbox Code Playgroud) | 日期 |
|---|
| 1960年第一季度 |
| 1960年第二季度 |
| 1960年第三季度 |
| 1960年第四季度 |
| 1961年第一季度 |
| 1961年第二季度 |
我有以下数据框。我正在尝试将第一列放入 tsibble 中。现在我有一个问题。我如何切换到日期以便可以将其读取为季度。
我尝试zoo使用
DATA.QTR <- DATA.QTR %>% mutate(QUARTER = as.Date(as.yearqtr(Date, "%Y %Q")))
Run Code Online (Sandbox Code Playgroud)
但它没有读它。
我想要一个像这里包含的数据系列:
http://robjhyndman.com/tsdldata/roberts/beards.dat
...并将其加载到R中的动物园时间序列对象中.没有日期信息表,但它列出了它是常规的,年度的,并且从y = 1866开始.这就是我正在尝试的......
beard <- read.zoo('http://robjhyndman.com/tsdldata/roberts/beards.dat',
header=FALSE,
index.column=0,
start="1866-01-01",
format="%Y",
skip=4)
Run Code Online (Sandbox Code Playgroud)
它主要起作用,但忽略了zooreg的"开始"参数.
所以,我有一个很好的解决方案,读取这个,然后像这样改变索引...
index(beard) <- as.Date(paste(seq(1866,1911, by=1),'-01-01',sep=''), format="%Y-%m-%d")
Run Code Online (Sandbox Code Playgroud)
...但如果有一个论点read.zoo()让我在一次通话中这样做,这将会更加光滑.我错过了,还是两步问题?
r ×10
zoo ×10
time-series ×2
apply ×1
arima ×1
as.date ×1
data.table ×1
date ×1
ggplot2 ×1
missing-data ×1
portfolio ×1
tsibble ×1