Skip to content

Commit 5367c6c

Browse files
author
livio
committed
...
1 parent daf1ac1 commit 5367c6c

13 files changed

+143
-191
lines changed

.Rbuildignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
(^\.github/.+$)|(^README.+$)
1+
(^\.github/.+$)|(^README.+$)|(^\docs/.+$)
2+
^.*\.Rproj$
3+
^\.Rproj\.user$

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.Rproj.user
2+
.Rhistory
3+
.RData
4+
.Ruserdata

NAMESPACE

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
S3method(as,jointest)
4+
S3method(p.adjust,jointest)
35
S3method(plot,jointest)
46
S3method(summary,jointest)
57
export(combine)
68
export(join_flipscores)
7-
export(p.adjust.fwer)
89
import(flipscores)
910
import(ggplot2)
1011
importFrom(flip,flip.adjust)

R/combine.R

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,54 @@
44
#' Alternativelly it can be a custom function that has a matrix as input. The function return a vector of length equal to the number of rows of the input matrix.
55
#' @param combined a) if \code{NULL} it combines all coefficients with the same names along models,
66
#' b) if a vector of characters, it gives a combination for each stats with these names
7-
#' c) if a list of arrays, each array has two columns reporting model (first column)
8-
#' and name of the coefficient to be combined.
7+
#' @param by vector of characters referring to the column's name in summary_table. Equal to "Coeff" by default.
98
#' @param tail direction of the alternative hypothesis. It can be "two.sided" (or 0, the default), "less" (or -1) or "greater" (or +1)
109
#' @export
1110
#'
12-
combine <- function (mods, comb_funct = "maxT", combined = NULL, tail = 0)
11+
combine <- function (mods, comb_funct = "maxT", combined = NULL, by=NULL, tail = 0)
1312
{
14-
names(mods) = .set_mods_names(mods)
15-
smr = .get_all_tested_coeffs_names(mods)
13+
# names(mods) = .set_mods_names(mods)
14+
if(is.null(by)) by_cols="Coeff"
15+
smr=apply(res$summary_table[,by_cols,drop=FALSE],1,paste,collapse="_")
1616
if (is.null(combined)) {
1717
uniq_nm = unique(smr)
1818
if (length(uniq_nm) == length(smr)) {
19-
combined = list(Overall = smr)
19+
combined = list(Overall = 1:ncol(res$Tspace))
2020
}
2121
else {
22-
combined = lapply(uniq_nm, function(nm) which(smr ==
23-
nm))
22+
combined = lapply(uniq_nm, function(nm) which(smr == nm))
2423
names(combined) = uniq_nm
2524
}
26-
}
27-
else if (!is.list(combined)) {
25+
} else
26+
if (!is.list(combined)) {
2827
uniq_nm = combined
29-
combined = lapply(uniq_nm, function(nm) which(smr ==
30-
nm))
28+
combined = lapply(uniq_nm, function(nm) which(smr == nm))
3129
names(combined) = uniq_nm
3230
}
3331
else {
3432
uniq_nm = unique(unlist(combined))
3533
}
36-
Tspace = .get_all_Tspace(mods)
34+
3735
res = lapply(1:length(combined), .npc2jointest,
38-
Tspace = Tspace, combined = combined, tail = tail, comb_funct = comb_funct)
36+
mods = mods, combined = combined, tail = tail, comb_funct = comb_funct)
3937
names(res) = names(combined)
38+
res=list(Tspace=.get_all_Tspace(res),summary_table=.get_all_summary_table(res))
4039
class(res) <- unique(c("jointest", class(res)))
4140
res
4241
}
4342

