如何使用R

时间:2017-11-15 09:30:16

标签: r database statistics correlation data-science

我想计算几种产品之间的多重相关性,具体取决于将它们组合在一起的频率(即EX:产品A与产品B的相关系数较大,因为A的大多数订单与产品B的订单相同)OR(如果没有订单同时有A& B那么它们之间的相关性= 0) 我想这样做是为每个新订单创建一个推荐模型(根据所选产品与其他产品之间的相关程度推荐产品)。 这是我的数据样本:

structure(list(WHWorkOrderHeaderId = c(137413L, 137413L, 137413L, 
137413L, 137413L, 137413L, 137413L, 137413L, 137413L, 137413L, 
137413L, 137413L, 136729L, 136729L, 136729L, 136729L, 136729L, 
136729L, 136729L, 136729L, 136729L, 136729L, 136729L, 136729L, 
136729L, 137260L, 137260L, 137260L, 137260L, 137260L, 137260L, 
137260L, 137260L, 137260L, 137260L, 137429L, 137429L, 137429L, 
137429L, 137429L, 137429L, 137429L, 137429L, 137429L, 137974L, 
137974L, 137902L, 137902L, 137902L, 137902L, 137902L, 137902L, 
137902L, 138837L, 138837L, 138837L, 138837L, 138837L, 138837L, 
140938L, 140938L, 140938L, 140938L, 140938L, 140938L, 140938L, 
140938L, 140938L, 140938L, 139642L, 139642L, 139642L, 139642L, 
139642L, 139642L, 139642L, 140676L, 140676L, 140676L, 140676L, 
140676L, 140676L, 139424L, 139424L, 139424L, 139424L, 139424L, 
139424L, 139424L, 139424L, 139424L, 141302L, 141302L, 141302L, 
141302L, 141302L, 141302L, 138297L, 138297L, 138297L, 138297L, 
138297L, 138297L, 138297L, 138297L, 138297L, 138297L, 138297L, 
138297L, 138297L, 137302L, 137302L, 137302L, 137302L, 134444L, 
134444L, 134444L, 141134L, 141134L, 141134L, 141134L, 134468L, 
134468L, 131965L, 131965L, 131965L, 131965L, 131965L, 131965L, 
131965L, 131965L, 131965L, 135722L, 135722L, 135722L, 135722L, 
135722L, 135722L, 135722L, 135722L, 135722L, 135722L, 135722L, 
135722L, 135722L, 135722L, 139444L, 139444L, 139444L, 139444L, 
139444L, 139444L, 137419L, 137419L, 137419L, 137419L, 137419L, 
137419L, 137419L, 137419L, 137419L, 137419L, 137419L, 137419L, 
136078L, 136078L, 136078L, 136078L, 136078L, 136078L, 136078L, 
136078L, 136078L, 136078L, 136078L, 136078L, 136078L, 131866L, 
131866L, 131866L, 131866L, 131866L, 131866L, 131866L, 139214L, 
139214L, 139214L, 139214L, 139214L, 139214L, 131997L, 131997L, 
131997L, 131997L, 139482L, 139482L, 139482L, 139482L, 139482L, 
139482L, 139482L, 137352L, 137352L, 137352L, 137352L, 137352L, 
137352L, 137352L, 137352L, 136610L, 136610L, 136610L, 136610L, 
136610L, 136066L, 136066L, 136066L, 136066L, 136066L, 136066L, 
136066L, 133358L, 133358L, 133358L, 134522L, 134522L, 134522L, 
134522L, 134522L, 134522L, 134522L, 134522L, 131481L, 131481L, 
131481L, 131918L, 131918L, 131918L, 131918L, 135758L, 135758L, 
135758L, 135758L, 135678L, 135678L, 135678L, 135678L, 135678L, 
135678L, 135678L, 135678L, 135678L, 131986L, 131986L, 131986L, 
131986L, 131986L, 131986L, 131986L, 139468L, 139468L, 139468L, 
139468L, 139468L, 139468L, 139468L, 139468L, 139468L, 139468L, 
139533L, 139533L, 139533L, 139533L, 139533L, 139533L, 139533L, 
139533L, 139533L, 131950L, 137540L, 137540L, 137540L, 137540L, 
137540L, 137540L, 137540L, 138021L, 138021L, 138021L, 138021L, 
138021L, 138021L, 138021L, 138021L, 138021L, 139280L, 139280L, 
139280L, 139280L, 139280L, 139280L, 139280L, 139280L, 139280L, 
139280L, 139280L, 139280L, 139280L, 139280L, 139280L, 139280L, 
134490L, 134490L, 134490L, 134490L, 134490L, 134490L, 134490L, 
134490L, 134490L, 134490L, 135747L, 135747L, 135747L, 135747L, 
135747L, 135747L, 135747L, 135747L, 135747L, 135747L, 135747L, 
141057L, 141057L, 141057L, 141057L, 141057L, 141057L, 141057L, 
141057L, 141057L, 141057L, 141057L, 141057L, 141057L, 141057L, 
141057L, 141057L, 141057L, 141057L, 141057L, 141057L, 141057L, 
136233L, 136233L, 136233L, 136233L, 136233L, 136233L, 136233L, 
136233L, 136233L, 136233L, 136233L, 136233L, 136233L, 136233L, 
136233L, 136233L, 136233L, 136233L, 136233L, 136233L, 136233L, 
136430L, 136430L, 136430L, 136430L, 136430L, 136430L, 136430L, 
136430L, 136430L, 136430L, 136430L, 136430L, 136430L, 136430L, 
141055L, 141055L, 141055L, 141055L, 141055L, 141055L, 141055L, 
141055L, 131666L, 131666L, 131666L, 131666L, 131666L, 131666L, 
131666L, 131666L, 131551L, 131551L, 131551L, 131551L, 131551L, 
131551L, 131551L, 131551L, 131551L, 131469L, 131469L, 131469L, 
131469L, 131469L, 131469L, 131469L, 131469L, 131469L, 131469L, 
131469L, 131301L, 131321L, 131775L, 131775L, 131775L, 131775L, 
131775L, 131775L, 131775L, 131775L, 131993L, 131993L, 131993L, 
131993L, 131993L, 131993L, 131993L, 131993L, 131993L, 131993L, 
131993L, 131993L, 132139L, 132139L, 132139L, 132139L, 132139L, 
132139L, 132139L, 132139L, 132139L, 132139L, 132139L, 132139L, 
132139L, 132139L, 134584L, 134584L, 134584L, 134584L, 134584L, 
134584L, 134584L, 134584L, 134584L, 134584L, 134584L, 134584L, 
134584L, 134584L, 134584L, 134584L, 134584L, 134584L, 134584L, 
134584L, 134584L, 134584L, 134584L, 134584L, 134585L, 134585L, 
134585L, 134585L, 134585L, 134585L, 134585L, 134585L, 134585L, 
134585L, 134585L, 134585L, 134585L, 134585L, 134585L, 134585L, 
134585L, 136986L, 136986L, 136986L, 136986L, 136986L, 132252L, 
132252L, 132252L, 132252L, 132252L, 132252L, 132252L, 132252L, 
132252L, 132252L, 131880L, 131880L, 131880L, 131880L, 131880L, 
131880L, 131880L, 131880L, 131880L, 131880L, 136227L, 136227L, 
136227L, 136227L, 136227L, 136227L, 136227L, 136227L, 136227L, 
136227L, 136227L, 136356L, 136356L, 136356L, 136356L, 136356L, 
136356L, 136356L, 136356L, 136356L, 136356L, 136356L, 136356L, 
136356L, 136356L, 136356L, 141289L, 141289L, 141289L, 141289L, 
141289L, 141289L, 141289L, 141289L, 141289L, 141289L, 141289L, 
141289L, 141289L, 141289L, 139342L, 139342L, 139342L, 139342L, 
139342L, 131915L, 131915L, 131915L, 131915L, 139297L, 139297L, 
139297L, 139297L, 139297L, 139297L, 139297L, 139297L, 132175L, 
132175L, 132175L, 132175L, 134504L, 134504L, 134504L, 134504L, 
134504L, 134504L, 134504L, 134504L, 134504L, 134504L, 133320L, 
133320L, 133320L, 133320L, 133320L, 133320L, 133320L, 133320L, 
133320L, 133320L, 133320L, 133320L, 133320L, 133320L, 133320L, 
133320L, 133320L, 133320L, 139105L, 139105L, 139105L, 139105L, 
139105L, 139105L, 139105L, 139105L, 138050L, 138050L, 138050L, 
138050L, 138050L, 138050L, 138050L, 138050L, 138050L, 138050L, 
132216L, 132216L, 132216L, 132216L, 132216L, 132216L, 132216L, 
132217L, 132217L, 132217L, 132217L, 132217L, 132217L, 132217L, 
132217L, 131635L, 131635L, 131635L, 135792L, 135792L, 135792L, 
135792L, 135792L, 135792L, 135792L, 135792L, 135792L, 135792L, 
135792L, 137650L, 137650L, 137650L, 137650L, 137788L, 137788L, 
137788L, 138028L, 138028L, 138028L, 138028L, 139057L, 139057L, 
139057L, 139057L, 138497L, 138497L, 138497L, 138497L, 138497L, 
138497L, 138597L, 139581L, 139581L, 139581L, 139581L, 139581L, 
139581L, 139581L, 139581L, 139581L, 139581L, 139581L, 139581L, 
139581L, 131894L, 131894L, 131894L, 131894L, 131894L, 131894L, 
131894L, 132205L, 132205L, 132205L, 132205L, 132205L, 132205L, 
132205L, 132205L, 132205L, 135981L, 135981L, 135981L, 135981L, 
135981L, 135981L, 135981L, 135981L, 135981L, 136683L, 136683L, 
136683L, 136683L, 136683L, 136683L, 136683L, 136683L, 136683L, 
136683L, 136683L, 136683L, 136683L, 136683L, 136683L, 136683L, 
136683L, 136683L, 137268L, 137268L, 137268L, 137268L, 137268L, 
137268L, 137268L, 137268L, 137268L, 137268L, 137268L, 137285L, 
138087L, 138087L, 138087L, 138087L, 138087L, 138087L, 138087L, 
138087L, 138087L, 138970L, 138970L, 138970L, 138970L, 138970L, 
138970L, 138970L, 138970L, 138970L, 138970L, 138970L, 138970L, 
132084L, 132084L, 141281L, 141281L, 141281L, 141281L, 137391L, 
137391L, 137391L, 137391L, 137391L, 137391L, 137391L, 137391L, 
138285L, 138285L, 138285L, 138285L, 138285L, 138285L, 138285L, 
138285L, 138285L, 138285L, 138285L, 133381L, 133381L, 133381L, 
133381L, 133381L, 133381L, 133381L, 139269L, 139265L, 139265L, 
139265L, 139265L, 139265L, 139265L, 136803L, 136803L, 136803L, 
136803L, 136803L, 136803L, 136803L, 136803L, 136803L, 136803L, 
136803L, 137032L, 137032L, 137032L, 137032L, 137032L, 137032L, 
137032L, 137234L, 137234L, 137234L, 137234L, 137234L, 137234L, 
137234L, 137234L, 137234L, 137004L, 137004L, 137004L, 137004L, 
137004L, 137004L, 137004L, 137004L, 137004L, 138205L, 138205L, 
138205L, 139070L, 139070L, 139070L, 139070L, 139070L, 139070L, 
139070L, 139070L, 139070L, 139070L, 139351L, 139351L, 139351L, 
139351L, 139351L, 139351L, 139351L, 139351L, 139351L, 139351L, 
140813L, 140813L, 140813L, 140813L, 140813L, 140813L, 140813L, 
139479L, 139479L, 139479L, 139479L, 139479L, 139479L, 139479L, 
139479L, 140667L, 140667L, 140667L, 140667L, 140667L, 140667L, 
140667L, 140667L, 133291L, 133291L, 133291L, 133291L, 139465L, 
139465L, 139465L, 139465L, 139465L, 139465L, 139465L, 139465L, 
139465L, 141174L, 141174L, 141174L, 141174L, 141174L, 141174L, 
141174L, 141174L, 141174L, 141174L, 141174L, 141174L, 141174L, 
141174L, 141174L, 141174L, 141174L, 139325L, 139325L, 139325L, 
139325L, 139325L, 139325L, 139325L, 139325L, 139325L, 139325L, 
139325L, 139325L, 139325L, 139325L, 139325L, 138061L, 138061L, 
138061L, 138061L, 138061L, 138061L, 138061L, 138061L, 138061L, 
138061L, 138061L, 138061L, 138061L, 139143L, 139143L, 139143L, 
139143L, 139143L, 139143L, 139143L, 139143L, 139143L, 139143L, 
139143L, 139143L, 138671L, 138671L, 138671L, 138671L, 138671L, 
138671L, 138671L, 138671L, 138671L, 138671L, 138671L, 138671L, 
131970L, 131970L, 131970L, 131970L, 131970L, 131970L, 131970L, 
131970L, 136802L, 136802L, 136802L, 136802L, 136802L, 136405L, 
136405L, 136405L, 136405L, 136405L, 136405L, 136405L, 139156L, 
139156L, 139156L, 139156L), OtherLangDescription = c("frying Potato", 
"Flame Grape", "Zebdaya Mango", "Green pepper", "Molokhia", "White Eggplant ", 
"Golden Onion", "Zucchini", "Dill", "Parsley", "Coriander", "Local Eggplant", 
"Local Lemon", "Local Pear", "Flame Grape", "Superior Grape", 
"Capsicum", "Green pepper", "Cucumber", "White Eggplant ", "Carrot", 
"Tomato", "Zucchini", "Parsley", "Local Celery ", "frying Potato", 
"Flame Grape", "Superior Grape", "Momtaza Owais Mango", "Barshomi Figs", 
"Green pepper", "Cucumber", "Tomato", "Red Onion", "Parsley", 
"Flame Grape", "Momtaza Owais Mango", "Barshomi Figs", "Hot Pepper", 
"Green pepper", "Cucumber", "Red Onion", "Zucchini", "Parsley", 
"Momtaza Owais Mango", "Barshomi Figs", "Local Pear", "Superior Grape", 
"Momtaza Owais Mango", "Barshomi Figs", "Cucumber", "White Eggplant ", 
"Tomato", "Classic Eggplant", "Local Pear", "Tomato", "Dill", 
"Parsley", "Coriander", "Classic Eggplant", "frying Potato", 
"Flame Grape", "Cucumber", "Molokhia", "Cooking Potato", "Tomato", 
"Cantaloupe", " Green Beans", "Baladi Cabbage", "Local Lemon", 
"Classic Eggplant", "frying Potato", "Banati Grape", "Chili Pepper", 
"Green pepper", "Tomato", "Local Lemon", "Classic Eggplant", 
"Banati Grape", "Chili Pepper", "Green pepper", "Tomato", "Classic Eggplant", 
"frying Potato", "Local Pear", "Chili Pepper", "Green pepper", 
"White Eggplant ", "Black Eggplant", "Tomato", "Zaghlol Dates", 
"Local Lemon", "Baladi Garlic", "Zucchini", "Dill", "Parsley", 
"Red Globe Grape", "frying Potato", "Local Pear", "Flame Grape", 
"Fons Mango", "Barshomi Figs", "Cucumber", "Carrot", "Tomato", 
"Red Onion", "Dill", "Parsley", "Coriander", "Owais Mango", "Melon", 
"Sadeeka Mango", "Barshomi Figs", "Avocado", "Strawberry", "Sugary Peach", 
"Local Cucumber", "Imported Peach", "Guava", "Watermelon ( per KG)", 
"Syrian plum", "Watermelon ( per KG)", "Hot Pepper", "Watermelon ( per KG)", 
"Hot Pepper", "Cucumber", "White Eggplant ", "Grape Leaves", 
"Carrot", "Tomato", "Red Onion", "Sugary Peach", "Local Lemon", 
"Mushroom (200G) old", "Red Radish old", "Orange For Juice", 
"Watermelon ( per KG)", "Melon", "Flame Grape", "Chili Pepper", 
"Capsicum", "Carrot", "Tomato", "Broccoli", "Sugary Peach", "Local Cucumber", 
"Classic Eggplant", "frying Potato", "Capsicum", "Green pepper", 
"Golden Onion", "Tomato", "frying Potato", "Flame Grape", "Superior Grape", 
"Sadeeka Mango", "Capsicum", "Green pepper", "Black Eggplant", 
"Cooking Potato", "Carrot", "Tomato", "Local Cucumber", "Local Hot Pepper", 
"Capsicum", "Green pepper", "Grape Leaves", "Cooking Potato", 
"Tomato", "Cantaloupe", "Local Cucumber", "Japanese Plum", "Dill", 
"White Cabbage", "Coriander", "Arugula", "Local Apple", "Local Lemon", 
"Classic Eggplant", "Morket Tangerine", "Cooking Potato", "Red Onion", 
"Tamr hendi", "Deluxe Dried Dates", "Sokkary Mango", "Nems Watermelon (KG)", 
"Sugary Peach", "Red Globe Grape", "Owais Mango", "Lebanese Apple", 
"Orange For Juice", "Watermelon ( per KG)", "Golden Apple", "Tomato", 
"Sweet Potato", "Sokkary Mango", "Gala Apple", "Tomato", "Okra", 
"Mint", "Red Globe Grape", "Watermelon ( per KG)", "Flame Grape", 
"Hot Pepper", "Capsicum", "Tomato", "Local Cucumber", "Parsley", 
"Local Apple", "Local Lemon", "Flame Grape", "Superior Grape", 
"Parsley", "Local Apple", "Watermelon ( per KG)", "Cooking Potato", 
"Taro", "Cantaloupe", "Local Cucumber", "Dried Dates", "Local Apple", 
"Watermelon ( per KG)", "Golden Onion", "Sugary Peach", "Arugula old", 
"frying Potato", "Parsle old", "Coriander old", "Dill old", "White Cabbage Old", 
"Tomato", "Local Cucumber", "Orange For Juice", "Watermelon ( per KG)", 
"Red Onion", "Watermelon ( per KG)", "Nectarine Peach", "Granny Apple", 
"Apricot", "Carrot", "Tomato", "Red Onion", "Zucchini", "Parsle old", 
"Coriander old", "Dill old", "Flame Grape", "Capsicum", "Carrot", 
"Tomato", "Local Cucumber", "Zucchini", "Orange For Juice", "Golden Apple", 
"Golden Onion", "Cooking Potato", "Carrot", "Tomato", "Taro", 
"Local Lemon", "Cauliflower", "Classic Eggplant", "frying Potato", 
"Autumn Royal Grape", "Capsicum", "Green pepper", "Cucumber", 
"Tomato", "Red Globe Grape", "Local Lemon", "Classic Eggplant", 
"frying Potato", "Autumn Royal Grape", "Capsicum", "Green pepper", 
"Cucumber", "Tomato", "Red Globe Grape", "Watermelon ( per KG)", 
"Local Lemon", "Classic Eggplant", "frying Potato", "Flame Grape", 
"Tomato", "Red Onion", "Local Cucumber", "Superior Grape", "Sokkary Mango", 
"Sadeeka Mango", "Barshomi Figs", "Hot Pepper", "Chili Pepper", 
"Black Eggplant", "Red Globe Grape", "Syrian plum", "Local Lemon", 
"Classic Eggplant", "frying Potato", "Pumpkin", "Banati Grape", 
"Sokkary Mango", "Green pepper", "Cucumber", "Molokhia", "Golden Onion", 
"Carrot", "Tomato", "Red Onion", "Red Garlic", "Mint", "Arugula", 
"Parsle old", "Coriander old", "Dill old", "White Cabbage Old", 
"Capsicum", "Green pepper", "White Eggplant ", "Carrot", "Tomato", 
"Sugary Peach", "Mushroom (200G) old", "Arugula old", "Mint old", 
"Dessert Peach", "frying Potato", "Parsle old", "Green pepper", 
"Cucumber", "Tomato", "Red Onion", "Sugary Peach", "Classic Eggplant", 
"Sweet Potato", "frying Potato", "Sweet Apple", "Local Pear", 
"Flame Grape", "Fons Mango", "Naoumi Mango", "Barshomi Figs", 
"Green pepper", "Cucumber", "Molokhia", "White Eggplant ", "Golden Apple", 
"Carrot", "Tomato", "Cantaloupe", " Green Beans", "Red Globe Grape", 
"Syrian plum", "Crimson Grape", "Local Lemon", "frying Potato", 
"Flame Grape", "kiwi", "Capsicum", "Cherry Tomatoes", "Beet", 
"Cucumber", "Granny Apple", "Gala Apple", "Golden Onion", "Carrot", 
"Tomato", "Red Onion", "Sugary Peach", "Zucchini", "Dill", "Parsley", 
"Coriander", "Arugula", "Local Celery ", "Watermelon ( per KG)", 
"frying Potato", "Chili Pepper", "Beet", "Cucumber", "Apricot", 
"Dill", "Parsley", "Imported Leek", "Coriander", "Red Radish", 
"Arugula", "Local Celery ", "Local Hot Pepper", "Cauliflower", 
"Watermelon ( per KG)", "Flame Grape", "Fons Mango", "Naoumi Mango", 
"Cantaloupe", "Red Globe Grape", "Syrian Green Apple", "Mushroom (200G) old", 
"Arugula old", "Parsle old", "Cucumber", "Golden Onion", "Tomato", 
"Cantaloupe", "Zucchini", "Local Lemon", "Mushroom (200G) old", 
"Red Radish old", "Parsle old", "Avocado", "African Crimson Grape", 
"Green pepper", "Cucumber", "Zucchini", "Navel Orange", "Morket Tangerine", 
"Pineapple ( per KG)", "Watermelon ( per KG)", "kiwi", "African Crimson Grape", 
"African Pear", "Nectarine Peach", "Golden Apple", "Gala Apple", 
"Local Banana", "Orange For Juice", "Orange For Juice", "African Crimson Grape", 
"Strawberry", "coconut", "Dried Figs 400G Old", "Amar Eldain 400G Old", 
"ِArugula Rokla  Old", "Imported Banana", "Apricot", "Classic Eggplant", 
"Avocado", "Strawberry", "Green pepper", "Nectarine Peach", "Golden Apple", 
"Granny Apple", "Gala Apple", "Tomato", "Cantaloupe", "Florida Peach", 
"Imported Banana", "Morket Tangerine", "Watermelon ( per KG)", 
"Superior Grape", "kiwi", "Strawberry", "Golden Apple", "Granny Apple", 
"Gala Apple", "Cantaloupe", "Sugary Peach", "Imported Banana", 
"Apricot", "Hollywood Plum", "Japanese Plum", "Orange For Juice", 
"Dessert Peach", "Morket Tangerine", "Watermelon ( per KG)", 
"frying Potato", "Melon", "Flame Grape", "Superior Grape", "Capsicum", 
"Cucumber", "White Eggplant ", "Gala Apple", "Golden Onion", 
"Cooking Potato", "Carrot", "Tomato", "Broccoli", "Sugary Peach", 
"Imported Banana", "Apricot", "Hollywood Plum", "Baladi Garlic", 
"Japanese Plum", "Zucchini", "Classic Eggplant", "Orange For Juice", 
"Dessert Peach", "Morket Tangerine", "Watermelon ( per KG)", 
"Melon", "Cucumber", "Golden Onion", "Cooking Potato", "Tomato", 
"Broccoli", "Sugary Peach", "Apricot", "Hollywood Plum", "Baladi Garlic", 
"Japanese Plum", "Zucchini", "Imported Cherry", "Flame Grape", 
"Momtaza Owais Mango", "Zebdaya Mango", "Imported Banana", "Local Lemon", 
"Mushroom (200G) old", "Orange For Juice", "frying Potato", "Red Cabbage old", 
"Parsle old", "Dill old", "Tomato", "Golden Raisin", "Zucchini", 
"Mushroom (200G) old", "Arugula old", "Italian Basil Old", "Black Eggplant", 
"Golden Onion", "coconut", "Golden Raisin", "Raisin", "Green Latoga Lettuce  Old", 
"ِArugula Rokla  Old", "Classic Eggplant", "Superior Grape", 
"Capsicum", "Green pepper", "Cucumber", "Carrot", "Tomato", "Sugary Peach", 
"Dill", "Parsley", "Arugula", "Local Lemon", "Classic Eggplant", 
"Watermelon ( per KG)", "Hot Pepper", "Avocado", "Capsicum", 
"Green pepper", "Cucumber", "Golden Onion", "Cooking Potato", 
"Carrot", "Tomato", "Zucchini", "Mushroom (200G)", "Red Globe Grape", 
"Local Lemon", "Classic Eggplant", "Sweet Potato", "frying Potato", 
"Autumn Royal Grape", "Capsicum", "Cucumber", "Golden Onion", 
"Carrot", "Tomato", "Zucchini", "Mushroom (200G)", "White Cabbage", 
"Red Spanish plum", "Cucumber", "Cooking Potato", "Tomato", "Red Onion", 
"Crimson Grape", "Local Lemon", "Classic Eggplant", "Tomato", 
"Red Onion", "Sweet Potato", "frying Potato", "Barbary Figs", 
"Green pepper", "Cucumber", "Tomato", "Red Globe Grape", "Owais Mango", 
"Morket Tangerine", "Watermelon ( per KG)", "Cherry Tomatoes", 
"Sugary Peach", "Dessert Peach", "frying Potato", "Superior Grape", 
"Tomato", "Red Onion", "Cantaloupe", "Sugary Peach", "Imported Banana", 
"Hollywood Plum", "Japanese Plum", "Local Lemon", "Mushroom (200G) old", 
"Watermelon ( per KG)", "Parsle old", "Dill old", "Hot Pepper", 
"Avocado", "Green pepper", "Cucumber", "Golden Apple", "Carrot", 
"Tomato", "Cantaloupe", "Tamr hendi", "sobya 1/4KG", "Hibiscus", 
"Imported Banana", "Apricot", "Watermelon ( per KG)", "frying Potato", 
"Ismalilia Fass Mango", "Cucumber", "Golden Onion", "Tomato", 
"Cantaloupe", "Arugula", "Local Lemon", "frying Potato", "Superior Grape", 
"Cucumber", "Golden Onion", "Carrot", "Tomato", "Red Onion", 
"Arugula", "Yasmina Mango", "Local Lemon", "Mushroom (200G) old", 
"Arugula old", "Red Cabbage old", "Capsicum", "Green pepper", 
"Grape Leaves", "Orange For Juice", "Morket Tangerine", "frying Potato", 
"Superior Grape", "Golden Apple", "Tomato", "Deluxe Dried Dates", 
"Japanese Plum", "Morket Tangerine", "Watermelon ( per KG)", 
"Sugary Peach", "Classic Eggplant", "Melon", "White Cabbage Old", 
"Flame Grape", "Superior Grape", "Golden Apple", "Tomato", "Red Onion", 
"Apricot", "Zucchini", "Iceberg Lettuce", "Flame Grape", "Sokkary Mango", 
"Zebdaya Mango", "Yasmina Mango", "Sokkary Mango", "Zebdaya Mango", 
"Yasmina Mango", "Watermelon ( per KG)", "Local Pear", "Flame Grape", 
"Sokkary Mango", "Banati Grape", "Fons Mango", "Zebdaya Mango", 
"Crimson Grape", "Imported Peach", "Flame Grape", "Zebdaya Mango", 
"Naoumi Mango", "Gala Apple", "Cantaloupe", "Cooking Potato", 
"Local Lemon", "Barhi Dates", "Fons Mango", "Green pepper", "Beet", 
"Cucumber", "Golden Onion", "Cooking Potato", "Tomato", "Zucchini", 
"Mint", "Baladi Cabbage", "Kathaa", "Classic Eggplant", "Mint old", 
"Green pepper", "Cucumber", "Golden Onion", "Cooking Potato", 
"Carrot", "Local Lemon", "Imported Apricot", "Hot Pepper", "Tomato", 
"Red Onion", "Cantaloupe", "package of Celery", "Local Cucumber", 
"Japanese Plum", "Local Lemon", "Classic Eggplant", "Melon", 
"Superior Grape", "Capsicum", "Cucumber", "Golden Onion", "Tomato", 
"Iceberg Lettuce", "Local Lemon", "Classic Eggplant", "Imported Cherry", 
"Flame Grape", "Chili Pepper", "Green pepper", "Cucumber", "Molokhia", 
"Black Eggplant", "Carrot", "Tomato", "Zucchini", "Mint", "Dill", 
"Parsley", "Coriander", "Arugula", "Local Eggplant", "Orange For Juice", 
"Imported Cherry", "Flame Grape", "Barshomi Figs", "Cucumber", 
"Golden Onion", "Tomato", "Dill", "Parsley", "Coriander", "Arugula", 
"Orange For Juice", "Local Lemon", "Flame Grape", "Momtaza Owais Mango", 
"Barshomi Figs", "Chili Pepper", "Cucumber", "Golden Onion", 
"Tomato", "Arugula", "Classic Eggplant", "Fons Mango", "Beet", 
"Cucumber", "Cooking Potato", "Tomato", "Dill", "Parsley", "Coriander", 
"Red Globe Grape", "Baladi Cabbage", "Kathaa", "coconut", "Deluxe Dried Dates", 
"Barhi Dates", "kiwi", "Golden Apple", "Imported Banana", "Classic Eggplant", 
"frying Potato", "Flame Grape", "Cucumber", "Black Eggplant", 
"Golden Onion", "Tomato", "Red Globe Grape", "frying Potato", 
"Flame Grape", "Sokkary Mango", "Cucumber", "Cooking Potato", 
"Tomato", "Red Onion", "Okra", "Zucchini", "Parsley", "Coriander", 
"Mushroom (200G) old", "Watermelon ( per KG)", "Flame Grape", 
"Cucumber", "Tomato", "Apricot", "Deluxe Dried Dates", "Crimson Grape", 
"Pomegranate", "Local Pear", "Cucumber", "Carrot", "Arugula", 
"Owais Mango", "Watermelon ( per KG)", "frying Potato", "Golden Onion", 
"Cooking Potato", "Tomato", "Local Cucumber", "Zucchini", "Mint", 
"Parsley", "Arugula", "Local Apple", "Local Lemon", "Zebdaya Mango", 
"Sadeeka Mango", "Capsicum", "Green pepper", "Sugary Peach", 
"Local Apple", "Local Lemon", "Flame Grape", "Momtaza Owais Mango", 
"Sokkary Mango", "Capsicum", "Cucumber", "Golden Onion", "Carrot", 
"Local Apple", "Classic Eggplant", "frying Potato", "Flame Grape", 
"Superior Grape", "Sadeeka Mango", "Green pepper", "Cucumber", 
"Tomato", "Local Apple", "Watermelon ( per KG)", "Local Pear", 
"Sokkary Mango", "Sweet Potato", "Watermelon ( per KG)", "frying Potato", 
"Green pepper", "Cucumber", "Golden Onion", "Tomato", "Red Onion", 
"Baladi Garlic", "Arugula", "Watermelon ( per KG)", "frying Potato", 
"Barbary Figs", "Barshomi Figs", "Beet", "Tomato", "Mint", "Arugula", 
"Owais Mango", "Lebanese Apple", "Zebdaya Mango", "kiwi", "Sugary Peach", 
"Zucchini", "Mushroom (200G)", "Iceberg Lettuce", "Crimson Grape", 
"Local Lemon", "Sweet Potato", "Watermelon ( per KG)", "Cucumber", 
"Golden Onion", "Zucchini", "Iceberg Lettuce", "Lebanese Apple", 
"Barshomi Figs", "Hot Pepper", "Cucumber", "Carrot", "Sugary Peach", 
" Green Beans", "Mushroom (200G)", "Local Celery ", "frying Potato", 
"Superior Grape", "Sugary Peach", "Local Cucumber", "Local Pear", 
"Golden Apple", "Tomato", "Imported Banana", "Dill", "Parsley", 
"Arugula", "Red Globe Grape", "Owais Mango", "Imported Peach", 
"Kaka", "Local Pear", "Banati Grape", "Barshomi Figs", "Capsicum", 
"Cucumber", "Tomato", "Cantaloupe", "Imported Banana", "Dill", 
"Parsley", "Owais Mango", "Kathaa", "Crimson Grape", "Spanish Plum", 
"Red Spanish plum", "Imported Peach", "Imported Apricot", "Local Pear", 
"Banati Grape", "Barbary Figs", "Capsicum", "Green pepper", "Cucumber", 
"Carrot", "Mint", "Parsley", "Arugula", "Local Celery ", "Syrian plum", 
"Crimson Grape", "Melon", "Local Pear", "Flame Grape", "Superior Grape", 
"Sadeeka Mango", "Barshomi Figs", "kiwi", "Capsicum", "Cucumber", 
"Golden Apple", "Red Onion", "Imported Banana", "Syrian plum", 
"Ismalilia Fass Mango", "Zebdaya Mango", "Barshomi Figs", "Tomato", 
"Red Onion", "Cantaloupe", "Imported Banana", "Dill", "Coriander", 
"Arugula", "Owais Mango", "Crimson Grape", "Melon", "Capsicum", 
"Cucumber", "Granny Apple", "Tomato", "Red Onion", "Imported Banana", 
"Dill", "Parsley", "Coriander", "Arugula", "Owais Mango", "Orange For Juice", 
"Morket Tangerine", "Capsicum", "Tomato", "Cantaloupe", "Florida Peach", 
"Imported Banana", "Dried Dates", "Watermelon ( per KG)", "Melon", 
"Tomato", "Sugary Peach", "Local Cucumber", "Watermelon ( per KG)", 
"Melon", "Superior Grape", "Beet", "Cucumber", "Carrot", "Sugary Peach", 
"Local Pear", "Barshomi Figs", "Golden Apple", "Cantaloupe")), .Names = c("WHWorkOrderHeaderId", 
"OtherLangDescription"), class = "data.frame", row.names = c(NA, 
-1000L))
prodList <- unique(New$OtherLangDescription) 
prodListId <- lapply(prodList, function(x) subset(New, OtherLangDescription == x)$WHWorkOrderHeaderId) 
prodListProduct <- lapply(prodListId, function(x) subset(New, WHWorkOrderHeaderId %in% x)$OtherLangDescription) 
prodListTable <- mapply(function(Product, ID) table(Product)/length(ID),
                        prodListProduct, prodListId) 
