2017-10-12 85 views
0

我試圖讓R apriori算法允許我在lhs中同時指定多個屬性。關於R Apriori的lhs的多個條件

rules <- apriori(Data, parameter=list(supp = 0.0001, conf = 0.001, minlen = 2), appearance = list(lhs = c("DiagnoseTekst=Acuut hartfalen"), default="rhs")) 

上面一行將篩選只是在一定DiagnoseTekst的LHS(在我的數據列是「MedicatieTekst」,「Geslacht」和「DiagnoseTekst」) 不過,我想這兩個DiagnoseTekst過濾和Geslacht。當我投入

rules <- apriori(Data, parameter=list(supp = 0.0001, conf = 0.001, minlen = 2), appearance = list(lhs = c("DiagnoseTekst=Acuut hartfalen", "Geslacht=M"), default="rhs")) 

我得到了一些只有DiagnoseTekst和一些只與Geslacht規則。 (在這種情況下,大多數情況下都應該具有這兩個屬性)。有什麼方法可以篩選搜索或結果,以便能夠爲lhs列指定多個條件?

的完整代碼是明確的:輸出的

Data <- as(data, "transactions") 

str(Data) 
rules <- apriori(Data, parameter=list(supp = 0.0001, conf = 0.001, minlen = 2), appearance = list(lhs = c("DiagnoseTekst=Acuut hartfalen"), default="rhs")) 
top.conf <- sort(rules, decreasing = TRUE, na.last = NA, by = c("confidence","lift")) 
set <- inspect(head(subset(top.conf), 30)) 

例子:

> set <- inspect(head(subset(top.conf), 30)) 
    lhs        rhs            support  confidence lift  count 
[1] {DiagnoseTekst=Acuut hartfalen} => {Geslacht=V}          0.066477566 0.525500378 1.1539592 30561 
[2] {DiagnoseTekst=Acuut hartfalen} => {Geslacht=M}          0.060025798 0.474499622 0.8712635 27595 
[3] {DiagnoseTekst=Acuut hartfalen} => {MedicatieTekst=FUROSEMIDE}      0.017917467 0.141636289 2.9290550 8237 
[4] {DiagnoseTekst=Acuut hartfalen} => {MedicatieTekst=METOPROLOL}      0.006279923 0.049642341 0.9877311 2887 
[5] {DiagnoseTekst=Acuut hartfalen} => {MedicatieTekst=PARACETAMOL}      0.005201003 0.041113557 0.6085413 2391 

回答

0

如果我理解正確的話,那麼你要確保兩個規定的項目必須在LHS發生的規則。外觀只能限制規則中可能出現的項目,但並不是所有項目都必須存在。但是,您可以使用subset進行過濾來解決此問題。下面的代碼中找到與使用%ain%在規則的LHS項目「年齡=高級」和「性別=男」的所有規則(都在,見? "%ain%"

> library("arules") 
> data(Adult) 

> rules <- apriori(Adult) 
> rules 
set of 6137 rules 

> rules <- subset(rules, lhs %ain% c("age=Senior","sex=Male")) 
> rules 
set of 167 rules 

> inspect(head(rules, by = "lift", n = 3)) 
    lhs         rhs     support confidence lift count 
[1] {age=Senior,                    
    marital-status=Married-civ-spouse,               
    sex=Male,                     
    capital-gain=None,                   
    native-country=United-States}  => {relationship=Husband} 0.12   1 2.5 5687 
[2] {age=Senior,                    
    marital-status=Married-civ-spouse,               
    race=White,                    
    sex=Male,                     
    capital-gain=None,                   
    native-country=United-States}  => {relationship=Husband} 0.11   1 2.5 5293 
[3] {age=Senior,                    
    marital-status=Married-civ-spouse,               
    sex=Male,                     
    capital-gain=None,                   
    capital-loss=None,                   
    native-country=United-States}  => {relationship=Husband} 0.11   1 2.5 5238