Skip to content

Commit 6c6dae1

Browse files
committed
change outputs to data.frame
for consistency with other objects
1 parent 6d4b729 commit 6c6dae1

File tree

5 files changed

+158
-118
lines changed

5 files changed

+158
-118
lines changed

R/ttest_class.R

Lines changed: 47 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ ttest = function(
4343
dof='entity',
4444
significant='entity',
4545
conf_int='entity',
46-
estimates='data.frame'
46+
estimates='entity'
4747
),
4848
prototype = list(name='t-test',
4949
description=paste0('A t-test compares the means of two factor levels. ',
@@ -85,12 +85,12 @@ ttest = function(
8585

8686
t_statistic=entity(name='t-statistic',
8787
ontology='STATO:0000176',
88-
type='numeric',
88+
type='data.frame',
8989
description='the value of the calculate statistics which is converted to a p-value when compared to a t-distribution.'
9090
),
9191
p_value=entity(name='p value',
9292
ontology='STATO:0000175',
93-
type='numeric',
93+
type='data.frame',
9494
description='the probability of observing the calculated t-statistic.'
9595
),
9696
dof=entity(name='degrees of freedom',
@@ -100,7 +100,7 @@ ttest = function(
100100
),
101101
significant=entity(name='Significant features',
102102
#ontology='STATO:0000069',
103-
type='logical',
103+
type='data.frame',
104104
description='TRUE if the calculated p-value is less than the supplied threhold (alpha)'
105105
),
106106
conf_int=entity(name='Confidence interval',
@@ -113,6 +113,11 @@ ttest = function(
113113
type='numeric',
114114
value=0.95,
115115
max_length = 1
116+
),
117+
estimates=entity(
118+
name = 'Estimates',
119+
description = 'The group means estimated when computing the t-statistic.',
120+
type='data.frame'
116121
)
117122
)
118123
)
@@ -134,64 +139,50 @@ setMethod(f="model_apply",
134139

135140
L=levels(y)
136141
if (length(L)!=2) {
137-
stop('must have exactly two levels for this implmentation of t-statistic')
142+
stop('must have exactly two levels for this implementation of t-statistic')
138143
}
139144

140-
estimate_name='estimate'
141-
if (M$paired){
142-
# check that we have a pair for each sample,
143-
# if not then remove
144-
u=unique(D$sample_meta[[M$paired_factor]])
145-
out=character(0) # list of sample_id to remove
146-
for (k in u) {
147-
n=sum(D$sample_meta[[M$paired_factor]]==k) # number of samples (could be same class)
148-
if (n<2) {
149-
out=c(out,k)
150-
}
151-
# if we have more than 2 then we need an even number.
152-
if (n%%2 != 0) {
153-
out=c(out,k)
154-
}
155-
# check we have enough groups (must be two for ttest)
156-
ng=length(unique(D$sample_meta[[M$factor_names]][D$sample_meta[[M$paired_factor]]==k]))
157-
if (ng != 2) {
158-
out=c(out,k)
159-
}
160-
161-
}
162-
#D$data=D$data[!(D$sample_meta[[M$paired_factor]] %in% out),]
163-
#D$sample_meta=D$sample_meta[!(D$sample_meta[[M$paired_factor]] %in% out),]
164-
D=D[!(D$sample_meta[[M$paired_factor]] %in% out),]
165-
y=D$sample_meta[[M$factor_names]]
166-
167-
# sort the data by sample id so that theyre in the right order for paired ttest
168-
temp=D$sample_meta[order(D$sample_meta[[M$factor_names]],D$sample_meta[[M$paired_factor]]), ]
169-
D=D[rownames(temp),]
170-
171-
# check number per class
172-
# if less then 2 then remove
173-
FF=filter_na_count(threshold=2,factor_name=M$factor_names)
174-
FF=model_apply(FF,D)
175-
D=predicted(FF)
176-
177-
# check equal numbers per class. if not equal then exclude.
178-
IN=rownames(FF$count)[(FF$count[,1]==FF$count[,2]) & (FF$count[,1]>2) & (FF$count[,2]>2)]
179-
D=D[,IN]
180145

146+
if (M$paired){
181147
estimate_name='estimate.mean of the differences'
148+
} else {
149+
estimate_name='estimate'
182150
}
183151

184-
185-
186152
X=D$data
187153
y=D$sample_meta[[M$factor_names]]
188154

189155
output=lapply(X,function(x) {
190156
a=tryCatch({
157+
158+
# check for pairs if required
159+
if (M$paired) {
160+
# get group A
161+
dfA=data.frame(val=x[y==L[1]],id=D$sample_meta[y==L[1],M$paired_factor])
162+
# get group B
163+
dfB=data.frame(val=x[y==L[2]],id=D$sample_meta[y==L[2],M$paired_factor])
164+
# merge
165+
Z = merge(dfA,dfB,by='id') # will exclude any sample without a matching pair in sample list
166+
# omit pairs with an NA
167+
Z = na.omit(Z) # excludes any pair with at least one NA
168+
169+
# check for at least 3 pairs
170+
if (nrow(Z)<3) {
171+
stop('not enough pairs')
172+
}
173+
174+
#extract for t-stat
175+
A = Z$val.x
176+
B = Z$val.y
177+
} else {
178+
A = x[y==L[1]]
179+
B = x[y==L[2]]
180+
}
181+
191182
g=unlist(
192183
t.test(
193-
x[y==L[1]],
194-
x[y==L[2]],
184+
A,
185+
B,
195186
paired = M$paired,
196187
var.equal=M$equal_variance,
197188
conf.level=M$conf_level
@@ -226,11 +217,11 @@ setMethod(f="model_apply",
226217
output=output[,-1]
227218
# ensure outputs are in the correct order (TODO: update to data.frame with rownames)
228219
output=output[CN,]
229-
output$p.value=p.adjust(output$p.value,method = param_value(M,'mtc'))
230-
output_value(M,'t_statistic')=output$statistic.t
231-
output_value(M,'p_value')=output$p.value
220+
output$p.value='p_value'=p.adjust(output$p.value,method = param_value(M,'mtc'))
221+
output_value(M,'t_statistic')=data.frame('t_statistic'=output$statistic.t,row.names = CN)
222+
output_value(M,'p_value')=data.frame('p_value'=output$p.value,row.names = CN)
232223
output_value(M,'dof')=output$parameter.df
233-
output_value(M,'significant')=output$p.value<param_value(M,'alpha')
224+
output_value(M,'significant')=data.frame('significant'=output$p.value<param_value(M,'alpha'),row.names=CN)
234225
M$conf_int=output[,4:5,drop=FALSE]
235226
colnames(M$conf_int)=c('lower','upper')
236227
if (M$paired) {
@@ -253,9 +244,9 @@ setMethod(f="model_apply",
253244
setMethod(f="as_data_frame",
254245
signature=c("ttest"),
255246
definition=function(M) {
256-
out=data.frame('t_statistic'=M$t_statistic,
257-
't_p_value'=M$p_value,
258-
't_significant'=M$significant)
247+
out=data.frame('t_statistic'=M$t_statistic[,1],
248+
't_p_value'=M$p_value[,1],
249+
't_significant'=M$significant[,1],row.names=rownames(M$t_statistic))
259250
out=cbind(out,M$estimates,M$conf_int)
260251
}
261252
)

0 commit comments

Comments
 (0)