colnames(prodListTable) <- prodList

1 个答案:

答案 0 :(得分:1)

首先,在理论方面,你所描述的情况(A&amp; B从不在同一时间在面板中)不会给你一个空关联但是相关性。实际上,每当A = 1,B = 0并且每当B = 1,A = 0,则出现反相关。相关性不一定等于-1,因为您显然会遇到A = B = 0的情况(除非客户总是购买A和B这是不现实的)。

另一方面,在很少购买A和B的情况下,您可能会遇到几乎无关的相关性(这意味着多次出现A = B = 0)。

至于如何编码,您可以使用model.matrix将数据转换为虚拟变量,aggregateWOrderId收集面板内容。以下是使用您的结构的示例。

mydf <- data.frame(WOrderId = c(c(1:8),c(1:8)), 
                   OtherLangDescription = c(rep("Water",4),rep("Milk",4),
                     rep("Apple",2), rep("Beef",2), rep("Candy",2), rep("Apple",2)))
mydf$OtherLangDescription <- as.factor(mydf$OtherLangDescription)
dummydf <- model.matrix(WOrderId ~ OtherLangDescription, mydf, 
                             contrasts.arg = list(OtherLangDescription = contrasts(mydf$OtherLangDescription, 
                                                      contrasts = F)))
fulldf <- data.frame(mydf$WOrderId, dummydf[,2:ncol(dummydf)])
agregdf <- aggregate(. ~ mydf.WOrderId, fulldf, sum)
cor(agregdf[,2:ncol(agregdf)])

