Different output from code used in function and used alone in R -
i've been working kind of data evaluate stocks diploma thesis
library(quantmod) getsymbols("nok",from="2012-01-01") data<-nok p1<-4 dc<-data[,4] do<-data[,1] emac<-ema(dc,n=p1) w1<-1.1 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 profit_l_3<-((lag(dc,-3)-lag(do,-1))/(lag(do,-1)))*100 profit_l_4<-((lag(dc,-4)-lag(do,-1))/(lag(do,-1)))*100 profit_l_5<-((lag(dc,-5)-lag(do,-1))/(lag(do,-1)))*100 profit_l_all<-ifelse(profit_l_1>w1,profit_l_1, ifelse(profit_l_2>w1,profit_l_2, ifelse(profit_l_3>w1,profit_l_3, ifelse(profit_l_4>w1,profit_l_4, ifelse(profit_l_5>w1,profit_l_5,profit_l_5))))) choosedn<-ifelse(profit_l_all==profit_l_1,1, ifelse(profit_l_all==profit_l_2,2, ifelse(profit_l_all==profit_l_3,3, ifelse(profit_l_all==profit_l_4,4, ifelse(profit_l_all==profit_l_5,5,0))))) profit_day<-profit_l_all/choosedn winloss<-ifelse(profit_day>0.3,1, ifelse(profit_day<=0.3,0,"nothing")) pos_emac_dc_d<-emac<dc pos_emac_dc_up<-emac>dc frame<-data.frame(pos_emac_dc_up,pos_emac_dc_d,profit_l_all,choosedn,profit_day,winloss) colnames(frame)<-c("pos_emac_dc_up","pos_emac_dc_d","profit_l_all","choosedn","profit_day","winloss") frame<-frame[complete.cases(frame),]
output of data.frame called frame work with. clear without na's.
i have written function quick evaluate combinations of variables in frame
quickmycomb<-function (x,y){ combination<-deparse(substitute(y)) df<-subset(x,y) success<-length(subset(df,winloss==1)$winloss)/length(df$winloss) absnumber<-nrow(df) relnumber<-nrow(df)/nrow(frame) sl<-data.frame(success,absnumber,relnumber) return(sl) }
and when use function like
quickmycomb(frame,lag(pos_emac_dc_d,1) & pos_emac_dc_up)
it says output frame 86 rows in case
success absnumber relnumber 1 0.6162791 86 0.1396104
but when use code function subset(frame,"combination") same combination of variables like
subset(frame,lag(pos_emac_dc_d,1) & pos_emac_dc_up)
it says output data.frame 0 rows!!! don't understand it, suppose right output second, don't know why doesn't work in function. advices should problem?
you cannot use subset
variable condition because subset use substitute
see passed function. however, can pass through values subset. 1 fix is
quickmycomb.1 <- function (x,...) { df <- subset(x,...) success <- length(subset(df,winloss==1)$winloss)/length(df$winloss) absnumber <- nrow(df) relnumber <- nrow(df)/nrow(frame) sl <- data.frame(success,absnumber,relnumber) return(sl) }
alternative can process subsetting inside function with
quickmycomb.2 <- function (x,y){ combination <- eval(substitute(y), x, parent.frame()) df <- x[combination, ] success <- length(subset(df,winloss==1)$winloss)/length(df$winloss) absnumber <- nrow(df) relnumber <- nrow(df)/nrow(frame) sl <- data.frame(success,absnumber,relnumber) return(sl) }
when run them both
quickmycomb.1(frame,lag(pos_emac_dc_d,1) & pos_emac_dc_up) quickmycomb.2(frame,lag(pos_emac_dc_d,1) & pos_emac_dc_up)
they both produce
success absnumber relnumber 1 nan 0 0
just when run current environment.
Comments
Post a Comment