I am using R to do some analysis. Previously, I have write my own functions and the R script run perfectly on mac OS.
However, when I try to run the same R script on Windows 64-bit, I came across some strange problems. For instance, after I installed and loaded the package plyr, I can actually run the function laply directly. But when I run my own function, which consists of function laply, it returns an error stating that "could not find function laply".
Also, since I tried to perform parallel computing, I loaded the package doParallel and use it together with library foreach. However, one of my functions returns error which states that could not find function %do%, while other functions do not. This is very very strange to me and I am so eager to solve it, yet no clues for me.
The error takes place in the function called Func.prune. Basically, it examines the association rules and find the redundant rules based on the lift value. The function is shown as follows. Here I also provide some input data.
rules <- list(Ant=list(c("CDWP = 3","CT in [369.38; 450.629]"),
c("CDWP = 3","Month = 3"),
c("Month = 3","PCHWP = 3"),
c("CDWP = 3","Month = 3"),
c("CDWP = 3","Month = 3","PCHWP = 3")),
Con=list("PCHWP = 3",
"WCC in [1040.528; 1882.797]",
"WCC in [1040.528; 1882.797]",
c("PCHWP = 3","WCC in [1040.528; 1882.797]"),
"WCC in [1040.528; 1882.797]"))
rules.m=data.frame(Freq=c(1760,rep(1740,4)),
Supp=c(0.2821,rep(0.2788,4)),
Conf=rep(1,5),
Lift=c(1.814250,1.946198,1.946198,2.028336,1.946198))
accuracy=50
Func.prune <- function(rules, rules.m, accuracy) {
require(foreach)
require(doParallel)
require(plyr)
registerDoParallel(cores=12)
item.ant <- llply(.data=rules$Ant, .fun=function(x) sapply(strsplit(x=x, split=" "), FUN=function(x) x[1]))
item.con <- llply(.data=rules$Con, .fun=function(x) sapply(strsplit(x=x, split=" "), FUN=function(x) x[1]))
res.prune <- foreach(i=1:length(item.ant)) %dopar% {
ant.ori <- rules$Ant[[i]]
con.ori <- rules$Con[[i]]
ant <- item.ant[[i]]
con <- item.con[[i]]
res.1 <- sapply(X=item.ant, FUN=function(x) {
if((length(x)<length(ant)) && (length(which(x %in% ant))==length(x))) {out=1} else {out=0}
return(out)})
res.2 <- sapply(X=item.con, FUN=function(x) {
if(length(x)==length(con) && length(which(x%in%con))==length(x)) {out=1} else {out=0}
return(out)
})
ind.sub.cand <- which(res.1==1 & res.2==1)
if(length(ind.sub.cand)==0) {final.upd=0} else {
#To check whether the consequent of sub candidate is the same with the consequent of considered rules
#Need to define accuracy to join similar ranges
ind.filt <- foreach (j = 1:length(ind.sub.cand), .combine=c) %do% {
ant.cand <- rules$Ant[[ind.sub.cand[j]]]
con.cand <- rules$Con[[ind.sub.cand[j]]]
con.cand.ind <- foreach(m = 1:length(con.cand), .combine=c) %do% {
if(length(grep(pattern="=", x=con.cand[m]))==1) {
out.ind=ifelse(sapply(X=strsplit(x=con.cand[m], split=" = "), FUN=function(x) x[2])==sapply(X=strsplit(con.ori[grep(pattern=sapply(X=strsplit(x=con.cand[m], split=" = "), FUN=function(x) x[1]), x=con.ori)], split=" = "), FUN=function(x) x[2]), yes=T, no=F)
} else {
name <- sapply(strsplit(x=con.cand[m], split=" in "), FUN=function(x) x[1])
low.ori <- sapply(strsplit(x=sapply(X=strsplit(x=con.ori[grep(pattern=name, x=con.ori)], split=" in "), FUN=function(x) x[2]), split="; "), FUN=function(x) x[1])
high.ori <- sapply(strsplit(x=sapply(X=strsplit(x=con.ori[grep(pattern=name, x=con.ori)], split=" in "), FUN=function(x) x[2]), split="; "), FUN=function(x) x[2])
low.ori.upd <- round_any(as.numeric(substr(x=low.ori, start=2, stop=nchar(low.ori))), accuracy=accuracy, f=floor)
high.ori.upd <- round_any(as.numeric(substr(x=high.ori, start=2, stop=(nchar(high.ori))-1)), accuracy=accuracy, f=ceiling)
low <- sapply(strsplit(x=sapply(strsplit(x=con.cand[m], split=" in "), FUN=function(x) x[2]), split="; "), FUN=function(x) x[1])
high <- sapply(strsplit(x=sapply(strsplit(x=con.cand[m], split=" in "), FUN=function(x) x[2]), split="; "), FUN=function(x) x[2])
low.upd <- round_any(as.numeric(substr(x=low, start=2, stop=nchar(low))), accuracy=accuracy, f=floor)
high.upd <- round_any(as.numeric(substr(x=high, start=1, stop=(nchar(low)-1))), accuracy=accuracy, f=ceiling)
out.ind <- ifelse(low.upd==low.ori.upd && high.upd==high.ori.upd, yes=T, no=F)
}
return(out.ind)
}
con.match <- ifelse(length(which(con.cand.ind==T))==length(con.cand), yes=1, no=0)
}
ind.sub.upd <- ind.sub.cand[which(ind.filt==1)]
if(length(ind.sub.upd)==0) {final.upd=0} else {
#To check whether the antecedent of sub candidate are subset of the considered rule's antecedent
out.final <- foreach(q = 1:length(ind.sub.upd), .combine=c) %do% {
ant.filt <- rules$Ant[[ind.sub.upd[q]]]
ant.ind <- foreach(p = 1:length(ant.filt), .combine=c) %do% {
if (length(grep(pattern=" = ", x=ant.filt[p]))==1) {
name <- sapply(strsplit(x=ant.filt[[p]], split=" = "), FUN=function(x) x[1])
ant.ori.value <- ant.ori[grep(pattern=name, x=ant.ori)]
res.ind <- ifelse(sapply(X=strsplit(x=ant.filt[[p]], split=" = "), FUN=function(x) x[2])==sapply(strsplit(ant.ori.value, split=" = "), FUN=function(x) x[2]), yes=T, no=F)
} else {
name <- sapply(strsplit(x=ant.filt[[p]], split=" in "), FUN=function(x) x[1])
ant.ori.value <- ant.ori[grep(pattern=name, x=ant.ori)]
low.ori <- sapply(strsplit(x=sapply(X=strsplit(ant.ori.value, split=" in "), FUN=function(x) x[2]), split="; "), FUN=function(x) x[1])
high.ori <- sapply(strsplit(x=sapply(X=strsplit(ant.ori.value, split=" in "), FUN=function(x) x[2]), split="; "), FUN=function(x) x[2])
low.ori.upd <- round_any(x=as.numeric(substr(x=low.ori, start=2, stop=nchar(low.ori))), accuracy=accuracy, f=floor)
high.ori.upd <- round_any(x=as.numeric(substr(x=high.ori, start=1, stop=(nchar(high.ori)-1))), accuracy=accuracy, f=ceiling)
low <- sapply(strsplit(x=sapply(strsplit(x=ant.filt[p], split=" in "), FUN=function(x) x[2]), split="; "), FUN=function(x) x[1])
high <- sapply(strsplit(x=sapply(strsplit(x=ant.filt[p], split=" in "), FUN=function(x) x[2]), split="; "), FUN=function(x) x[2])
low.upd <- round_any(as.numeric(substr(x=low, start=2, stop=nchar(low))), accuracy=accuracy, f=floor)
high.upd <- round_any(as.numeric(substr(x=high, start=1, stop=(nchar(low)-1))), accuracy=accuracy, f=ceiling)
res.ind <- ifelse((low.upd>=low.ori.upd) && (high.upd<=high.ori.upd), yes=T, no=F)
}
return(res.ind)
}
ant.match <- ifelse(length(which(ant.ind==T))==length(ant.filt), yes=1, no=0)
}
ind.sub.final <- ind.sub.upd[which(out.final==1)]
#To check the lift value
final <- foreach(o = 1:length(ind.sub.final), .combine=c) %do% {
lift.ori <- rules.m[i, "Lift"]
lift.sub <- rules.m[ind.sub.final[o], "Lift"]
v <- ifelse(lift.sub >= lift.ori, yes=T, no=F)
}
final.upd <- ifelse(length(which(final==T))==0, yes=0, no=ind.sub.final[which(final==T)])
}
return(final.upd)
}
}
return(res.prune)
}
So when actualy run this function:
Func.prune(rules=rules, rules.m=rules.m, accuracy=accuracy)
I got the following error: Error in { : task 5 failed - couldnot find function %do%
Any help is appreciated. Thanks in advance for your help.