提供以下输出。

                          OtherLangDescriptionApple OtherLangDescriptionBeef OtherLangDescriptionCandy
OtherLangDescriptionApple                 1.0000000               -0.5773503                -0.5773503
OtherLangDescriptionBeef                 -0.5773503                1.0000000                -0.3333333
OtherLangDescriptionCandy                -0.5773503               -0.3333333                 1.0000000
OtherLangDescriptionMilk                  0.0000000               -0.5773503                 0.5773503
OtherLangDescriptionWater                 0.0000000                0.5773503                -0.5773503
                          OtherLangDescriptionMilk OtherLangDescriptionWater
OtherLangDescriptionApple                0.0000000                 0.0000000
OtherLangDescriptionBeef                -0.5773503                 0.5773503
OtherLangDescriptionCandy                0.5773503                -0.5773503
OtherLangDescriptionMilk                 1.0000000                -1.0000000
OtherLangDescriptionWater               -1.0000000                 1.0000000

正如您所看到的,由于苹果从未与牛肉或糖果在同一面板中,因此相关性为负。牛肉与水有“很大”的相关性,因为每当你吃牛肉时水总是存在。

请注意,苹果和牛奶的相关性为零(苹果和水相同),因为购买苹果的时间有一半,也会购买水。因此,零相关并不意味着产品是反相关的(它们只是不相关的)。

编辑根据评论,您不是计算相关性,而是计算每个产品的条件概率。

这是实现此计算的一种方法。

prodList <- unique(mydf$OtherLangDescription) # get distinct list of products
prodListId <- lapply(prodList, function(x) subset(mydf, OtherLangDescription == x)$WOrderId) # get list of orders in which each product appears
prodListProduct <- lapply(prodListId, function(x) subset(mydf, WOrderId %in% x)$OtherLangDescription) # get products appearing in those orders
prodListTable <- mapply(function(Product, ID) table(Product)/length(ID),
                         prodListProduct, prodListId) # divide occurrences of products by number of orders to get conditional probabilities
colnames(prodListTable) <- prodList
相关问题