循环并清除R中的基本功能

时间:2014-06-04 22:48:11

标签: r loops

我有这个数据集

install.packages("combinat")
install.packages("quantmod")
library(quantmod)
library(combinat)
library(utils)
getSymbols("AAPL",from="2012-01-01")
data<-AAPL

p1<-4    
dO<-data[,1]
dC<-data[,4]
emaO<-EMA(dO,n=p1)
emaC<-EMA(dC,n=p1)
Pos_emaO_dO_UP<-emaO>dO
Pos_emaO_dO_D<-emaO<dO
Pos_emaC_dC_UP<-emaC>dC
Pos_emaC_dC_D<-emaC<dC
Pos_emaC_dO_D<-emaC<dO
Pos_emaC_dO_UP<-emaC>dO
Pos_emaO_dC_UP<-emaO>dC
Pos_emaO_dC_D<-emaO<dC
Profit_L_1<-((lag(dC,-1)-lag(dO,-1))/(lag(dO,-1)))*100
Profit_L_2<-(((lag(dC,-2)-lag(dO,-1))/(lag(dO,-1)))*100)/2
Profit_L_3<-(((lag(dC,-3)-lag(dO,-1))/(lag(dO,-1)))*100)/3
Profit_L_4<-(((lag(dC,-4)-lag(dO,-1))/(lag(dO,-1)))*100)/4
Profit_L_5<-(((lag(dC,-5)-lag(dO,-1))/(lag(dO,-1)))*100)/5
Profit_L_6<-(((lag(dC,-6)-lag(dO,-1))/(lag(dO,-1)))*100)/6
Profit_L_7<-(((lag(dC,-7)-lag(dO,-1))/(lag(dO,-1)))*100)/7
Profit_L_8<-(((lag(dC,-8)-lag(dO,-1))/(lag(dO,-1)))*100)/8
Profit_L_9<-(((lag(dC,-9)-lag(dO,-1))/(lag(dO,-1)))*100)/9
Profit_L_10<-(((lag(dC,-10)-lag(dO,-1))/(lag(dO,-1)))*100)/10

给予此框架

frame<-data.frame(Pos_emaO_dO_UP,Pos_emaO_dO_D,Pos_emaC_dC_UP,Pos_emaC_dC_D,Pos_emaC_dO_D,Pos_emaC_dO_UP,Pos_emaO_dC_UP,Pos_emaO_dC_D,Profit_L_1,Profit_L_2,Profit_L_3,Profit_L_4,Profit_L_5,Profit_L_6,Profit_L_7,Profit_L_8,Profit_L_9,Profit_L_10)
colnames(frame)<-c("Pos_emaO_dO_UP","Pos_emaO_dO_D","Pos_emaC_dC_UP","Pos_emaC_dC_D","Pos_emaC_dO_D","Pos_emaC_dO_UP","Pos_emaO_dC_UP","Pos_emaO_dC_D","Profit_L_1","Profit_L_2","Profit_L_3","Profit_L_4","Profit_L_5","Profit_L_6","Profit_L_7","Profit_L_8","Profit_L_9","Profit_L_10")

有变量的向量供以后使用

vector<-c("Pos_emaO_dO_UP","Pos_emaO_dO_D","Pos_emaC_dC_UP","Pos_emaC_dC_D","Pos_emaC_dO_D","Pos_emaC_dO_UP","Pos_emaO_dC_UP","Pos_emaO_dC_D")

我将所有可能的组合与向量的4个变量组合在一起(没有依赖变量)

comb<-as.data.frame(combn(vector,4))
comb

然后离开,胡说八道&#34;组合(变量的两个可能值)

rc<-comb[!sapply(comb, function(x) any(duplicated(sub('_D|_UP', '', x))))]
rc

然后我准备第一个组合以便稍后进行分组

var<-paste(rc[,1],collapse=" & ")
var

并对帧进行子集化(使用所有DV)

kr<-eval(parse(text=paste0('subset(frame,' , var,')' )))
kr

现在我通过4个变量的第一个组合得到了subseted df。 然后我用它上面的评价函数

