| Title: | Robust Score Testing in GLMs, by Sign-Flip Contributions |
|---|---|
| Description: | Provides robust tests for testing in GLMs, by sign-flipping score contributions. The tests are robust against overdispersion, heteroscedasticity and, in some cases, ignored nuisance variables. See Hemerik, Goeman and Finos (2020) <doi:10.1111/rssb.12369>, De Santis, Goeman, Hemerik, Davenport, Finos (2025) <doi:10.1080/01621459.2025.2491775> and De Santis, Goeman, Davenport, Hemerik, Finos (2025) <doi:10.1214/25-EJS2409>. |
| Authors: | Livio Finos [cre, aut]
|
| Maintainer: | Livio Finos <[email protected]> |
| License: | GPL-2 |
| Version: | 1.3.2 |
| Built: | 2026-11-12 00:05:09 UTC |
| Source: | https://github.com/livioivil/flipscores |
This is the anova method for flipscores object. Remark: it performs type III deviance decomposition as in car::Anova.
## S3 method for class 'flipscores' anova(object, model1 = NULL, score_type = NULL, n_flips = 5000, id = NULL, ...)## S3 method for class 'flipscores' anova(object, model1 = NULL, score_type = NULL, n_flips = 5000, id = NULL, ...)
object |
|
model1 |
a |
score_type |
The type of score that is computed. It can be "orthogonalized", "effective" or "basic".
Default is "orthogonalized". "effective" and "orthogonalized" take into account the nuisance estimation. The default is |
n_flips |
The number of random flips of the score contributions.
When |
id |
a |
... |
other parameters allowed in |
set.seed(1) dt=data.frame(X=scale(rnorm(50)), Z=factor(rep(LETTERS[1:3],length.out=50))) dt$Y=rpois(n=nrow(dt),lambda=exp(dt$X*(dt$Z=="C"))) mod0=flipscores(Y~Z+X,data=dt,family="poisson") summary(mod0) anova(mod0) mod1=flipscores(Y~Z*X,data=dt,family="poisson") summary(mod1) anova(mod0,model1 = mod1)set.seed(1) dt=data.frame(X=scale(rnorm(50)), Z=factor(rep(LETTERS[1:3],length.out=50))) dt$Y=rpois(n=nrow(dt),lambda=exp(dt$X*(dt$Z=="C"))) mod0=flipscores(Y~Z+X,data=dt,family="poisson") summary(mod0) anova(mod0) mod1=flipscores(Y~Z*X,data=dt,family="poisson") summary(mod1) anova(mod0,model1 = mod1)
Coerce jfs to data frame
## S3 method for class 'jfs' as.data.frame(x, row.names = NULL, optional = FALSE, ...)## S3 method for class 'jfs' as.data.frame(x, row.names = NULL, optional = FALSE, ...)
x |
A |
row.names |
Ignored. |
optional |
Ignored. |
... |
Additional arguments (currently unused). |
A data frame with all summary tables stacked, with a
Model column prepended.
Generic function to coerce objects to class 'jfs'.
Coerces an fs_contrasts object to class jfs.
as.jfs(x, ...) ## Default S3 method: as.jfs(object, ...)as.jfs(x, ...) ## Default S3 method: as.jfs(object, ...)
x |
an object of class |
... |
further arguments (currently unused) |
an object of class jfs
Coerce contrast objects to class 'jfs'.
## S3 method for class 'fs_contrasts' as.jfs(x, ...)## S3 method for class 'fs_contrasts' as.jfs(x, ...)
x |
An object (typically from contrast methods). |
... |
Additional arguments passed to methods. |
Combine jfs objects
## S3 method for class 'jfs' c(...)## S3 method for class 'jfs' c(...)
... |
Objects to combine. Supported objects are |
A jfs object.
jfs objectsMethods for combining jfs objects.
combine_tests combines the tests derived from multiverse models.
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.
combine_tests(mods, comb_funct = "maxT", by = NULL, by_list=NULL, tail = 0) combine_contrasts(mods, comb_funct = "Mahalanobis", tail = 0)combine_tests(mods, comb_funct = "maxT", by = NULL, by_list=NULL, tail = 0) combine_contrasts(mods, comb_funct = "Mahalanobis", tail = 0)
mods |
a |
comb_funct |
combining function to be used.
Several functions are implemented: "mean", "median", "Fisher", "Liptak", (equal to) "Stoufer", "Tippet", (equal to) "minp", "maxT", "Mahalanobis".
Alternatively it can be a custom function that has a Tspace matrix as input.
For |
by |
if |
by_list |
NULL (default) or a list of vectors. For each vector of the list it combines test statistics with position given by the element of the vector. If the vectors in the list are characters, these refer to names(mods$Tspace). |
tail |
direction of the alternative hypothesis. It can be "two.sided" (or 0, the default), "less" (or -1) or "greater" (or +1). |
The function returns a jfs-object.
#First example library(flipscores) set.seed(123) #Simulate data n=20 D=data.frame(X=rnorm(n),Z1=rnorm(n),Z2=rnorm(n)) D$Y=D$Z1+D$X+rnorm(n) # Run four glms abd combine it in a list mod1=glm(Y~X+Z1+Z2,data=D) mod2=glm(Y~X+poly(Z1,2)+Z2,data=D) mod3=glm(Y~X+poly(Z1,2)+poly(Z2,2),data=D) mod4=glm(Y~X+Z1+poly(Z2,2),data=D) mods=list(mod1=mod1,mod2=mod2,mod3=mod3,mod4=mod4) # Let us analyze the tests related to coefficient "X" and combine them res=flipscores(mods,n_flips = 5000, seed = 1, tested_coeffs = "X") summary(combine_tests(res)) # Second (continued) example # flipscores jointly on all models and all coefficients mod1=glm(Y~X+Z1+Z2,data=D) mod2=glm(Y~X+poly(Z1,2)+Z2,data=D) mod3=glm(Y~X+poly(Z1,2)+poly(Z2,2),data=D) mod4=glm(Y~X+Z1+poly(Z2,2),data=D) mods=list(mod1=mod1,mod2=mod2,mod3=mod3,mod4=mod4) res=flipscores(mods,n_flips = 2000) summary(combine_tests(res)) summary(combine_tests(res, by="model")) summary(combine_tests(res, by="coefficient")) res2=combine_contrasts(res) summary(res2) #custom combinations: coeffs=c("(Intercept)","X","Z1","Z2") coeffs_ids=lapply(coeffs,grep,res2$summary_table$coeff) names(coeffs_ids)=coeffs summary(combine_tests(res2,by_list = coeffs_ids))#First example library(flipscores) set.seed(123) #Simulate data n=20 D=data.frame(X=rnorm(n),Z1=rnorm(n),Z2=rnorm(n)) D$Y=D$Z1+D$X+rnorm(n) # Run four glms abd combine it in a list mod1=glm(Y~X+Z1+Z2,data=D) mod2=glm(Y~X+poly(Z1,2)+Z2,data=D) mod3=glm(Y~X+poly(Z1,2)+poly(Z2,2),data=D) mod4=glm(Y~X+Z1+poly(Z2,2),data=D) mods=list(mod1=mod1,mod2=mod2,mod3=mod3,mod4=mod4) # Let us analyze the tests related to coefficient "X" and combine them res=flipscores(mods,n_flips = 5000, seed = 1, tested_coeffs = "X") summary(combine_tests(res)) # Second (continued) example # flipscores jointly on all models and all coefficients mod1=glm(Y~X+Z1+Z2,data=D) mod2=glm(Y~X+poly(Z1,2)+Z2,data=D) mod3=glm(Y~X+poly(Z1,2)+poly(Z2,2),data=D) mod4=glm(Y~X+Z1+poly(Z2,2),data=D) mods=list(mod1=mod1,mod2=mod2,mod3=mod3,mod4=mod4) res=flipscores(mods,n_flips = 2000) summary(combine_tests(res)) summary(combine_tests(res, by="model")) summary(combine_tests(res, by="coefficient")) res2=combine_contrasts(res) summary(res2) #custom combinations: coeffs=c("(Intercept)","X","Z1","Z2") coeffs_ids=lapply(coeffs,grep,res2$summary_table$coeff) names(coeffs_ids)=coeffs summary(combine_tests(res2,by_list = coeffs_ids))
Computes Score contributions for variables in model1 (or derived from ) under the null model model0.
Same usage as anova.glm.
The parameter id is used too,
if present in model0 (with priority) or in model1.
compute_scores(model0, model1, score_type = "standardized", ..., id = NULL)compute_scores(model0, model1, score_type = "standardized", ..., id = NULL)
model0 |
a |
model1 |
a |
score_type |
The type of score that is computed. It is "orthogonalized", "effective" or "basic". "effective" and "orthogonalized" take into account the nuisance estimation. |
... |
other arguments. |
id |
Cluster identifiers, a column name, or a one-sided formula. The
resolved vector is stored in |
Jesse Hemerik, Riccardo De Santis, Jelle Goeman and Livio Finos
set.seed(1) Z=rnorm(20) X=Z+rnorm(20) Y=rpois(n=20,lambda=exp(Z+X)) mod0=glm(Y~Z,family="poisson") X=data.frame(X=X) scr0=compute_scores(model0 = mod0, model1 = X) head(scr0)set.seed(1) Z=rnorm(20) X=Z+rnorm(20) Y=rpois(n=20,lambda=exp(Z+X)) mod0=glm(Y~Z,family="poisson") X=data.frame(X=X) scr0=compute_scores(model0 = mod0, model1 = X) head(scr0)
This function allows to apply the confint method to flipscores objects.
## S3 method for class 'flipscores' confint( object, parm = NULL, level = 0.95, type = c("equitailed", "symmetric"), score_type = c("standardized", "effective"), alternative = c("two.sided", "greater", "less"), amp_const = 20, flips = NULL, n_flips = NULL, ... )## S3 method for class 'flipscores' confint( object, parm = NULL, level = 0.95, type = c("equitailed", "symmetric"), score_type = c("standardized", "effective"), alternative = c("two.sided", "greater", "less"), amp_const = 20, flips = NULL, n_flips = NULL, ... )
object |
a |
parm |
a specification of which parameters are to be given confidence intervals, either a vector of numbers or a vector of names. If missing, they are recovered from the input model ( |
level |
the confidence level required. |
type |
the type of confidence interval to build, either "equitailed" or "symmetric". Default is "equitailed". |
score_type |
The type of score that is computed. It can be "standardized" or "effective". Default is inherited by the input model. |
alternative |
The direction of the test to invert, and corresponding interval with respect to the estimate. It can be "two.sided" (default), "greater", or "less". |
amp_const |
Argument related to the maximum initial span of the search interval. See details. |
flips |
the fixed flips to use in the interval search. If |
n_flips |
If |
... |
additional arguments for methods. |
All families allowed in glm, as well as Negative Binomial, are supported, and inherited here by the input model object. See the main function flipscores for further information.
flipscores is based on randomly sign-flipped score contributions. Performing the test (and generating the confidence interval) different times might give slightly different results. Providing a flip matrix generated with make_flips is suggested to uniform the results.
to_be_tested works differently for factors and character variables: the corresponding dummy variables are to be tested separately. So, to test a numeric variable X, one could write to_be_tested = "X", but to test a factor Z with levels A, B and C one should write to_be_tested = c("ZB", "ZC") as they appear in the model summary.
The bisection search is initialized in a predetermined interval. The maximum width of this interval is given by an amplitude (depending on the size of the estimated parameter and its standard error) multiplied by amp_const. The default for amp_const is 20. If the confidence bound is not found in the initial interval, Inf or -Inf is returned.
#' @seealso flipscores, make_flips
A matrix (or vector) with columns giving lower and upper confidence limits for each parameter. These will be labelled as (1-level)/2 and 1 - (1-level)/2 in
set.seed(1) dt <- data.frame(X=scale(rnorm(50)), Z=factor(rep(LETTERS[1:3],length.out=50))) dt$Y=rpois(n=nrow(dt),lambda=exp(dt$X*(dt$Z=="C"))) mod0 <- flipscores(Y~Z+X,data=dt,family="poisson") summary(mod0) confint(mod0, parm="X") xx <- rnorm(20) zz <- rnorm(20, 0.2 * xx) yy <- rnorm(20, 1 + 2 * xx - zz) flps <- make_flips(20, 1000) mod1 <- flipscores(yy ~ xx + zz) summary(mod1) confint(mod1, flips = flps)set.seed(1) dt <- data.frame(X=scale(rnorm(50)), Z=factor(rep(LETTERS[1:3],length.out=50))) dt$Y=rpois(n=nrow(dt),lambda=exp(dt$X*(dt$Z=="C"))) mod0 <- flipscores(Y~Z+X,data=dt,family="poisson") summary(mod0) confint(mod0, parm="X") xx <- rnorm(20) zz <- rnorm(20, 0.2 * xx) yy <- rnorm(20, 1 + 2 * xx - zz) flps <- make_flips(20, 1000) mod1 <- flipscores(yy ~ xx + zz) summary(mod1) confint(mod1, flips = flps)
Extended version of flipscores() supporting joint testing when
formula is a list of glm objects, a list of formulas, or a formula
with a matrix response.
flipscores( formula, family = gaussian(), data = NULL, score_type = "standardized", n_flips = 5000, alternative = "two.sided", id = NULL, seed = NULL, to_be_tested = NULL, tested_coeffs = NULL, flips = NULL, precompute_flips = TRUE, ... )flipscores( formula, family = gaussian(), data = NULL, score_type = "standardized", n_flips = 5000, alternative = "two.sided", id = NULL, seed = NULL, to_be_tested = NULL, tested_coeffs = NULL, flips = NULL, precompute_flips = TRUE, ... )
formula |
One of:
|
family |
Error distribution for |
data |
A data frame. Used in cases 1, 3, 4. |
score_type |
Type of score. One of |
n_flips |
Number of random flips. Default |
alternative |
Alternative hypothesis. Default |
id |
Optional cluster identifiers, also used to align score rows across
models. Use a vector with one value per score row, a column name, or a
one-sided formula such as |
seed |
Random seed for reproducibility. |
to_be_tested |
Character vector or list of coefficient names to test.
If a character vector, the same terms are intersected with each model's
coefficients. If a list, each element applies to the corresponding model.
If |
tested_coeffs |
Alias for |
flips |
Optional matrix of pre-computed flips. Columns may be named by
observations or, when |
precompute_flips |
Logical, default |
... |
Additional arguments passed to |
A flipscores object (case 1) or a jfs
object (cases 2, 3, 4). Joint objects retain local score matrices in
objects, with resolved identifiers in attr(scores, "id"),
and an alignment list containing the common rows and local
row-to-common-row index maps.
set.seed(1) n <- 50 df <- data.frame( y1 = rnorm(n), y2 = rnorm(n), yb = rbinom(n, 1, 0.5), y1p = rpois(n, lambda = 3), y2p = rpois(n, lambda = 5), x1 = rnorm(n), x2 = rnorm(n) ) # Case 1: single formula res=flipscores(y1 ~ x1 + x2, family = gaussian(), data = df) summary(res) # Case 2: list of glm objects models <- list( glm(y1 ~ x1 + x2, data = df, family = gaussian()), glm(y2 ~ x1 + x2, data = df, family = gaussian()), glm(yb ~ x1 + x2, data = df, family = binomial()) ) res=flipscores(models) summary(res) res=flipscores(models, to_be_tested = "x1") summary(res) # Different dataset sizes: resolve IDs separately in each model's data df$ids <- rep(seq_len(n / 2), each = 2) df_short <- df[-c(2, 5, 9), ] models_by_id <- list( full = glm(y1 ~ x1 + x2, data = df), shorter = glm(y2 ~ x1 + x2, data = df_short) ) # A shared vector of length 50 cannot match the shorter model's 47 rows: tryCatch( flipscores(models_by_id, id = df$ids, to_be_tested = "x1", n_flips = 99), error = function(e) message(conditionMessage(e)) ) # These forms support different row counts and cluster flips by the same IDs: res_by_id <- flipscores(models_by_id, id = ~ids, to_be_tested = "x1", n_flips = 99, seed = 1) res_by_name <- flipscores(models_by_id, id = "ids", to_be_tested = "x1", n_flips = 99, seed = 1) # Case 3: list of formulas, same family res=flipscores( list(y1 ~ x1 + x2, y2 ~ x1 + x2), family = gaussian(), data = df ) summary(res) # Case 4: matrix response, gaussian res=flipscores(cbind(y1, y2) ~ x1 + x2, family = gaussian(), data = df) summary(res) # Case 4: matrix response, poisson res=flipscores(cbind(y1p, y2p) ~ x1 + x2, family = poisson(), data = df) summary(res) # more examples n=20 DD=data.frame(X=rnorm(n),Z1=rnorm(n),Z2=rnorm(n)) DD$Y=DD$Z1+DD$X+rnorm(n) # Run four glms abd combine it in a list mod1=glm(Y~X+Z1+Z2,data=DD) mod2=glm(Y~X+poly(Z1,2)+Z2,data=DD) mod3=glm(Y~X+poly(Z1,2)+poly(Z2,2),data=DD) mod4=glm(Y~X+Z1+poly(Z2,2),data=DD) mods=list(mod1=mod1,mod2=mod2,mod3=mod3,mod4=mod4) res=flipscores(mods, to_be_tested = "X") summary(res)set.seed(1) n <- 50 df <- data.frame( y1 = rnorm(n), y2 = rnorm(n), yb = rbinom(n, 1, 0.5), y1p = rpois(n, lambda = 3), y2p = rpois(n, lambda = 5), x1 = rnorm(n), x2 = rnorm(n) ) # Case 1: single formula res=flipscores(y1 ~ x1 + x2, family = gaussian(), data = df) summary(res) # Case 2: list of glm objects models <- list( glm(y1 ~ x1 + x2, data = df, family = gaussian()), glm(y2 ~ x1 + x2, data = df, family = gaussian()), glm(yb ~ x1 + x2, data = df, family = binomial()) ) res=flipscores(models) summary(res) res=flipscores(models, to_be_tested = "x1") summary(res) # Different dataset sizes: resolve IDs separately in each model's data df$ids <- rep(seq_len(n / 2), each = 2) df_short <- df[-c(2, 5, 9), ] models_by_id <- list( full = glm(y1 ~ x1 + x2, data = df), shorter = glm(y2 ~ x1 + x2, data = df_short) ) # A shared vector of length 50 cannot match the shorter model's 47 rows: tryCatch( flipscores(models_by_id, id = df$ids, to_be_tested = "x1", n_flips = 99), error = function(e) message(conditionMessage(e)) ) # These forms support different row counts and cluster flips by the same IDs: res_by_id <- flipscores(models_by_id, id = ~ids, to_be_tested = "x1", n_flips = 99, seed = 1) res_by_name <- flipscores(models_by_id, id = "ids", to_be_tested = "x1", n_flips = 99, seed = 1) # Case 3: list of formulas, same family res=flipscores( list(y1 ~ x1 + x2, y2 ~ x1 + x2), family = gaussian(), data = df ) summary(res) # Case 4: matrix response, gaussian res=flipscores(cbind(y1, y2) ~ x1 + x2, family = gaussian(), data = df) summary(res) # Case 4: matrix response, poisson res=flipscores(cbind(y1p, y2p) ~ x1 + x2, family = poisson(), data = df) summary(res) # more examples n=20 DD=data.frame(X=rnorm(n),Z1=rnorm(n),Z2=rnorm(n)) DD$Y=DD$Z1+DD$X+rnorm(n) # Run four glms abd combine it in a list mod1=glm(Y~X+Z1+Z2,data=DD) mod2=glm(Y~X+poly(Z1,2)+Z2,data=DD) mod3=glm(Y~X+poly(Z1,2)+poly(Z2,2),data=DD) mod4=glm(Y~X+Z1+poly(Z2,2),data=DD) mods=list(mod1=mod1,mod2=mod2,mod3=mod3,mod4=mod4) res=flipscores(mods, to_be_tested = "X") summary(res)
Defines factor contrasts from a formula or accepts fully custom coefficient contrasts, then applies the flip-score test to each contrast.
flipscores_contrasts( model, specs = NULL, linfct = NULL, ref = NULL, score_type = "standardized", n_flips = 5000, alternative = "two.sided", id = NULL, seed = NULL, flips = NULL, ... )flipscores_contrasts( model, specs = NULL, linfct = NULL, ref = NULL, score_type = "standardized", n_flips = 5000, alternative = "two.sided", id = NULL, seed = NULL, flips = NULL, ... )
model |
a fitted |
specs |
a contrast formula, coefficient contrast matrix, numeric
vector, named list of numeric vectors, or |
linfct |
optional custom coefficient contrast matrix, numeric vector,
or named list of numeric vectors. This follows the same idea as
|
ref |
control level for Dunnett-like formula contrasts. It can be a
level name, cell name, or numeric index. The default is the first level/cell,
except for |
score_type |
type of score, as in |
n_flips |
number of sign flips. |
alternative |
|
id |
optional cluster id. |
seed |
optional random seed. |
flips |
optional precomputed flip matrix. |
... |
currently unused. |
an object of class fs_contrasts.
Performs robust score tests for one or more coefficients in a linear model
(ordinary least squares) by sign‑flipping score contributions. The test is
valid under heteroscedasticity, overdispersion and, in some cases, ignored
nuisance variables. It is an efficient alternative to flipscores()
when the model is Gaussian and the link is identity, the score_type
implemented is the "standardized".
flipscores_lm( formula, data = NULL, n_flips = 5000, alternative = "two.sided", seed = NULL, tested_coeffs = NULL, flips = NULL, ... )flipscores_lm( formula, data = NULL, n_flips = 5000, alternative = "two.sided", seed = NULL, tested_coeffs = NULL, flips = NULL, ... )
formula |
A formula describing the linear model (e.g. |
data |
An optional data frame containing the variables in the model.
If |
n_flips |
Number of random sign flips. Defaults to |
alternative |
Alternative hypothesis. Must be one of |
seed |
Optional integer seed for reproducibility of the random flips. |
tested_coeffs |
Character vector naming the coefficients to test.
If |
flips |
Optional matrix of pre‑computed sign flips. It must have
|
... |
Additional arguments passed to internal functions (currently not used). |
The function works by first projecting the response and the tested covariate onto the orthogonal complement of the nuisance design matrix. The resulting score contributions are then randomly sign‑flipped (with fixed signs across observations) to generate a permutation distribution of the test statistic.
The observed score statistic for a coefficient is
, where and are the
residualised covariate and residualised response, respectively. Under the
null hypothesis the sign of each observation’s contribution is arbitrary,
so repeatedly flipping the signs yields an exact conditional test.
The method is robust to misspecification of the variance structure and does not require the errors to be normally distributed or homoscedastic.
An object of class c("fs_lm", ...) with the following
components:
TspaceA matrix of dimension n_flips by k,
where k = length(tested_coeffs). The first row contains the
observed test statistics; the remaining rows contain the test statistics
obtained after random sign flips.
summary_tableA data frame with one row per tested coefficient, containing the observed score, the two‑sided or one‑sided p‑value, and optionally other summary statistics (e.g. standardised score).
infoA list with the original formula, the names of the design matrix columns, and the names of the response variables.
callThe matched call.
Hemerik, J., Goeman, J. J., & Finos, L. (2020). Robust testing in generalized linear models by sign‑flipping score contributions. Journal of the Royal Statistical Society Series B: Statistical Methodology, 82(3), 841‑864. doi:10.1111/rssb.12369
De Santis, R., Goeman, J. J., Hemerik, J., Davenport, S., & Finos, L. (2025). Inference in Generalized Linear Models with Robustness to Misspecified Variances. Journal of the American Statistical Association, 1‑10. doi:10.1080/01621459.2025.2491775
flipscores for the general GLM version;
summary.fs_lm for summarising results;
anova.fs_lm for joint tests across coefficients.
set.seed(123) n <- 100 data <- data.frame( x1 = rnorm(n), x2 = rnorm(n), y = 2 * rnorm(n, mean = 0, sd = 1) # pure noise (null holds) ) data$y2 = data$x1+ rnorm(n, mean = 0, sd = 1) # pure noise (null holds) # Test the coefficient of x1 res <- flipscores_lm(cbind(y,y2) ~ x1 + x2, data = data, n_flips = 999, alternative = "two.sided") flipscores:::summary.fs_lm(res) # Test only x2 res2 <- flipscores_lm(y ~ x1 + x2, data = data, tested_coeffs = "x2") flipscores:::summary.fs_lm(res2)set.seed(123) n <- 100 data <- data.frame( x1 = rnorm(n), x2 = rnorm(n), y = 2 * rnorm(n, mean = 0, sd = 1) # pure noise (null holds) ) data$y2 = data$x1+ rnorm(n, mean = 0, sd = 1) # pure noise (null holds) # Test the coefficient of x1 res <- flipscores_lm(cbind(y,y2) ~ x1 + x2, data = data, n_flips = 999, alternative = "two.sided") flipscores:::summary.fs_lm(res) # Test only x2 res2 <- flipscores_lm(y ~ x1 + x2, data = data, tested_coeffs = "x2") flipscores:::summary.fs_lm(res2)
Methods for flipscores objects.
The following are methods to extract and manipulate relevant information from
a flipscores object.
## S3 method for class 'flipscores' print(x, ...) ## S3 method for class 'flipscores' summary(object, ...)## S3 method for class 'flipscores' print(x, ...) ## S3 method for class 'flipscores' summary(object, ...)
x |
a flipscores object |
... |
additional arguments to be passed |
object |
a flipscores object |
This function computes the generalized partial correlation coefficient
for each specified variable in a generalized linear model. For each variable,
it refits the null model excluding that variable and computes the cosine similarity
between the residualized predictor and standardized residuals.
gcor(full_glm, terms = NULL, normalize = FALSE, intercept_too = FALSE)gcor(full_glm, terms = NULL, normalize = FALSE, intercept_too = FALSE)
full_glm |
A fitted GLM object of class 'glm'. |
terms |
Character vector of variable names (referred to the model.matrix, that is pay attention for factors) for which to compute generalized partial correlations. If 'NULL' (default), computes for all non-intercept terms in the model. |
normalize |
FALSE by default. |
intercept_too |
Logical indicating whether to include the intercept as a variable. Default is FALSE. |
The generalized partial correlation \r measures the association between
a predictor and response after adjusting for all other terms in the model.
It is defined as the cosine similarity between the residualized predictor
and standardized residuals :
where:
is the residualized predictor
is the standardized residual vector
is the hat matrix for the nuisance covariates
is the weight matrix
is the variance matrix and is the derivative matrix
The function uses 'flipscores:::get_par_expo_fam()' to compute and
consistently with the flipscores package methodology.
The normalized generalized partial correlation is computed as:
where is the maximum possible correlation and is the minimum.
When the (full) model has only intercept and only one predictor X, the
generalized (non partial) correlation is computed and the normalization factor
for X is exact.
In the more general case with more predictors, for sample sizes , brute force search is used to find
the exact extrema. For larger sample sizes, a greedy multi-start algorithm is employed:
Multiple starting points are generated using thresholding and random sampling
From each start, coordinates are greedily flipped to improve the correlation
Early stopping is used when no improvements are found for several iterations
The best solution across all starts is returned
This approach provides a good trade-off between computational efficiency and solution quality for large problems where brute force is infeasible.
if normalize is FALSE, a data frame with five columns:
variable |
The variable name |
r |
The generalized partial correlation coefficient |
while if normalize is TRUE
terms |
The variable name |
r |
The generalized partial correlation coefficient |
r_n |
The normalized generalized partial correlation coefficient |
null_model |
The null model used to compute the generalized (partial) correlation |
algorithm |
The algorithm used to compute the upper/lower bounds of the generalized partial correlation coefficient (to compute its normalized version) |
exact |
logical |
Livio Finos and Paolo Girardi
set.seed(1) dt=data.frame(X=rnorm(20), Z=factor(rep(LETTERS[1:3],length.out=20))) dt$Y=rpois(n=20,lambda=exp(dt$Z=="C")) mod=flipscores(Y~Z+X,data=dt,family="poisson",n_flips=1000) summary(mod) # Compute generalized partial correlations for all terms (results <- gcor(mod)) # Compute for specific terms only gcor(mod, terms = c("X", "ZC")) gcor(mod, terms = c("X", "ZC"),normalize=TRUE) gcor(mod, intercept_too=TRUE, normalize=TRUE) set.seed(123) dt=data.frame(X=rnorm(20), Z=factor(rep(LETTERS[1:3],length.out=20))) dt$Y=rbinom(n=20,prob=plogis((dt$Z=="C")*2),size=1) mod=flipscores(Y~Z+X,data=dt,family="binomial",n_flips=1000) summary(mod) (results <- gcor(mod,normalize=TRUE)) # Compute for specific terms only gcor(mod, terms = c("X", "ZC"),normalize=TRUE) ############################ set.seed(1) Z=rnorm(20) X=Z+rnorm(20)-10 Y=rpois(n=20,lambda=exp(Z+.25*X)) model1=glm(Y~Z+X,family="poisson") gcor(model1) gcor(model1, normalize=TRUE) #normalized_gcor_poisson(model1, "X", verbose = TRUE)[1:3] model0=glm(Y~Z,family="poisson") ## Not run: sapply(c('v','kl','sse','lr','n'),function(type) rsq.partial(model1,model0,type=type)$partial.rsq) set.seed(1) Z=rnorm(20) X=Z+rnorm(20) Y=rbinom(n=20,1,prob=plogis((-.5*Z+1*X))) model1=glm(Y~Z+X,family="binomial") gcor(model1) gcor(model1, normalize=TRUE) #normalized_gcor_binomial(model1, "X", verbose = TRUE)[1:3] model0=glm(Y~Z,family="binomial") ## Not run: sapply(c('v','kl','sse','lr','n'),function(type) rsq.partial(model1,model0,type=type)$partial.rsq)#' set.seed(1) Z=rnorm(20) X=10+Z+rnorm(20) Y=rnorm(n=20,mean=(Z+2*X)) model1=glm(Y~Z+X) gcor(model1) gcor(model1, normalize=TRUE) #library(rsq) model0x=glm(X~Z) model0y=glm(Y~Z) #t(.nrmz(resid(model0x)))%*% .nrmz(resid(model0y)) #library(ppcor) #pcor(cbind(X,Z,Y)) ## Not run: sapply(c('v','kl','sse','lr','n'),function(type) rsq.partial(model1,model0,type=type)$partial.rsq)set.seed(1) dt=data.frame(X=rnorm(20), Z=factor(rep(LETTERS[1:3],length.out=20))) dt$Y=rpois(n=20,lambda=exp(dt$Z=="C")) mod=flipscores(Y~Z+X,data=dt,family="poisson",n_flips=1000) summary(mod) # Compute generalized partial correlations for all terms (results <- gcor(mod)) # Compute for specific terms only gcor(mod, terms = c("X", "ZC")) gcor(mod, terms = c("X", "ZC"),normalize=TRUE) gcor(mod, intercept_too=TRUE, normalize=TRUE) set.seed(123) dt=data.frame(X=rnorm(20), Z=factor(rep(LETTERS[1:3],length.out=20))) dt$Y=rbinom(n=20,prob=plogis((dt$Z=="C")*2),size=1) mod=flipscores(Y~Z+X,data=dt,family="binomial",n_flips=1000) summary(mod) (results <- gcor(mod,normalize=TRUE)) # Compute for specific terms only gcor(mod, terms = c("X", "ZC"),normalize=TRUE) ############################ set.seed(1) Z=rnorm(20) X=Z+rnorm(20)-10 Y=rpois(n=20,lambda=exp(Z+.25*X)) model1=glm(Y~Z+X,family="poisson") gcor(model1) gcor(model1, normalize=TRUE) #normalized_gcor_poisson(model1, "X", verbose = TRUE)[1:3] model0=glm(Y~Z,family="poisson") ## Not run: sapply(c('v','kl','sse','lr','n'),function(type) rsq.partial(model1,model0,type=type)$partial.rsq) set.seed(1) Z=rnorm(20) X=Z+rnorm(20) Y=rbinom(n=20,1,prob=plogis((-.5*Z+1*X))) model1=glm(Y~Z+X,family="binomial") gcor(model1) gcor(model1, normalize=TRUE) #normalized_gcor_binomial(model1, "X", verbose = TRUE)[1:3] model0=glm(Y~Z,family="binomial") ## Not run: sapply(c('v','kl','sse','lr','n'),function(type) rsq.partial(model1,model0,type=type)$partial.rsq)#' set.seed(1) Z=rnorm(20) X=10+Z+rnorm(20) Y=rnorm(n=20,mean=(Z+2*X)) model1=glm(Y~Z+X) gcor(model1) gcor(model1, normalize=TRUE) #library(rsq) model0x=glm(X~Z) model0y=glm(Y~Z) #t(.nrmz(resid(model0x)))%*% .nrmz(resid(model0y)) #library(ppcor) #pcor(cbind(X,Z,Y)) ## Not run: sapply(c('v','kl','sse','lr','n'),function(type) rsq.partial(model1,model0,type=type)$partial.rsq)
Collection of methods for objects of class jfs.
p.adjust method for class "jfs" and "flipscores".
Add adjusted p-values into the jfsand flipscores object.
p.adjust(object, method = "maxT", tail = 0, ...)p.adjust(object, method = "maxT", tail = 0, ...)
object |
an object of class |
method |
any method implemented in |
tail |
argument: expresses the tail direction of the alternative hypothesis. It can be "two.sided" (or 0, the default), "less" (or -1) or "greater" (or +1). |
... |
additional arguments to be passed |
n_flipsxn_obs matrix of random +1 and -1.
The first row is made by ones (i.e. the observed test statistic is computed)It creates a n_flipsxn_obs matrix of random +1 and -1.
The first row is made by ones (i.e. the observed test statistic is computed)
make_flips(n_obs, n_flips, obs_names = NULL, id = NULL)make_flips(n_obs, n_flips, obs_names = NULL, id = NULL)
n_obs |
number of observations |
n_flips |
number of flips |
obs_names |
optional observation names for the columns. |
id |
optional cluster identifiers, one per observation. |
# example code make_flips(n_obs=10,n_flips=20)# example code make_flips(n_obs=10,n_flips=20)
Extract the model matrix from the underlying fitted model stored in an 'fs_contrasts' object.
## S3 method for class 'fs_contrasts' model.matrix(object, ...)## S3 method for class 'fs_contrasts' model.matrix(object, ...)
object |
An object of class 'fs_contrasts'. |
... |
Additional arguments passed to 'model.matrix' (e.g., 'data'). |
The model matrix (design matrix) of the original model.
Print method for jfs
## S3 method for class 'jfs' print(x, n = 2, ...)## S3 method for class 'jfs' print(x, n = 2, ...)
x |
A |
n |
Number of rows to show at head and tail of summary table. |
... |
Additional arguments (currently unused). |
Summary method for jfs
## S3 method for class 'jfs' summary(object, digits = 4, ...)## S3 method for class 'jfs' summary(object, digits = 4, ...)
object |
A |
digits |
Number of digits to print. Default |
... |
Additional arguments (currently unused). |
Ensures that update() on a flipscores object returns
a proper flipscores object rather than a plain glm.
## S3 method for class 'flipscores' update(object, ...)## S3 method for class 'flipscores' update(object, ...)
object |
A |
... |
Additional arguments to update in the call, such as
|
A flipscores object.
This method creates a 'jfs' object from a fitted model and a set of contrasts. The resulting object can be used with 'flipscores' for joint permutation testing across multiple contrasts or models.
## S3 method for class 'fs_contrasts' update(object, formula., ..., evaluate = TRUE)## S3 method for class 'fs_contrasts' update(object, formula., ..., evaluate = TRUE)
object |
An object of class 'fs_contrasts'. |
formula. |
Changes to the formula – see [update.formula()] for details. |
... |
Additional arguments passed to 'update()' for the underlying model, or to the contrast‑generating function. |
data |
New data frame. |
family |
New family for the GLM. |
linfct |
New contrast matrix (or formula to generate it). |
An object of class 'jfs' (joint flipscores) that contains the original model, the contrast matrix, and other relevant information.
An updated object of class 'fs_contrasts'.