我目前正在使用以下方法将mts数据集转换为时间索引为列的数据框.有没有更优雅的方式来做到这一点?
z <- ts(matrix(rnorm(300), 100, 3), start=c(1961, 1), frequency=12)
YM<-cbind(Year=as.numeric(floor(time(z))),Month=as.numeric(cycle(z)))
z<-cbind(as.data.frame(YM),as.data.frame(z))
str(z)
Run Code Online (Sandbox Code Playgroud) 我想计算自单变量时间序列的200个周期高点以来经过的周期数.例如,这是SPY的收盘价:
require(quantmod)
getSymbols("SPY",from='01-01-1900')
Data <- Cl(SPY)
Run Code Online (Sandbox Code Playgroud)
现在,我可以使用Lagquantmod中的函数找到本系列的200个周期的高点:
periodHigh <- function(x,n) {
Lags <- Lag(x,1:n)
High <- x == apply(Lags,1,max)
x[High]
}
periodHigh(Data, 200)
Run Code Online (Sandbox Code Playgroud)
但是现在我被卡住了.如何将其合并回原始系列(Data)并计算系列中的每个点,自上一个n期高点以来经过了多少个时段?
是否有优雅的单线(使用任何R包)来完成以下任务?
tab <- aggregate(. ~ Species, dat=iris, mean)
total <- data.frame(Species='Overall', t(colMeans(iris[,-5])))
rbind(tab, total)
Run Code Online (Sandbox Code Playgroud) 我想知道是否有人在 R 中实现了类似于scikit-learn 管道的东西?它们是将数据集上的多个操作链接在一起的有用方法,例如 PCA + RBM + 逻辑回归。
在我看来,transformscikit-learn 中的方法类似于predictR 中的 S3 方法,并且fitscikit learn 中的方法类似于调用 R 中的函数,该函数返回带有 fit 方法的 S3 对象,因此可以是这样可能的。
(请注意,magrittr 的前向管道并没有真正解决问题:scikit-learn 的管道本身具有拟合和预测方法,这使得它们可重用并适用于新数据)
我想创建一个将混合数字和分数(作为字符串)转换为浮点数的函数.以下是一些例子:
'1 1/2' -> 1.5
'11/2' -> 5.5
'7/8' -> 0.875
'3' -> 3
'7.7' -> 7.7
Run Code Online (Sandbox Code Playgroud)
我目前正在使用这个功能,但我认为它可以改进.它也不处理已经是十进制表示的数字
def mixedtofloat(txt):
mixednum = re.compile("(\\d+) (\\d+)\\/(\\d+)",re.IGNORECASE|re.DOTALL)
fraction = re.compile("(\\d+)\\/(\\d+)",re.IGNORECASE|re.DOTALL)
integer = re.compile("(\\d+)",re.IGNORECASE|re.DOTALL)
m = mixednum.search(txt)
n = fraction.search(txt)
o = integer.search(txt)
if m:
return float(m.group(1))+(float(m.group(2))/float(m.group(3)))
elif n:
return float(n.group(1))/float(n.group(2))
elif o:
return float(o.group(1))
else:
return txt
Run Code Online (Sandbox Code Playgroud)
谢谢!
我正在尝试在R中编写一个程序,当给定一个向量时,它将返回该向量中所有可能的元素元组.
例如:元组(c('a','b','c'))= c('a','b','c'); 出租车'); c('a','c'),c('b','c'); C( 'A'); C( 'B'); C( 'C')
我认为它应该返回一个向量列表.
作为参考,这是一个在Stata中执行类似功能的程序.
我在R中有一个函数,给定n天,返回最后n个工作日的列表.我的解决方案工作正常,但感觉不够优雅,我想知道是否有任何简单的方法来改善它.
WeekdayList <- function(n) {
Today <- as.Date(Sys.time())
days <- c(Today)
i <- 1
while (length(days) < n) {
NewDay <- as.Date(Today-i)
if (!weekdays(NewDay) %in% c("Saturday", "Sunday")) {
days <- c(days,NewDay)
}
i <- i+1
}
days
}
WeekdayList(30)
WeekdayList(2)
Run Code Online (Sandbox Code Playgroud)
排除假期也是一个很好的功能.
我想计算一个向量中每个索引的等级,例如:
x <- c(0.82324952352792, 0.11953364405781, 0.588659686036408, 0.41683742380701,
0.11452184105292, 0.438547774450853, 0.586471405345947, 0.943002870306373,
0.28184655145742, 0.722095313714817)
calcRank <- function(x){
sorted <- x[order(x)]
ranks <- sapply(x, function(x) which(sorted==x))
return(ranks)
}
calcRank(x)
> calcRank(x)
[1] 9 2 7 4 1 5 6 10 3 8
Run Code Online (Sandbox Code Playgroud)
有一个更好的方法吗?
在R xgboost包中,我可以指定predictions=TRUE在交叉验证期间保存折叠后预测,例如:
library(xgboost)
data(mtcars)
xgb_params = list(
max_depth = 1,
eta = 0.01
)
x = model.matrix(mpg~0+., mtcars)
train = xgb.DMatrix(x, label=mtcars$mpg)
res = xgb.cv(xgb_params, train, 100, prediction=TRUE, nfold=5)
print(head(res$pred))
Run Code Online (Sandbox Code Playgroud)
我如何在python包中做相同的操作?我在python中找不到prediction参数xgboost.cv.