My Data如下所示,其中DFD是我的数据帧。
DFD
Names BP jobcode bp_Category
1 A 100 Doctor low_BP
2 B 150 Doctor Medium_BP
3 C 200 Engineer High_BP
4 D 110 Engineer low_BP
5 E 160 Student Medium_BP
以下是我如何获得每个职务代码中患有低、高和中等BP的百分比,如下所示。
tabLE<-table(DFD$bp_Category,DFD$jobcode)
> prop.table(tabLE,2)*100
Doctor Engineer Student
low_BP 50 50 0
Medium_BP 50 0 100
High_BP 0 50 0
我想问一下,如何以及通过哪种统计测试,我可以看到所有三个bp_categories的三个作业代码之间的显著差异。例如,我想看看工程师在博士和学生中Medium_BP的比例是否显著最高??
Data
Names<-c("A","B","C","D","E")
BP<-c(100,150,200,110,160)
jobcode<-c("Doctor","Doctor","Engineer","Engineer","Student")
jobcode<-ordered(jobcode)
DFD<-data.frame(Names,BP,jobcode)
DFD$bp_Category[DFD$BP<140]<-"low_BP"
DFD$bp_Category[DFD$BP<170 & DFD$BP>140]<-"Medium_BP"
DFD$bp_Category[DFD$BP<201 & DFD$BP>170]<-"High_BP"
DFD$bp_Category<-ordered(DFD$bp_Category, levels = c("low_BP","Medium_BP","High_BP"))
tabDFD <- with(DFD, table(DFD$bp_Category,DFD$jobcode))
tabLE<-table(DFD$bp_Category,DFD$jobcode)
prop.table(tabLE,2)*100
使用模拟数据集,其中BP和职业之间的比例或多或少相等:
set.seed(111)
DFD = data.frame(jobcode = sample(c("Doctor","Engineer","Student"),10000,replace=TRUE),
bp_Category = sample(c("low_BP","Medium_BP","High_BP"),10000,replace=TRUE)
)
由于这是在null下模拟的,您可以看到它大约为所有的33%
tabDFD <- with(DFD, table(DFD$bp_Category,DFD$jobcode))
tabLE<-table(DFD$bp_Category,DFD$jobcode)
prop.table(tabLE,2)*100
Doctor Engineer Student
High_BP 32.81156 33.89058 32.96930
low_BP 33.68453 32.73556 33.82527
Medium_BP 33.50391 33.37386 33.20543
我们可以对每一行进行卡方测试,但我们需要知道医生、工程师和学生的预期比例,所以我们得到了这个:
probs = colSums(tabLE)/sum(tabLE)
然后,对于每一行,我们测试每个单元格与我们预期的偏离程度:
library(broom)
library(purrr)
results = split(as.matrix(tabLE),rownames(tabLE)) %>%
map_dfr(~tidy(chisq.test(.x,p=probs)),.id="BP")
results
# A tibble: 3 x 5
BP statistic p.value parameter method
<chr> <dbl> <dbl> <dbl> <chr>
1 High_BP 0.676 0.713 2 Chi-squared test for given probabilities
2 low_BP 0.697 0.706 2 Chi-squared test for given probabilities
3 Medium_BP 0.0451 0.978 2 Chi-squared test for given probabilities