@@ -14,6 +14,7 @@ ttest<-setClass(
14
14
params.mtc = ' entity.stato' ,
15
15
params.factor_names = ' entity' ,
16
16
params.paired = ' entity' ,
17
+ params.paired_factor = ' character' ,
17
18
# OUTPUTS
18
19
outputs.t_statistic = ' entity.stato' ,
19
20
outputs.p_value = ' entity' ,
@@ -89,6 +90,36 @@ setMethod(f="method.apply",
89
90
if (length(L )!= 2 ) {
90
91
stop(' must have exactly two levels for this implmentation of t-statistic' )
91
92
}
93
+
94
+ if (M $ paired ){
95
+ # check that we have a pair for each sample,
96
+ # if not then remove
97
+ u = unique(D $ sample_meta [[M $ paired_factor ]])
98
+ out = character (0 ) # list of sample_id to remove
99
+ for (k in u ) {
100
+ n = sum(D $ sample_meta [[M $ paired_factor ]]== k )
101
+ if (n < 2 ) {
102
+ out = c(out ,k )
103
+ }
104
+
105
+ }
106
+ X = X [! (D $ sample_meta [[M $ paired_factor ]] %in% out ),]
107
+ y = y [! (D $ sample_meta [[M $ paired_factor ]] %in% out )]
108
+
109
+ # sort the data by sample id so that theyre in the right order for paired ttest
110
+ X = apply(X ,2 ,function (x ) {
111
+ a = x [y == L [1 ]]
112
+ b = x [y == L [2 ]]
113
+ ay = y [y == L [1 ]]
114
+ by = y [y == L [2 ]]
115
+ a = a [order(ay )]
116
+ b = b [order(by )]
117
+ return (c(a ,b ))
118
+ })
119
+ # also make sure the meta data is in the right order
120
+ y = y [order(D $ sample_meta [[M $ paired_factor ]])]
121
+ }
122
+
92
123
output = apply(X ,2 ,function (x ) {a = unlist(t.test(x [y == L [1 ]],x [y == L [2 ]],paired = M $ paired )[c(" statistic" ," p.value" ," parameter" ,' conf.int' ,' estimate' )])})
93
124
output = as.data.frame(t(output ),stringsAsFactors = FALSE )
94
125
output $ p.value = p.adjust(output $ p.value ,method = param.value(M ,' mtc' ))
@@ -98,8 +129,14 @@ setMethod(f="method.apply",
98
129
output.value(M ,' significant' )= output $ p.value < param.value(M ,' alpha' )
99
130
M $ conf_int = output [,4 : 5 ,drop = FALSE ]
100
131
colnames(M $ conf_int )= c(' lower' ,' upper' )
101
- M $ estimates = output [,6 : 7 ,drop = FALSE ]
102
- colnames(M $ estimates )= as.character(interaction(' estimate.mean' ,L ))
132
+ if (M $ paired ) {
133
+ M $ estimates = output [,6 ,drop = FALSE ]
134
+ colnames(M $ estimates )= ' estimated_mean_difference'
135
+ } else {
136
+ M $ estimates = output [,6 : 7 ,drop = FALSE ]
137
+ colnames(M $ estimates )= as.character(interaction(' estimate.mean' ,L ))
138
+ }
139
+
103
140
return (M )
104
141
}
105
142
)
0 commit comments