Skip to content

Commit

Permalink
check ok
Browse files Browse the repository at this point in the history
  • Loading branch information
livio committed Nov 3, 2023
1 parent 5a3320f commit 3321c76
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 189 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(^\.github/.+$)|(^README.+$)
4 changes: 2 additions & 2 deletions R/anova_flipscores.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ anova.flipscores <- function(object, model1=NULL,
score1=scores[,id_col,drop=FALSE]
attributes(score1)$scale_objects=attributes(scores)$scale_objects[[id_col]]
attributes(score1)$score_type=attributes(scores)$score_type
as.matrix(.flip_test_no_pval(score1, ftail=I,precompute_flips = FALSE,
.score_fun = flipscores:::.score_std,n_flips = n_flips))
as.matrix(.flip_test_no_pval(score1, precompute_flips = FALSE,
.score_fun = .score_std,n_flips = n_flips))
})
dst=mahalanobis_npc(Tspace)

Expand Down
26 changes: 0 additions & 26 deletions R/compute_flips.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,32 +44,6 @@ compute_flips<- function(scores,alternative="two.sided",
...){


############### only for internal experiments
#######START
if(attributes(scores)$score_type=="my_lab") {
.score_fun <- function(flp,Y,Xt) Xt%*%flp%*%Y
n_obs=nrow(scores)
Xt=attributes(scores)$scale_objects$Xt

Tobs= .score_fun(diag(n_obs),scores,Xt=Xt)
# set.seed(seed)
Tspace=as.vector(c(Tobs,replicate(n_flips-1,{
flp<-flip::rom(n_obs)
.score_fun(flp,scores,Xt)
})))
# set.seed(NULL)
# Tspace=.sum2t(Tspace,
# sumY2 = sum(Y^2,na.rm = TRUE),
# n=sum(!is.na(Y)))

p.values=.t2p(ftail(unlist(Tspace)))
# named vector?

out=list(Tspace=Tspace,p.values=p.values)
names(out$p.values)=names(scores)
return(out)
}
######### END
##########################################

# browser()
Expand Down
34 changes: 16 additions & 18 deletions R/flipscores.R
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
#' @title Robust testing in GLMs, by sign-flipping score contributions
#'
#' @description Provides robust tests for testing in GLMs, by sign-flipping score contributions. The tests are often robust against overdispersion, heteroscedasticity and, in some cases, ignored nuisance variables.
#' @param score_type The type of score that is computed. It can be "orthogonalized", "effective" or "basic".
#' @param formula see \code{glm} function. It can also be a model (usually generated by a call to \code{glm}); in this case, any other glm-related parameter (e.g. \code{family, data, etc.}) are discarded, the function will make use of the ones used to generate the model.
#' (i.e. \code{formula}, \code{family}, \code{data}, etc) are not considered. It is \code{NULL} by default (i.e. not used).
#' @param family see \code{glm} function.
#' @param data see \code{glm} function.
#' @param score_type The type of score that is computed. It can be "standardized" "orthogonalized", "effective" or "basic".
#' Both "orthogonalized" and "effective" take into account the nuisance estimation and they provide the same
#' test statistic. In case of small samples "effective score" might have a slight anti-conservative behaviour.
#' "orthogonalized effective score" gives a solution for this issue.
#' Note that in case of a big model matrix, the "orthogonalized" may take a long time.
#'
#' "standardized effective score" gives a solution for this issue.
#' "orthogonalized" has a similar intent, note however that in case of a big model matrix, it may be slow.
#' @param n_flips The number of random flips of the score contributions. Overwritten with the \code{nrow(flips)} when \code{flips} is not \code{NULL} (see parameter \code{flips} for more details).
#' When \code{n_flips} is equal or larger than the maximum number of possible flips (i.e. n^2), all possible flips are performed.
#'
#' @param id a \code{vector} identifying the clustered observations. If \code{NULL} (default) observations are assumed to be independent. If \code{id} is not \code{NULL}, only \code{score_type=="effective"} is allowed, yet.
#' @param alternative It can be "greater", "less" or "two.sided" (default)
# @param seed \code{NULL} by default.
#' @param formula see \code{glm} function. It can also be a model (usually generated by a call to \code{glm}); in this case, any other glm-related parameter (e.g. \code{family, data, etc.}) are discarded, the function will make use of the ones used to generate the model.
#' @param family see \code{glm} function.
#' @param data see \code{glm} function.
#' @param model can be a model (e.g. \code{lm} or \code{glm}). In this case other parameters used to fit a \code{glm}
#' (i.e. \code{formula}, \code{family}, \code{data}, etc) are not considered. It is \code{NULL} by default (i.e. not used).
#' @param id a \code{vector} identifying the clustered observations. If \code{NULL} (default) observations are assumed to be independent. If \code{id} is not \code{NULL}, only \code{score_type=="effective"} is allowed, yet.
#' @param seed \code{NULL} by default.
#' @param to_be_tested vector of indices or names of coefficients of the glm model to be tested (it is faster than computing every scores and p-values of course).
#' @param precompute_flips \code{TRUE} by default. Overwritten if \code{flips} is not \code{NULL}. If \code{FALSE} the matrix of flips is not computed and the flips are made 'on-the-fly' before computing the test statistics; it may be usefull when \code{flips} is very large (see parameter \code{flips} for more details).
#' @param flips matrix fo +1 or -1, the matrix has \code{n_flips} rows and n (number of observations) columns
#' @param precompute_flips \code{TRUE} by default. Overwritten if \code{flips} is not \code{NULL}. If \code{FALSE} the matrix of flips is not computed and the flips are made 'on-the-fly' before computing the test statistics; it may be usefull when \code{flips} is very large (see parameter \code{flips} for more details).
#' @param output_flips \code{FALSE} by default. If \code{TRUE} the \code{flips} matrix is returned. Useful when the same flips are needed for more glms, for example in the case of multivariate glms where the joint distribution of test statistis if used for multivariate inference.
#' @param ... see \code{glm} function.
#'
#'
#' @usage flipscores(formula, family, data, score_type,
#' n_flips=5000, alternative ="two.sided",
#' output_flips=FALSE,
#' id = NULL, ...)
#' @usage flipscores(formula, family, data, score_type = "standardized",
#' n_flips = 5000, alternative = "two.sided", id = NULL,
#' seed = NULL, to_be_tested = NULL, flips = NULL,
#' precompute_flips = TRUE, output_flips = FALSE, ...)
#'
#' @return an object of class \code{flipscores}.
#' See also its methods (\code{summary.flipscores}, \code{anova.flipscores}, \code{print.flipscores}).
Expand Down Expand Up @@ -71,6 +68,7 @@ flipscores<-function(formula, family, data,
to_be_tested=NULL,
flips=NULL,
precompute_flips=TRUE,
output_flips=FALSE,
...){
# if(FALSE) flip() #just a trick to avoid warnings in package building
# temp=is(formula) #just a trick to avoid warnings in package building
Expand All @@ -88,7 +86,7 @@ flipscores<-function(formula, family, data,
flip_param_call= mf[c(1L,m)]

# rinomino la funzione da chiamare:
flip_param_call[[1L]]=.flip_test#quote(flipscores:::.flip_test) #
flip_param_call[[1L]]=.flip_test

flip_param_call$id=eval(flip_param_call$id, parent.frame())
flip_param_call$n_flips <- eval(flip_param_call$n_flips, parent.frame())
Expand Down
70 changes: 35 additions & 35 deletions man/compute_scores.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 29 additions & 29 deletions man/flipscores-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3321c76

Please sign in to comment.