44-
.npc2jointest <- function (id, Tspace, combined, tail, comb_funct)
43+
.npc2jointest <- function (id, mods, combined, tail, comb_funct)
4544
{
4645
comb_name = names(combined)[id]
4746
if (is.null(comb_name))
4847
comb_name = "Combined"
49-
Tspace = npc(Tspace[, combined[[id]]], comb_funct = comb_funct,
48+
Tspace = npc(mods$Tspace[, combined[[id]]], comb_funct = comb_funct,
5049
tail = tail)
5150
colnames(Tspace) = comb_name
52-
summary_table = data.frame(Coeff = comb_name, Stat = comb_funct,
51+
Coeff=mods$summary_table[combined[[id]],"Coeff"]
52+
Coeff=unique(Coeff)
53+
if(length(Coeff)>1) Coeff="many"
54+
summary_table = data.frame(Coeff = Coeff, Stat = comb_funct,
5355
nMods = max(1, length(combined[[id]])), S = Tspace[1],
5456
p = .t2p_only_first(Tspace, tail = 1))
5557
list(Tspace = Tspace, summary_table = summary_table)

R/facilities.R

Lines changed: 0 additions & 83 deletions
This file was deleted.

R/join_flipscores.R

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@
2727
#' seed = 1, tested_coeffs = "X")
2828
#'summary(res)
2929
#'summary(combine(res))
30-
#'res=p.adjust.fwer(res)
30+
#'res=jointest:::p.adjust.jointest(res)
3131
#'summary(res)
32+
#'summary(combine(res,by="Model"))
3233
join_flipscores <- function (mods, tested_coeffs = NULL, n_flips = 5000, score_type = "standardized",
3334
statistics = "t",seed=NULL,...)
3435
{
@@ -49,8 +50,10 @@ join_flipscores <- function (mods, tested_coeffs = NULL, n_flips = 5000, score_t
4950
n_obs=sapply(mods, function(mod) length(mod$y))
5051
n_obs=max(n_obs)
5152

53+
mods_names=names(mods)
54+
5255
FLIPS=flipscores:::.make_flips (n_obs=n_obs,n_flips=n_flips)
53-
modflips = lapply(1:length(mods), function(i) {
56+
mods = lapply(1:length(mods), function(i) {
5457
temp = flipscores(formula = eval(mods[[i]],parent.frame()), score_type = score_type,
5558
flips = eval(FLIPS), to_be_tested = tested_coeffs[[i]],
5659
output_flips=FALSE
@@ -68,10 +71,14 @@ join_flipscores <- function (mods, tested_coeffs = NULL, n_flips = 5000, score_t
6871
temp$summary_table=.get_summary_table_from_flipscores(temp)
6972
temp
7073
})
71-
if(is.null(names(mods))){
72-
names(modflips)=paste0("mod",1:length(modflips))
74+
75+
if(is.null(mods_names)){
76+
names(mods)=paste0("mod",1:length(mos))
7377
} else
74-
names(modflips) = names(mods)
75-
class(modflips) <- unique(c("jointest", class(modflips)))
76-
modflips
78+
names(mods) = mods_names
79+
80+
81+
out=list(Tspace=.get_all_Tspace(mods),summary_table=.get_all_summary_table(mods))
82+
class(out) <- unique(c("jointest", class(out)))
83+
out
7784
}

R/jointest_package.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# @importFrom MASS glm.nb
77
# @importFrom plyr laply
88
# @importFrom methods is
9+
# @importFrom methods as
910
# @importFrom stats D as.formula model.matrix sd summary.glm update
1011
#' @examples
1112
#' n=20

R/methods.R

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,7 @@
5757

5858
NULL
5959
#
60-
# #' as.jointest method for a jointest object.
61-
# #' @rdname jointest-method
62-
# #' @param object an object of class \code{jointest}.
63-
# #' @param names_obj a vector of names, its length must be equal to the length of \code{object}
64-
# #' @param ... additional arguments to be passed
65-
# #' @method as jointest
66-
# #' @docType methods
67-
# #' @export
68-
69-
# as.jointest <- function (object, names_obj=NULL, ...)
70-
# {
71-
# TODO: calcolare summary_table in ogni elemento di object. se flipscores usa
72-
# .get_summary_table_from_flipscores()
73-
# if(!is.null(names_obj)) names(object)=names_obj
74-
# if (is.null(names(object))) names(object)=paste0("mod",1:length(object))
75-
# class(object) <- unique(c("jointest", class(object)))
76-
# object
77-
# }
60+
7861
# #' print.jointest print method for a jointest object.
7962
# #' @param x a jointest object
8063
# #' @method print jointest
@@ -94,25 +77,37 @@ NULL
9477

9578
summary.jointest <- function (object, ...)
9679
{
97-
if (length(object) > 1) {
98-
summary_table = lapply(object, function(cmb) cmb$summary_table)
99-
summary_table = do.call(rbind,summary_table)
100-
}
101-
else {
102-
summary_table = object[[1]]$summary_table
103-
}
104-
rownames(summary_table) = names(object)
105-
summary_table
80+
object$summary_table
10681
}
10782

10883
.get_summary_table_from_flipscores <- function(object){
10984
tab = as.data.frame(summary(object)$coefficients)
11085
tab = tab[!is.na(tab[, "Score"]), ]
86+
colnames(tab)[ncol(tab)]="p"
11187
tab = cbind( Coeff = rownames(tab), tab)
11288
}
11389

11490

11591
is_signif=NULL
92+
###########################
93+
#' as.jointest method for a jointest object.
94+
#' @rdname jointest-method
95+
#' @param object an object of class \code{jointest}.
96+
#' @param names_obj a vector of names, its length must be equal to the length of \code{object}
97+
#' @param ... additional arguments to be passed
98+
#' @method as jointest
99+
#' @docType methods
100+
#' @export
101+
102+
as.jointest <- function (object, names_obj=NULL, ...)
103+
{
104+
# TODO: calcolare summary_table in ogni elemento di object. se flipscores usa
105+
# .get_summary_table_from_flipscores()
106+
if(!is.null(names_obj)) names(object)=names_obj
107+
if (is.null(names(object))) names(object)=paste0("mod",1:length(object))
108+
class(object) <- unique(c("jointest", class(object)))
109+
object
110+
}
116111
#############################################
117112
#' plot.jointest summary method for a jointest object.
118113
#' @rdname jointest-method

R/padjust.R

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
1-
#'Correct p-values of a jointest object for multiple testing (FamilyWise Error Rate)
2-
#'@param mods object of class \code{jointest} (it can be a list of glm or flipscores converted in a \code{jointest} object using \code{as.jointest})
3-
#'@param method any method implemented in \code{flip::flip.adjust} or a custom function. In the last case it must be a function that uses a matrix as input and returns a vector of adjusted p-values equal to the numbero fo columns of the inputed matrix.
4-
#'@param tail = 0
5-
#'@param ... further parameters
6-
#'@export
7-
#'@importFrom flip flip.adjust
8-
9-
10-
p.adjust.fwer <- function (mods, method = "maxT", tail = 0, ...)
1+
# #'Correct p-values of a jointest object for multiple testing (i.e. Selective Inference)
2+
# #'@param mods object of class \code{jointest} (it can be a list of glm or flipscores converted in a \code{jointest} object using \code{as.jointest})
3+
# #'@param method any method implemented in \code{flip::flip.adjust} or a custom function. In the last case it must be a function that uses a matrix as input and returns a vector of adjusted p-values equal to the numbero fo columns of the inputed matrix.
4+
# #'@param tail = 0
5+
# #'@param ... further parameters
6+
# #'@export
7+
# #'@importFrom flip flip.adjust
8+
#'
9+
#' p.adjust.jointest method for a jointest object.
10+
#' @rdname jointest-method
11+
#' @param object an object of class \code{jointest}.
12+
#' @param method any method implemented in \code{flip::flip.adjust} or a custom function. In the last case it must be a function that uses a matrix as input and returns a vector of adjusted p-values equal to the numbero fo columns of the inputed matrix.
13+
#' @param ... additional arguments to be passed
14+
#' @method p.adjust jointest
15+
#' @docType methods
16+
#' @importFrom flip flip.adjust
17+
#' @export
18+
#'
19+
p.adjust.jointest <- function (mods, method = "maxT", tail = 0, ...)
1120
{
12-
Tspace = as.matrix(.get_all_Tspace(mods))
13-
colnames(Tspace) = paste0("v", 1:ncol(Tspace))
1421
if(is.character(method)){
15-
p.adj = flip.adjust(.set_tail(Tspace, tail = tail),
22+
p.adj = flip.adjust(.set_tail(mods$Tspace, tail = tail),
1623
method = method) }
1724
else if(is.function(method)){
18-
p.adj = method(.set_tail(Tspace, tail = tail))
19-
25+
p.adj = method(.set_tail(mods$Tspace, tail = tail))
2026
}
21-
tmp = lapply(1:length(mods), function(id) {
22-
mods[[id]]$summary_table$p.adj = p.adj[id]
23-
mods[[id]]
24-
})
25-
names(tmp)=names(mods)
26-
class(tmp) <- unique(c("jointest", class(tmp)))
27-
tmp
27+
mods$summary_table$p.adj = p.adj
28+
mods
2829
}

0 commit comments

Comments
 (0)