evaluation<-function(x){
  s_1<-nrow(x[x$Profit_L_1>0,])/nrow(x)
  s_2<-nrow(x[x$Profit_L_2>0,])/nrow(x)
  s_3<-nrow(x[x$Profit_L_3>0,])/nrow(x)
  s_4<-nrow(x[x$Profit_L_4>0,])/nrow(x)
  s_5<-nrow(x[x$Profit_L_5>0,])/nrow(x)
  s_6<-nrow(x[x$Profit_L_6>0,])/nrow(x)
  s_7<-nrow(x[x$Profit_L_7>0,])/nrow(x)
  s_8<-nrow(x[x$Profit_L_8>0,])/nrow(x)
  s_9<-nrow(x[x$Profit_L_9>0,])/nrow(x)
  s_10<-nrow(x[x$Profit_L_10>0,])/nrow(x)
  n_1<-nrow(x[x$Profit_L_1>0,])/nrow(frame)
  n_2<-nrow(x[x$Profit_L_2>0,])/nrow(frame)
  n_3<-nrow(x[x$Profit_L_3>0,])/nrow(frame)
  n_4<-nrow(x[x$Profit_L_4>0,])/nrow(frame)
  n_5<-nrow(x[x$Profit_L_5>0,])/nrow(frame)
  n_6<-nrow(x[x$Profit_L_6>0,])/nrow(frame)
  n_7<-nrow(x[x$Profit_L_7>0,])/nrow(frame)
  n_8<-nrow(x[x$Profit_L_8>0,])/nrow(frame)
  n_9<-nrow(x[x$Profit_L_9>0,])/nrow(frame)
  n_10<-nrow(x[x$Profit_L_10>0,])/nrow(frame)
  pr_1<-sum(kr[,"Profit_L_1"])/nrow(kr[,kr=="Profit_L_1"])
  pr_2<-sum(kr[,"Profit_L_2"])/nrow(kr[,kr=="Profit_L_2"])
  pr_3<-sum(kr[,"Profit_L_3"])/nrow(kr[,kr=="Profit_L_3"])
  pr_4<-sum(kr[,"Profit_L_4"])/nrow(kr[,kr=="Profit_L_4"])
  pr_5<-sum(kr[,"Profit_L_5"])/nrow(kr[,kr=="Profit_L_5"])
  pr_6<-sum(kr[,"Profit_L_6"])/nrow(kr[,kr=="Profit_L_6"])
  pr_7<-sum(kr[,"Profit_L_7"])/nrow(kr[,kr=="Profit_L_7"])
  pr_8<-sum(kr[,"Profit_L_8"])/nrow(kr[,kr=="Profit_L_8"])
  pr_9<-sum(kr[,"Profit_L_9"])/nrow(kr[,kr=="Profit_L_9"])
  pr_10<-sum(kr[,"Profit_L_10"])/nrow(kr[,kr=="Profit_L_10"])
  mat<-matrix(c(s_1,n_1,pr_1,s_2,n_2,pr_2,s_3,n_3,pr_3,s_4,n_4,pr_4,s_5,n_5,pr_5,s_6,n_6,pr_6,s_7,n_7,pr_7,s_8,n_8,pr_8,s_9,n_9,pr_9,s_10,n_10,pr_10),ncol=3,nrow=10,dimnames=list(c(1:10),c("s","n","pr")))
  df<-as.data.frame(mat)
  return(df)
}
result<-evaluation(kr)
result

我需要在几个案例中提供帮助。

1,在评估函数中,矩阵的形成方式是错误的(s_1,n_1,pr_1从第一列开始,但我需要按行开始排序)

2,我需要使用一些循环/ lapply函数来通过所有可能的组合(不仅是第一个像这种情况(var<-paste(rc[,1],collapse=" & "))并且具有可理解的输出,其中评估函数用于每个组合和我将能够看到变量的哪个组合是完成的评估(理解我需要认识到这个评估的内容)并比较每个组合的评估结果。

3,这不是主要观点,但我通常想要评估所有可能的组合(它意味着2:n个变量以及每个变量的所有组合)然后根据特定的DV获得最佳组合(Profit_L_1或Profit_L_2等)。我现在在循环方面非常弱,所以,如果可能的话,请记住我以后要用它做些什么。

谢谢,随时更新,修复或改进问题(如果有一些事情可以更轻松,更有效地完成 - 做到这一点 - 我愿意接受每一个有意义的建议。

0 个答案:

没有答案