简体   繁体   中英

Issue in creating contingency table in R

I am using ISLR package for my statistics practice. I am using OJ dataset. I am trying to create a contingency table for Purchase column and specialPrice columns for weach of the population.

I am trying to find the likelihood of CH being sold if there is a special price.

Here is my code so far.

library(ISLR)
CH <- table(OJ[OJ$Purchase == 'CH', "SpecialCH"])
MM <- table(OJ[OJ$Purchase == 'MM', "SpecialMM"])
table (MM, CH)

The out put that I get is a bit weird.

     CH
MM    121 532
  101   1   0
  316   0   1

I am trying to find the odds ration and eventually apply McNemar's test. But I am unable to generate the contingency table. I can do it by hand but need to do it in R.

You are trying to work with 3 variables, but a contingency table only uses 2. I recommend using xtabs since the formula method saves some typing and it does a better job of labeling the table:

xtabs(~SpecialMM+SpecialCH, OJ)  # Only 4 weeks are both on special
#          SpecialCH
# SpecialMM   0   1
#         0 743 154
#         1 169   4 
xtabs(~Purchase+SpecialCH, OJ)   # When CH is on special ca 75% CH
#         SpecialCH
# Purchase   0   1
#       CH 532 121
#       MM 380  37
# xtabs(~Purchase+SpecialMM, OJ)  # When MM is on special ca 58% MM
#         SpecialMM
# Purchase   0   1
#       CH 581  72
#       MM 316 101

The first table asks the question. Are specials for one brand associated with the other brand. There are 1070 purchases of OJ represented. CH was on special 158 times and MM was on special 173 times. But only 4 times are both brands on special. This table suggests that MM and CH are not on special at the same time. You could use Chi Square or another test to see if that is a significant deviation from random assignment of specials.

The second and third tables look at purchase of OJ to see if one brand is more likely to be purchased relative to the other brand when it is on sale. Notice that most OJ purchases occur when neither is on sale, but it could be that sales boost the purchase of the brand on sale. Again the statistical tests would tell you if this could just be random chance or unlikely to be chance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM