reportExample.Rmd
library(knitr)
library(ggplot2)
library(RColorBrewer)
library(reshape2)
library(formattable)
library(rms)
library(htmlTable)
#source('D:/source/cqsR/R/summaryTableFunctions.R')
#devtools::source_url("https://raw.github.com/slzhao/cqsR/master/R/summaryTableFunctions.R")
library(cqsR)
set.seed(123)
rawData<-matrix(nrow=200,ncol=10,rnorm(200*10))
row.names(rawData)<-paste0("Sample",1:200)
colnames(rawData)<-paste0("Feature",1:10)
rawData<-data.frame(rawData,FeatureYN1=sample(c(rep(0,100),rep(1,100)),200),FeatureYN2=as.character(sample(c(rep(0,100),rep(1,100)),200)),FeatureGroup1=as.character(sample(c(rep("Pre/Group1",100),rep("Post/Group2",100)),200)),FeatureCategory1=sample(c("D","E"),200,replace = TRUE),FeatureCategory2=sample(c("A","B","C","D"),200,replace = TRUE),stringsAsFactors = TRUE)
#make some NAs
rawData[c(30:40),"Feature6"]=NA
rawData[c(35:70),"Feature7"]=NA
rawData[c(1:6),"FeatureYN2"]=NA
tableOut=summaryTable(rawData,varCols=c("FeatureYN2","Feature6","FeatureCategory1"),varColsPaired=c("FeatureYN1","Feature7","FeatureCategory2"),pairedTest = TRUE)
printSummaryTable(tableOut)
N | Pre (200) | Post (200) | Test Statistic | |
---|---|---|---|---|
FeatureYN2 (Pre) vs FeatureYN1 (Post) |
McNemar’s chi-squared=0.01; P=0.92 | |||
0 | 51% (196) | 51% (99) | 50% (97) | |
1 | 49% (192) | 49% (95) | 50% (97) | |
Feature6 (Pre) vs Feature7 (Post) |
Feature6 (189); Feature7 (164); Both=159 | -0.673 -0.044 0.582 (0.036±1.055) | -0.776 -0.072 0.622 (-0.095±0.985) | V=6911 ; P=0.344 |
FeatureCategory1 (Pre) vs FeatureCategory2 (Post) |
||||
A | 12% (46) | 0% (0) | 23% (46) | |
B | 18% (72) | 0% (0) | 36% (72) | |
C | 10% (40) | 0% (0) | 20% (40) | |
D | 33% (133) | 46% (91) | 21% (42) | |
E | 27% (109) | 55% (109) | 0% (0) | |
For categorical variable, numbers after proportions are counts; For continuous variable, a b c (x±s). a b c represent the lower quartile a, the median b, and the upper quartile c in different categories. x±s represents Mean±SD. Tests used: McNemar’s chi-squared test for symmetry of rows and columns in a two-dimensional contingency table; Fisher’s exact test for categorical variable; Wilcoxon Signed Rank Test for continuous variable; |
tableOut=summaryTable(rawData,groupCol="FeatureGroup1",varCols=c("FeatureYN2","Feature6","FeatureCategory2"),pairedTest = TRUE)
## Will perform paired test, Please confirm Samples were correctly matched/paired by groupCol.
N | Post/Group2 (100) | Pre/Group1 (100) | Test Statistic | |
---|---|---|---|---|
FeatureYN2 |
McNemar’s chi-squared=0; P=1 | |||
0 | 51% (97) | 50% (48) | 51% (49) | |
1 | 49% (95) | 50% (48) | 49% (47) | |
Feature6 |
Post/Group2 (93); Pre/Group1 (96); Both=93 | -0.651 0.125 0.916 (0.093±1.112) | -0.653 -0.071 0.535 (0.009±0.948) | V=2459 ; P=0.296 |
FeatureCategory2 |
McNemar’s chi-squared=4.51; P=0.608 | |||
A | 23% (46) | 26% (26) | 20% (20) | |
B | 36% (72) | 36% (36) | 36% (36) | |
C | 20% (40) | 18% (18) | 22% (22) | |
D | 21% (42) | 20% (20) | 22% (22) | |
For categorical variable, numbers after proportions are counts; For continuous variable, a b c (x±s). a b c represent the lower quartile a, the median b, and the upper quartile c in different categories. x±s represents Mean±SD. Tests used: McNemar’s chi-squared test for symmetry of rows and columns in a two-dimensional contingency table; Fisher’s exact test for categorical variable; Wilcoxon Signed Rank Test for continuous variable; |
##
## McNemar's Chi-squared test with continuity correction
##
## data: matrixForTest
## McNemar's chi-squared = 0.010204, df = 1, p-value = 0.9195
#Second table
dataOneGroup1=as.character(rawData[which(rawData$FeatureYN1==0),"FeatureYN2"])
dataOneGroup2=as.character(rawData[which(rawData$FeatureYN1==1),"FeatureYN2"])
matrixForTest=table(dataOneGroup1,dataOneGroup2)
mcnemar.test(matrixForTest)
##
## McNemar's Chi-squared test with continuity correction
##
## data: matrixForTest
## McNemar's chi-squared = 0, df = 1, p-value = 1
tableOut=summaryTable(rawData,varCols=c("FeatureYN2","Feature6","FeatureCategory1"),varColsPaired=c("FeatureYN1","Feature7","FeatureCategory2"))
printSummaryTable(tableOut)
N | Group 1 (200) | Group 2 (200) | Test Statistic | |
---|---|---|---|---|
FeatureYN2 (Group 1) vs FeatureYN1 (Group 2) |
X-squared=0.01; P=0.917 | |||
0 | 51% (199) | 51% (99) | 50% (100) | |
1 | 49% (195) | 49% (95) | 50% (100) | |
Feature6 (Group 1) vs Feature7 (Group 2) |
Feature6 (189); Feature7 (164) | -0.653 -0.018 0.591 (0.049±1.027) | -0.785 -0.091 0.621 (-0.095±0.994) | W=16494 ; P=0.298 |
FeatureCategory1 (Group 1) vs FeatureCategory2 (Group 2) |
X-squared=285.05; P<0.001 | |||
A | 12% (46) | 0% (0) | 23% (46) | |
B | 18% (72) | 0% (0) | 36% (72) | |
C | 10% (40) | 0% (0) | 20% (40) | |
D | 33% (133) | 46% (91) | 21% (42) | |
E | 27% (109) | 55% (109) | 0% (0) | |
For categorical variable, numbers after proportions are counts; For continuous variable, a b c (x±s). a b c represent the lower quartile a, the median b, and the upper quartile c in different categories. x±s represents Mean±SD. Tests used: Chi-squared test for categorical variable; Non-Paired Wilcoxon Rank Sum Test for continuous variable; |
tableOut=summaryTable(rawData,groupCol="FeatureGroup1",varCols=c("FeatureYN2","Feature6","FeatureCategory2"))
printSummaryTable(tableOut)
N | Post/Group2 (100) | Pre/Group1 (100) | Test Statistic | |
---|---|---|---|---|
FeatureYN2 |
X-squared=0.02; P=0.888 | |||
0 | 51% (99) | 50% (48) | 52% (51) | |
1 | 49% (95) | 50% (48) | 48% (47) | |
Feature6 |
Post/Group2 (93); Pre/Group1 (96) | -0.651 0.125 0.916 (0.093±1.112) | -0.684 -0.071 0.541 (0.007±0.941) | W=4771 ; P=0.415 |
FeatureCategory2 |
X-squared=1.28; P=0.734 | |||
A | 23% (46) | 26% (26) | 20% (20) | |
B | 36% (72) | 36% (36) | 36% (36) | |
C | 20% (40) | 18% (18) | 22% (22) | |
D | 21% (42) | 20% (20) | 22% (22) | |
For categorical variable, numbers after proportions are counts; For continuous variable, a b c (x±s). a b c represent the lower quartile a, the median b, and the upper quartile c in different categories. x±s represents Mean±SD. Tests used: Chi-squared test for categorical variable; Non-Paired Wilcoxon Rank Sum Test for continuous variable; |
outVar="FeatureGroup1"
varForTable1=c("FeatureYN2","Feature6","FeatureCategory2")
formulaForTable<-as.formula(paste0(paste(varForTable1, collapse=" + "),"~",outVar))
s <- summaryM(formulaForTable, data=rawData,
overall=TRUE, test=TRUE)
html(s, exclude1=FALSE, what=c('%'),digits=3, prmsd=TRUE)
Descriptive Statistics (N=200). | |||||
N |
Post/Group2 N=100 |
Pre/Group1 N=100 |
Combined N=200 |
Test Statistic |
|
---|---|---|---|---|---|
FeatureYN2 : 0 | 194 | 50% (48) | 52% (51) | 51% (99) | χ21=0.08, P=0.7761 |
1 | 50% (48) | 48% (47) | 49% (95) | ||
Feature6 | 189 | -0.65051 0.12503 0.91599 ( 0.09264 ± 1.11226) | -0.68399 -0.07066 0.54079 ( 0.00706 ± 0.94135) | -0.65277 -0.01798 0.59106 ( 0.04917 ± 1.02714) | F1 187=0.67, P=0.4162 |
FeatureCategory2 : A | 200 | 26% (26) | 20% (20) | 23% (46) | χ23=1.28, P=0.7341 |
B | 36% (36) | 36% (36) | 36% (72) | ||
C | 18% (18) | 22% (22) | 20% (40) | ||
D | 20% (20) | 22% (22) | 21% (42) | ||
a b c represent the lower quartile a, the median b, and the upper quartile c for continuous variables. x ± s represents X ± 1 SD. N is the number of non-missing values. Numbers after proportions are frequencies. Tests used: 1Pearson test; 2Wilcoxon test . |
temp<-summaryTableContinus(rawData,variables=c("Feature1","Feature2","FeatureYN1","FeatureCategory2"),groupVariable="Feature3")
printSummaryTableContinus(temp)
N | Variable | Test Statistic | |
---|---|---|---|
Feature1 |
200 | -0.02 | S=1357978, P=0.795 |
Feature2 |
200 | -0.04 | S=1382684, P=0.602 |
FeatureYN1 |
200 | X2=0.98, P=0.321 | |
0 | 100 | -0.611 -0.004 0.581 (-0.01±0.894) | |
1 | 100 | -0.49 0.173 0.743 (0.073±1.034) | |
FeatureCategory2 |
200 | X2=6.92, P=0.075 | |
A | 46 | -0.821 -0.244 0.315 (-0.26±0.977) | |
B | 72 | -0.49 0.173 0.675 (0.047±0.94) | |
C | 40 | -0.491 0.339 0.875 (0.161±1.029) | |
D | 42 | -0.281 0.13 0.711 (0.202±0.89) | |
One continuous and one categorical variables: a b c (x±s). a b c represent the lower quartile a, the median b, and the upper quartile c for continuous variable in different categories. x±s represents X±SD. Two continuous variables: a. a represents the rho statistic of Spearman’s correlation analysis. Tests used: Kruskal-Wallis test for one continuous and one categorical variables; Spearman correlation for two continuous variables. |