Skip to content

Commit 7b87f82

Browse files
committed
debug
1 parent ce03cfc commit 7b87f82

File tree

11 files changed

+165
-243
lines changed

11 files changed

+165
-243
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ Description: runs resampling-based tests jointly (e.i. sign-flip score tests [He
88
Imports: flipscores, ggplot2, methods, flip
99
Encoding: UTF-8
1010
License: GPL-2
11-
RoxygenNote: 7.3.1
11+
RoxygenNote: 7.3.2

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ S3method(p.adjust,jointest)
55
S3method(plot,jointest)
66
S3method(summary,jointest)
77
export(combine)
8+
export(combine_contrasts)
89
export(flip2sss)
910
export(join_flipscores)
1011
import(dplyr)

R/combine.R

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,37 +32,37 @@ combine <- function (mods, comb_funct = "maxT", by = NULL, by_list=NULL, tail =
3232
res = lapply(1:length(combined), .npc2jointest,
3333
mods = mods, combined = combined, tail = tail, comb_funct = comb_funct)
3434
names(res) = names(combined)
35-
res=list(Tspace=.get_all_Tspace(res),summary_table=.get_all_summary_table(res))
35+
res=list(Tspace=jointest:::.get_all_Tspace(res),summary_table=jointest:::.get_all_summary_table(res))
3636
class(res) <- unique(c("jointest", class(res)))
3737
res
3838
}
3939

4040

4141
###################
42-
#' (nonparametric) combination of jointest object
43-
#' @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})
44-
#' @param comb_funct Combining function to be used. Several functions are implemented: "mean", "median", "Fisher", "Liptak", (equal to) "Stoufer", "Tippet", (equal to) "minp", "maxT" (the default).
45-
#' 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.
46-
#' @param tail direction of the alternative hypothesis. It can be "two.sided" (or 0, the default), "less" (or -1) or "greater" (or +1)
4742
#' @export
48-
# @describeIn combine \code{combine_contrasts} combines the tests derived from the contrasts of a factor variable to get a global test for the factor (i.e. categorical predictor). It has strong analogies with anova test.
43+
#' @describeIn combine \code{combine_contrasts} combines the tests derived from the contrasts of a factor variable to get a global test for the factor (i.e. categorical predictor). It has strong analogies with anova test.
4944

5045
combine_contrasts <- function (mods, comb_funct = "maxT", tail = 0)
5146
{
5247
# names(mods) = .set_mods_names(mods)
53-
combined=paste(mods$summary_table$Model,mods$summary_table$.assign,sep="_")
54-
if (is.null(combined)){
55-
warning('There is not column $summary_table$.assign, nothing is done.' )
56-
}
57-
uniq_nm = unique(combined)
58-
combined = lapply(uniq_nm, function(nm) which(combined == nm))
59-
names(combined) = uniq_nm
60-
61-
res = lapply(1:length(combined), .npc2jointest,
62-
mods = mods, combined = combined, tail = tail, comb_funct = comb_funct)
63-
names(res) = names(combined)
64-
res=list(Tspace=.get_all_Tspace(res),summary_table=.get_all_summary_table(res))
65-
class(res) <- unique(c("jointest", class(res)))
48+
# combined=paste(mods$summary_table$Model,mods$summary_table$.assign,sep="_")
49+
# if (is.null(combined)){
50+
# warning('There is not column $summary_table$.assign, nothing is done.' )
51+
# }
52+
# uniq_nm = unique(combined)
53+
# combined = lapply(uniq_nm, function(nm) which(combined == nm))
54+
# names(combined) = uniq_nm
55+
#
56+
# res = lapply(1:length(combined), .npc2jointest,
57+
# mods = mods, combined = combined, tail = tail, comb_funct = comb_funct)
58+
# names(res) = names(combined)
59+
# res=list(Tspace=.get_all_Tspace(res),summary_table=.get_all_summary_table(res))
60+
# class(res) <- unique(c("jointest", class(res)))
61+
# res
62+
smr=apply(res$summary_table[,c("Model",".assign"),drop=FALSE],1,paste,collapse=".")
63+
new_names=sapply(unique(smr),function(x).find_common_pattern(res$summary_table$Coeff[smr==x]))
64+
res=combine(res,by=c("Model",".assign"),comb_funct = comb_funct, tail = tail)
65+
res$summary_table$Coeff=new_names
6666
res
6767
}
6868

@@ -72,15 +72,15 @@ combine_contrasts <- function (mods, comb_funct = "maxT", tail = 0)
7272
comb_name = names(combined)[id]
7373
if (is.null(comb_name))
7474
comb_name = "Combined"
75-
Tspace = npc(mods$Tspace[, combined[[id]],drop=FALSE], comb_funct = comb_funct,
75+
Tspace = jointest:::npc(mods$Tspace[, combined[[id]],drop=FALSE], comb_funct = comb_funct,
7676
tail = tail)
7777
colnames(Tspace) = comb_name
7878
Coeff=mods$summary_table[combined[[id]],"Coeff"]
7979
Coeff=unique(Coeff)
8080
if(length(Coeff)>1) Coeff="many"
8181
summary_table = data.frame(Coeff = Coeff, Stat = comb_funct,
8282
nMods = max(1, length(combined[[id]])), S = Tspace[1],
83-
p = .t2p_only_first(Tspace, tail = 1))
83+
p = jointest:::.t2p_only_first(Tspace, tail = 1))
8484
list(Tspace = Tspace, summary_table = summary_table)
8585
}
8686

R/flip2sss.R

Lines changed: 70 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,32 @@
55
#' @param formula A formula or a list of formulas. It can be a complete model as.formula or a list of formulas, one for each element produced by the function.
66
#' @param data The dataset to be used for fitting the model.
77
#' @param cluster A vector or a formula evaluated on the data that defines the clusters.
8+
#' @param family as in \code{glm}, but given as a character. Not used if argument \code{summstats_within} is not \code{NULL}.
89
#' @param summstats_within A vector of summary statistics model within the data or a function with argument data.
910
#' @param ... Other arguments passed to the `flipscores` function.
10-
#'
11+
#' @export
1112
#' @return A jointest object containing the model results. Note that the flipscores models for each coefficient within are also included in the jointest object.
1213
#'
1314
#' @examples
14-
#' set.seed(1)
15-
#' N=50
16-
#' n=rpois(N,10)
15+
#' set.seed(123)
16+
#' N=20
17+
#' n=rpois(N,20)
1718
#' reff=rep(rnorm(N),n)
1819
#'
1920
#' D=data.frame(X1=rnorm(length(reff)),
2021
#' X2=rep(rnorm(N),n),
2122
#' Grp=factor(rep(rep(LETTERS[1:3],length.out=N),n)),
2223
#' SOGG=rep(1:N,n))
23-
#' D$Y=rbinom(n=nrow(D),prob=dlogis((D$Grp=="B") * D$X1 + D$X2),size=1)
24+
#' D$Y=rbinom(n=nrow(D),prob=plogis( 2*D$X1 * (D$Grp=="B") + 2*D$X2+reff),size=1)
2425
#'
2526
#' formula <- Y ~ Grp * X1 + X2
2627
#' cluster <- factor(D$SOGG)
2728
#' library(logistf)
2829
#' summstats_within <- 'logistf::logistf(Y ~ X1, family = binomial(link = "logit"),control=logistf::logistf.control(maxit=100))'
2930
#' #summstats_within <- 'glm(Y ~ X1, family = binomial(link = "logit"))'
3031
#' library(jointest)
31-
#' res <- flip2sss(formula, D, cluster, summstats_within)
32+
#' res <- flip2sss(formula, D, cluster, summstats_within=summstats_within)
33+
#' res <- flip2sss(formula, D, cluster, family="binomial")
3234
#' summary(res)
3335
#' summary(combine(res))
3436
#' summary(combine(res,by="Model"))
@@ -40,6 +42,7 @@
4042
flip2sss <- function(formula=NULL,
4143
data=NULL,
4244
cluster=NULL,
45+
family="gaussian",
4346
summstats_within=NULL,
4447
...){
4548

@@ -50,11 +53,11 @@ flip2sss <- function(formula=NULL,
5053

5154
###################
5255

53-
between_objs = .get_sets_vars_between(formula, data, cluster)
56+
vars_between_within = .get_vars_between_within(formula, data, cluster)
5457

55-
vars_between=between_objs$vars_between
56-
data2lev=between_objs$data2lev
57-
rm(between_objs)
58+
vars_between=vars_between_within$vars_between
59+
vars_within=vars_between_within$vars_within
60+
rm(vars_between_within)
5861
## make the second level dataset
5962
set_between = unique(unlist(vars_between))
6063
vars_between_formulas = lapply(vars_between, paste0, collapse = "+")
@@ -63,13 +66,15 @@ flip2sss <- function(formula=NULL,
6366
names(vars_between_formulas) = names(vars_between)
6467

6568
#####################
69+
if(is.null(summstats_within))
70+
summstats_within=paste0("glm(",formula[[2]],formula[[1]],vars_within,", family=",family,")")
71+
set_between=c(".cluster",set_between[set_between!="1"])
6672
data$.cluster=cluster
67-
ss_within = data %>%
68-
group_by(.cluster) %>%
73+
data2lev = data %>%
74+
group_by(data[set_between]) %>%
6975
summarise(as.data.frame(t(coefficients(eval(parse(text=summstats_within))))))
70-
ss_within$.cluster=NULL
71-
names(ss_within) = gsub("\\W", ".", names(ss_within))
72-
data2lev=cbind(data2lev,ss_within)
76+
data2lev$.cluster=NULL
77+
names(data2lev) = gsub("\\W", ".", names(data2lev))
7378

7479
mods = lapply(vars_between_formulas, function(frm) glm(eval(frm, parent.frame()), data = data2lev))
7580

@@ -92,44 +97,62 @@ flip2sss <- function(formula=NULL,
9297
}
9398

9499
######################
95-
.get_sets_vars_between <- function(formula, data, cluster){
100+
.expand_form <- function(FUN){
101+
out <- reformulate(labels(terms(FUN)), FUN[[2]])
102+
out
103+
}
104+
######################
105+
.get_vars_between_within <- function(formula, data, cluster){
106+
formula=.expand_form(formula)
96107
#clst_vals = unique(cluster) NON SERVE!
97108
D = model.matrix(formula, data = data)
98109

99110
## find constant cols within cluster
100111
const_id = do.call(rbind, by(D, cluster, function(D) as.data.frame(t(apply(D, 2, is.constant)))))
101112
vars_between_intercept = apply(const_id, 2, all)
102113

103-
data2lev=by(D,cluster, function(d) d[1,vars_between_intercept,drop=FALSE])
104-
data2lev=do.call(rbind,data2lev)
105-
106-
terms = colnames(D)#attributes(terms(formula))$term.labels#labels(terms(reformulate(as.character(formula))))
107-
ids_const = unique(attributes(D)$assign[vars_between_intercept])
108-
# attributes(D)$assign%in%ids_const
109-
if(attributes(terms(formula))$intercept==1){
110-
ids_const = ids_const + 1
111-
terms = c("1", terms)
112-
}
113-
intercept_vars_between_names=names(vars_between_intercept[vars_between_intercept])
114-
intercept_vars_between_names=gsub("^\\(Intercept\\)$","1",intercept_vars_between_names)
115-
vars_between_names = list(.Intercept. = intercept_vars_between_names)
116-
117-
cors = suppressWarnings(by(D[, !vars_between_intercept], cluster, cor))
118-
dim3 = c(dim(cors[[1]]), length(cors))
119-
cors = array(unlist(cors), dim3)
120-
cors = suppressWarnings(apply(cors, c(1, 2), min, na.rm = TRUE))
121-
cors[is.infinite(cors)]=1
122-
ei = eigen(cors)
123-
vars_between_others = lapply(which(ei$values > .1), function(cls) names(vars_between_intercept[!vars_between_intercept])[which(ei$vectors[, cls] != 0)])
124-
within_coeffs = sapply(vars_between_others, function(preds_vars) Reduce(intersect, strsplit(preds_vars, ":")))
125-
126-
vars_between_others = lapply(1:length(within_coeffs), function(i) gsub(within_coeffs, "1", vars_between_others[[i]]))
127-
vars_between_others = lapply(1:length(within_coeffs), function(i) gsub(":1$", "", vars_between_others[[i]]))
128-
vars_between_others = lapply(1:length(within_coeffs), function(i) gsub("^1:", "", vars_between_others[[i]]))
129-
130-
names(vars_between_others) = within_coeffs
131-
vars_between_names = c(vars_between_names, vars_between_others)
132-
list(data2lev=data2lev,vars_between_names=vars_between_names)
133-
}
114+
#vars_between_intercept
115+
between_vars=attributes(D)$assign[vars_between_intercept]
116+
intercept=0%in%between_vars
117+
between_vars=unique(between_vars)
118+
between_vars_intercept<-
119+
between_vars<-attr(terms(formula),"term.labels")[between_vars]
120+
if(intercept)
121+
between_vars_intercept=c("1",between_vars_intercept)
134122

123+
# formula_preds=paste(collapse = "+",between_vars)
124+
# formula_preds_between_intercept=paste0(formula[[2]],formula[[1]],formula_preds)
125+
126+
# data_intercept=data[,between_vars]
127+
128+
129+
130+
############# within variables
131+
within_vars=attributes(D)$assign[!vars_between_intercept]
132+
within_vars=unique(within_vars)
133+
within_vars_all=attr(terms(formula),"term.labels")[within_vars]
134+
within_vars=within_vars_all
135+
for(x in between_vars)
136+
within_vars=gsub(x,"",within_vars)
137+
within_vars=gsub(":","",within_vars)
138+
within_vars=unique(within_vars)
139+
140+
############# others between variables
141+
vars_between=lapply(within_vars, function(x) {
142+
temp=within_vars_all[grep(x,within_vars_all)]
143+
temp=gsub(x,"",within_vars_all)
144+
temp=gsub(":","",temp)
145+
if(any(temp=="")) temp[temp==""]="1"
146+
temp
147+
})
148+
names(vars_between)=within_vars
149+
150+
temp=list(".Intercept."=between_vars_intercept)
151+
vars_between=c(temp,vars_between)
152+
153+
154+
list(vars_within=within_vars,
155+
vars_between=vars_between)
156+
}
157+
135158
is.constant <- function(x) length(unique(x)) == 1

0 commit comments

Comments
 (0)