Title: | Optimal Nonparametric Testing of Missing Completely at Random |
---|---|
Description: | Provides functions for carrying out nonparametric hypothesis tests of the MCAR hypothesis based on the theory of Frechet classes and compatibility. Also gives functions for computing halfspace representations of the marginal polytope and related geometric objects. |
Authors: | Thomas B. Berrett [aut, cre] , Alberto Bordino [aut], Danat Duisenbekov [aut], Sean Jaffe [aut], Richard J. Samworth [aut] |
Maintainer: | Thomas B. Berrett <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.2.2 |
Built: | 2024-10-30 09:21:50 UTC |
Source: | CRAN |
Generate the matrix A, whose columns are the vertices of the marginal polytope.
Amatrix(bS, M)
Amatrix(bS, M)
bS |
A binary matrix specifying the set of observation patterns. Each row encodes a single pattern. |
M |
A vector of positive integers giving the alphabet sizes of the discrete variables. |
The matrix A.
bS=matrix(c(1,1,0, 1,0,1, 0,1,1),byrow=TRUE,ncol=3) M=c(2,2,2) Amatrix(bS,M)
bS=matrix(c(1,1,0, 1,0,1, 0,1,1),byrow=TRUE,ncol=3) M=c(2,2,2) Amatrix(bS,M)
Generate the matrix A, whose columns are the vertices of the marginal polytope, as a sparse matrix.
AmatrixSparse(bS, M)
AmatrixSparse(bS, M)
bS |
A binary matrix specifying the set of observation patterns. Each row encodes a single pattern. |
M |
A vector of positive integers giving the alphabet sizes of the discrete variables. |
The matrix A.
bS=matrix(c(1,1,0, 1,0,1, 0,1,1),byrow=TRUE,ncol=3) M=c(2,2,2) AmatrixSparse(bS,M)
bS=matrix(c(1,1,0, 1,0,1, 0,1,1),byrow=TRUE,ncol=3) M=c(2,2,2) AmatrixSparse(bS,M)
Generates the row indices used internally to generate the sparse matrix A.
aMatrixSparseRevLex(bS, M)
aMatrixSparseRevLex(bS, M)
bS |
A binary matrix specifying the set of observation patterns. Each row encodes a single pattern. |
M |
A vector of positive integers giving the alphabet sizes of the discrete variables. |
A vector of row indices.
Calculate a critical value for an MCAR test based on knowledge of the facet
structure of the Minkowski sum calculated by ConsMinkSumHrep
.
Cimproved(nS, bS, M, DR, Fp, alpha)
Cimproved(nS, bS, M, DR, Fp, alpha)
nS |
A vector of sample sizes, with each entry corresponding to an observation pattern. |
bS |
A binary matrix specifying the set of observation patterns. Each row encodes a single pattern. |
M |
A vector of positive integers giving the alphabet sizes of the discrete variables. |
DR |
The quantity |
Fp |
The quantity |
alpha |
The desired significance level |
The critical value defined in Berrett and Samworth (2023).
Berrett TB, Samworth RJ (2023). “Optimal nonparametric testing of Missing Completely At Random, and its connections to compatibility.” Ann. Statist., 51, 2170–2193.
bS=matrix(c(1,1,0, 1,0,1, 0,1,1),byrow=TRUE,ncol=3) r=4; s=3 M=c(r,s,2) Cimproved(rep(1000,3),bS,M,1,(2^r-2)*(2^s-2),0.05)
bS=matrix(c(1,1,0, 1,0,1, 0,1,1),byrow=TRUE,ncol=3) r=4; s=3 M=c(r,s,2) Cimproved(rep(1000,3),bS,M,1,(2^r-2)*(2^s-2),0.05)
A map from the joint space to an index set.
col_index(M, x)
col_index(M, x)
M |
A vector of positive integers giving the alphabet sizes of the discrete variables. |
x |
An element of the joint space. |
A positive integer no greater than the cardinality of the joint space uniquely identifying x
.
M=c(2,2,2) col_index(M,c(1,1,1)) col_index(M,c(1,1,2)) M=c(4,3,2) col_index(M,c(1,1,1)) col_index(M,c(2,1,1)) col_index(M,c(1,2,1)) col_index(M,c(1,1,2))
M=c(2,2,2) col_index(M,c(1,1,1)) col_index(M,c(1,1,2)) M=c(4,3,2) col_index(M,c(1,1,1)) col_index(M,c(2,1,1)) col_index(M,c(1,2,1)) col_index(M,c(1,1,2))
Generates the column indices used internally to generate the sparse matrix A.
colVector(cardS, cardChi)
colVector(cardS, cardChi)
cardS |
The number of missingness patterns. |
cardChi |
The cardinality of the full joint space. |
A vector of column indices.
A function that computes as defined in
Section 5 in Bordino and Berrett (2024), or
as defined in Section 2 in
Bordino and Berrett (2024). The sequence of means/variances, and the
sequence of patterns, are calculated with
getSigmaS
.
compute_av(type, X)
compute_av(type, X)
type |
If set equal to "mean", computes |
X |
The whole dataset with missing values. |
The value of or
.
Bordino A, Berrett TB (2024). “Tests of Missing Completely At Random based on sample covariance matrices.” arXiv preprint arXiv:2401.05256.
library(MASS) d = 3 n = 200 SigmaS=list() #Random 2x2 correlation matrices (necessarily consistent) for(j in 1:d){ x=runif(2,min=-1,max=1); y=runif(2,min=-1,max=1); SigmaS[[j]]=cov2cor(x%*%t(x) + y%*%t(y)) } X = data.frame(matrix(nrow = 3*n, ncol = 3)) X[1:n, c(1,2)] = mvrnorm(n, c(0,0), SigmaS[[1]]) X[(n+1):(2*n), c(2, 3)] = mvrnorm(n, c(0,0), SigmaS[[2]]) X[(2*n+1):(3*n), c(1, 3)] = mvrnorm(n, c(0,0), SigmaS[[3]]) X = as.matrix(X) xxx = get_SigmaS(X)$patterns compute_av("var", X) compute_av("mean", X)
library(MASS) d = 3 n = 200 SigmaS=list() #Random 2x2 correlation matrices (necessarily consistent) for(j in 1:d){ x=runif(2,min=-1,max=1); y=runif(2,min=-1,max=1); SigmaS[[j]]=cov2cor(x%*%t(x) + y%*%t(y)) } X = data.frame(matrix(nrow = 3*n, ncol = 3)) X[1:n, c(1,2)] = mvrnorm(n, c(0,0), SigmaS[[1]]) X[(n+1):(2*n), c(2, 3)] = mvrnorm(n, c(0,0), SigmaS[[2]]) X[(2*n+1):(3*n), c(1, 3)] = mvrnorm(n, c(0,0), SigmaS[[3]]) X = as.matrix(X) xxx = get_SigmaS(X)$patterns compute_av("var", X) compute_av("mean", X)
A function solving a SDP problem to compute the incompatibility index for a sequence of
correlation matrices, as defined in Bordino and Berrett (2024).
Writes the SDP problem in standard primal form, and uses
csdp
to solve this.
computeR(patterns = list(), SigmaS = list())
computeR(patterns = list(), SigmaS = list())
patterns |
A vector with all the patterns in |
SigmaS |
The sequence of correlation matrices |
The value of , in the interval
.
The optimal for the primal problem.
The sequence of matrices as defined in Bordino and Berrett (2024).
The optimal for the dual problem.
The sequence of correlation matrices in input.
Bordino A, Berrett TB (2024). “Tests of Missing Completely At Random based on sample covariance matrices.” arXiv preprint arXiv:2401.05256.
d = 3 SigmaS=list() #Random 2x2 correlation matrices (necessarily consistent) for(j in 1:d){ x=runif(2,min=-1,max=1); y=runif(2,min=-1,max=1) SigmaS[[j]]=cov2cor(x%*%t(x) + y%*%t(y)) } result = computeR(list(c(1,2),c(2,3), c(1,3)), SigmaS = SigmaS) result$R
d = 3 SigmaS=list() #Random 2x2 correlation matrices (necessarily consistent) for(j in 1:d){ x=runif(2,min=-1,max=1); y=runif(2,min=-1,max=1) SigmaS[[j]]=cov2cor(x%*%t(x) + y%*%t(y)) } result = computeR(list(c(1,2),c(2,3), c(1,3)), SigmaS = SigmaS) result$R
Computes the minimal halfspace representation of the Minkowski sum of the marginal polytope and the consistent ball defined in Berrett and Samworth (2023).
ConsMinkSumHrep(bS, M, round = FALSE)
ConsMinkSumHrep(bS, M, round = FALSE)
bS |
A binary matrix specifying the set of observation patterns. Each row encodes a single pattern. |
M |
A vector of positive integers giving the alphabet sizes of the discrete variables. |
round |
A logical value indicating whether or not to round coefficients to 15 significant figures.
The function |
A halfspace representation object as used by the rcdd
package. See Geyer and Meeden (2021) for more detail.
Berrett TB, Samworth RJ (2023). “Optimal nonparametric testing of Missing Completely At Random, and its connections to compatibility.” Ann. Statist., 51, 2170–2193.
Geyer CJ, Meeden GD (2021). rcdd: Computational Geometry. https://CRAN.R-project.org/package=rcdd.
bS=matrix(c(1,1,0, 1,0,1, 0,1,1),byrow=TRUE,ncol=3) ConsMinkSumHrep(bS,c(2,2,2))
bS=matrix(c(1,1,0, 1,0,1, 0,1,1),byrow=TRUE,ncol=3) ConsMinkSumHrep(bS,c(2,2,2))
Calculate a simple critical value for an MCAR test using only knowledge of the set of observation patterns and the joint observation space.
Csimple(nS, bS, M, alpha)
Csimple(nS, bS, M, alpha)
nS |
A vector of sample sizes, with each entry corresponding to an observation pattern. |
bS |
A binary matrix specifying the set of observation patterns. Each row encodes a single pattern. |
M |
A vector of positive integers giving the alphabet sizes of the discrete variables. |
alpha |
The desired significance level |
The universal critical value defined in Berrett and Samworth (2023).
Berrett TB, Samworth RJ (2023). “Optimal nonparametric testing of Missing Completely At Random, and its connections to compatibility.” Ann. Statist., 51, 2170–2193.
bS=matrix(c(1,1,0, 1,0,1, 0,1,1),byrow=TRUE,ncol=3) r=4; s=3 M=c(r,s,2) Csimple(rep(1000,3),bS,M,0.05)
bS=matrix(c(1,1,0, 1,0,1, 0,1,1),byrow=TRUE,ncol=3) r=4; s=3 M=c(r,s,2) Csimple(rep(1000,3),bS,M,0.05)
Perform one step of the EM algorithm for finding the MLE under MCAR in a contingency table.
EMiteration(pt, p0h, n0, pSh, nS, bS, M)
EMiteration(pt, p0h, n0, pSh, nS, bS, M)
pt |
An input probability mass function on the joint space, to be updated. |
p0h |
An empirical mass function calculated using complete observations. |
n0 |
An integer giving the number of complete observations used to calculate |
pSh |
A sequence of empirical mass functions calculated using incomplete observations. |
nS |
A sequence of integers giving the numbers of incomplete observations used to calculate |
bS |
A binary matrix specifying the set of observation patterns. Each row encodes a single pattern. |
M |
A vector of positive integers giving the alphabet sizes of the discrete variables. |
The updated probability mass function on the joint space.
bS=matrix(c(1,1,0, 1,0,1, 0,1,1),byrow=TRUE,ncol=3) # Our canonical 3d example M=c(2,2,2) n0=200 nS=c(200,200,200) pS=c(0.125,0.375,0.375,0.125,0.250,0.250,0.250,0.250,0.100,0.400,0.400,0.100) P12=pS[1:4]; P13=pS[5:8]; P23=pS[9:12] X12=t(rmultinom(1,size=nS[1],prob=P12)/nS[1]) X13=t(rmultinom(1,size=nS[2],prob=P13)/nS[2]) X23=t(rmultinom(1,size=nS[3],prob=P23)/nS[3]) pSh=cbind(X12,X13,X23) p0=array(0.125,dim=c(2,2,2)) p0h=array(rmultinom(1,n0,p0),dim=M)/n0 EMiteration(p0,p0h,n0,pSh,nS,bS,M)
bS=matrix(c(1,1,0, 1,0,1, 0,1,1),byrow=TRUE,ncol=3) # Our canonical 3d example M=c(2,2,2) n0=200 nS=c(200,200,200) pS=c(0.125,0.375,0.375,0.125,0.250,0.250,0.250,0.250,0.100,0.400,0.400,0.100) P12=pS[1:4]; P13=pS[5:8]; P23=pS[9:12] X12=t(rmultinom(1,size=nS[1],prob=P12)/nS[1]) X13=t(rmultinom(1,size=nS[2],prob=P13)/nS[2]) X23=t(rmultinom(1,size=nS[3],prob=P23)/nS[3]) pSh=cbind(X12,X13,X23) p0=array(0.125,dim=c(2,2,2)) p0h=array(rmultinom(1,n0,p0),dim=M)/n0 EMiteration(p0,p0h,n0,pSh,nS,bS,M)
The marginal polytope and related objects have many symmetries. By relabelling the levels of discrete variables we transform facets into other facets. This function reduces a list of halfspace normals to its equivalence classes.
EquivalenceClass(bS, M, Hrep)
EquivalenceClass(bS, M, Hrep)
bS |
A binary matrix specifying the set of observation patterns. Each row encodes a single pattern. |
M |
A vector of positive integers giving the alphabet sizes of the discrete variables. |
Hrep |
An H-representation generated by |
A list of representative halfspace normals.
bS=matrix(c(1,1,0, 1,0,1, 0,1,1),byrow=TRUE,ncol=3) # Our canonical 3d example Hrep=MargPolyHrep(bS,c(2,2,2)) EquivalenceClass(bS,c(2,2,2),Hrep)
bS=matrix(c(1,1,0, 1,0,1, 0,1,1),byrow=TRUE,ncol=3) # Our canonical 3d example Hrep=MargPolyHrep(bS,c(2,2,2)) EquivalenceClass(bS,c(2,2,2),Hrep)
Carry out Fuchs's test of MCAR in a contingency table, given complete and incomplete observations.
FuchsTest(p0h, n0, pSh, nS, bS, M, Niter)
FuchsTest(p0h, n0, pSh, nS, bS, M, Niter)
p0h |
An empirical mass function calculated using complete observations. |
n0 |
An integer giving the number of complete observations used to calculate |
pSh |
A sequence of empirical mass functions calculated using incomplete observations. |
nS |
A sequence of integers giving the numbers of incomplete observations used to calculate |
bS |
A binary matrix specifying the set of observation patterns. Each row encodes a single pattern. |
M |
A vector of positive integers giving the alphabet sizes of the discrete variables. |
Niter |
An integer giving the number of iterations to be used in the EM algorithm for calculating the null MLE. |
The p-value of Fuchs's test, found by comparing the log likelihood ratio statistic to the chi-squared distribution with the appropriate number of degrees of freedom. Described in Fuchs (1982).
Fuchs C (1982). “Maximum likelihood estimation and model selection in contingency tables with missing data.” J. Amer. Statist. Assoc., 77(378), 270–278.
bS=matrix(c(1,1,0, 1,0,1, 0,1,1),byrow=TRUE,ncol=3) # Our canonical 3d example M=c(2,2,2) n0=200 nS=c(200,200,200) pS=c(0.125,0.375,0.375,0.125,0.250,0.250,0.250,0.250,0.100,0.400,0.400,0.100) P12=pS[1:4]; P13=pS[5:8]; P23=pS[9:12] X12=t(rmultinom(1,size=nS[1],prob=P12)/nS[1]) X13=t(rmultinom(1,size=nS[2],prob=P13)/nS[2]) X23=t(rmultinom(1,size=nS[3],prob=P23)/nS[3]) pSh=cbind(X12,X13,X23) p0=array(0.125,dim=c(2,2,2)) p0h=array(rmultinom(1,n0,p0),dim=M)/n0 FuchsTest(p0h,n0,pSh,nS,bS,M,50)
bS=matrix(c(1,1,0, 1,0,1, 0,1,1),byrow=TRUE,ncol=3) # Our canonical 3d example M=c(2,2,2) n0=200 nS=c(200,200,200) pS=c(0.125,0.375,0.375,0.125,0.250,0.250,0.250,0.250,0.100,0.400,0.400,0.100) P12=pS[1:4]; P13=pS[5:8]; P23=pS[9:12] X12=t(rmultinom(1,size=nS[1],prob=P12)/nS[1]) X13=t(rmultinom(1,size=nS[2],prob=P13)/nS[2]) X23=t(rmultinom(1,size=nS[3],prob=P23)/nS[3]) pSh=cbind(X12,X13,X23) p0=array(0.125,dim=c(2,2,2)) p0h=array(rmultinom(1,n0,p0),dim=M)/n0 FuchsTest(p0h,n0,pSh,nS,bS,M,50)
Using the same the notation of Bordino and Berrett (2024), computes
the sequence of patterns , means
, variances
, and correlation matrices
for a dataset with missing values.
get_SigmaS(X)
get_SigmaS(X)
X |
The dataset with incomplete data. |
patterns
The sequence of patterns .
n_pattern
The cardinality of .
data_pattern
A vector where the data are grouped according to .
muS
The sequence of means.
C_S
The sequence of covariance matrices.
sigma_squared_S
The sequence of variances.
SigmaS
The sequence of correlation matrices.
ambient_dimension
The dimension of the data.
Bordino A, Berrett TB (2024). “Tests of Missing Completely At Random based on sample covariance matrices.” arXiv preprint arXiv:2401.05256.
library(copula) library(missMethods) n = 1000 cp = claytonCopula(param = c(1), dim = 5) P = mvdc(copula = cp, margins = c("exp", "exp", "exp", "exp", "exp"), paramMargins = list(list(1), list(1), list(1), list(1), list(1))) X = rMvdc(n, P) X = delete_MCAR(X, 0.1, c(1,4,5)) get_SigmaS(X)
library(copula) library(missMethods) n = 1000 cp = claytonCopula(param = c(1), dim = 5) P = mvdc(copula = cp, margins = c("exp", "exp", "exp", "exp", "exp"), paramMargins = list(list(1), list(1), list(1), list(1), list(1))) X = rMvdc(n, P) X = delete_MCAR(X, 0.1, c(1,4,5)) get_SigmaS(X)
Computes the minimal halfspace representation of the Minkowski sum of the marginal polytope and the inconsistent ball defined in Berrett and Samworth (2023).
InconsMinkSumHrep(bS, M, round = FALSE)
InconsMinkSumHrep(bS, M, round = FALSE)
bS |
A binary matrix specifying the set of observation patterns. Each row encodes a single pattern. |
M |
A vector of positive integers giving the alphabet sizes of the discrete variables. |
round |
A logical value indicating whether or not to round coefficients to 15 significant figures.
The function |
A halfspace representation object as used by the rcdd
package. See Geyer and Meeden (2021) for more detail.
Berrett TB, Samworth RJ (2023). “Optimal nonparametric testing of Missing Completely At Random, and its connections to compatibility.” Ann. Statist., 51, 2170–2193.
Geyer CJ, Meeden GD (2021). rcdd: Computational Geometry. https://CRAN.R-project.org/package=rcdd.
bS=matrix(c(1,1, 1,0),byrow=TRUE,ncol=2) InconsMinkSumHrep(bS,c(2,2))
bS=matrix(c(1,1, 1,0),byrow=TRUE,ncol=2) InconsMinkSumHrep(bS,c(2,2))
Calculates the total cardinality of the sample spaces.
infoS(bS, M)
infoS(bS, M)
bS |
A binary matrix specifying the set of observation patterns. Each row encodes a single pattern. |
M |
A vector of positive integers giving the alphabet sizes of the discrete variables. |
The total cardinality.
Calculates the individual cardinalities of the sample spaces.
infoS2(bS, M)
infoS2(bS, M)
bS |
A binary matrix specifying the set of observation patterns. Each row encodes a single pattern. |
M |
A vector of positive integers giving the alphabet sizes of the discrete variables. |
A vector of individual cardinalities.
Carry out Little's test of MCAR
little_test(X, alpha, type = "mean&cov")
little_test(X, alpha, type = "mean&cov")
X |
The dataset with incomplete data, where all the pairs of variables are observed together. |
alpha |
The nominal level of the test. |
type |
Determines the test statistic to use, based on the discussion in Section 5 in Bordino and Berrett (2024).
The default option is "mean&cov", and uses the test statistic |
A Boolean, where TRUE stands for reject MCAR. This is computed by comparing the p-value of Little's test,
found by comparing the log likelihood ratio statistic to the chi-squared distribution with the appropriate number
of degrees of freedom, with the nominal level alpha
. Described in Little (1988).
Bordino A, Berrett TB (2024). “Tests of Missing Completely At Random based on sample covariance matrices.” arXiv preprint arXiv:2401.05256.
Little RJ (1988). “A test of Missing Completely at Random for multivariate data with missing values.” J. Amer. Statist. Assoc., 83, 1198–1202.
library(MASS) alpha = 0.05 n = 200 SigmaS=list() #Random 2x2 correlation matrices (necessarily consistent) for(j in 1:3){ x=runif(2,min=-1,max=1); y=runif(2,min=-1,max=1) SigmaS[[j]]=cov2cor(x%*%t(x) + y%*%t(y)) } X1 = mvrnorm(n, c(0,0), SigmaS[[1]]) X2 = mvrnorm(n, c(0,0), SigmaS[[2]]) X3 = mvrnorm(n, c(0,0), SigmaS[[3]]) columns = c("X1","X2","X3") X = data.frame(matrix(nrow = 3*n, ncol = 3)) X[1:n, c("X1", "X2")] = X1 X[(n+1):(2*n), c("X2", "X3")] = X2 X[(2*n+1):(3*n), c("X1", "X3")] = X3 X = as.matrix(X) little_test(X, alpha)
library(MASS) alpha = 0.05 n = 200 SigmaS=list() #Random 2x2 correlation matrices (necessarily consistent) for(j in 1:3){ x=runif(2,min=-1,max=1); y=runif(2,min=-1,max=1) SigmaS[[j]]=cov2cor(x%*%t(x) + y%*%t(y)) } X1 = mvrnorm(n, c(0,0), SigmaS[[1]]) X2 = mvrnorm(n, c(0,0), SigmaS[[2]]) X3 = mvrnorm(n, c(0,0), SigmaS[[3]]) columns = c("X1","X2","X3") X = data.frame(matrix(nrow = 3*n, ncol = 3)) X[1:n, c("X1", "X2")] = X1 X[(n+1):(2*n), c("X2", "X3")] = X2 X[(2*n+1):(3*n), c("X1", "X3")] = X3 X = as.matrix(X) little_test(X, alpha)
Compute the log likelihood of a probability mass function, under MCAR, given complete and incomplete data
loglik0(p, p0h, n0, pSh, nS, bS, M)
loglik0(p, p0h, n0, pSh, nS, bS, M)
p |
A probability mass function whose log likelihood is to be calculated. |
p0h |
An empirical mass function calculated using complete observations. |
n0 |
An integer giving the number of complete observations used to calculate |
pSh |
A sequence of empirical mass functions calculated using incomplete observations. |
nS |
A sequence of integers giving the numbers of incomplete observations used to calculate |
bS |
A binary matrix specifying the set of observation patterns. Each row encodes a single pattern. |
M |
A vector of positive integers giving the alphabet sizes of the discrete variables. |
The value of the log likelihood.
bS=matrix(c(1,1,0, 1,0,1, 0,1,1),byrow=TRUE,ncol=3) # Our canonical 3d example M=c(2,2,2) n0=200 nS=c(200,200,200) pS=c(0.125,0.375,0.375,0.125,0.250,0.250,0.250,0.250,0.100,0.400,0.400,0.100) P12=pS[1:4]; P13=pS[5:8]; P23=pS[9:12] X12=t(rmultinom(1,size=nS[1],prob=P12)/nS[1]) X13=t(rmultinom(1,size=nS[2],prob=P13)/nS[2]) X23=t(rmultinom(1,size=nS[3],prob=P23)/nS[3]) pSh=cbind(X12,X13,X23) p0=array(0.125,dim=c(2,2,2)) p0h=array(rmultinom(1,n0,p0),dim=M)/n0 loglik0(p0,p0h,n0,pSh,nS,bS,M)
bS=matrix(c(1,1,0, 1,0,1, 0,1,1),byrow=TRUE,ncol=3) # Our canonical 3d example M=c(2,2,2) n0=200 nS=c(200,200,200) pS=c(0.125,0.375,0.375,0.125,0.250,0.250,0.250,0.250,0.100,0.400,0.400,0.100) P12=pS[1:4]; P13=pS[5:8]; P23=pS[9:12] X12=t(rmultinom(1,size=nS[1],prob=P12)/nS[1]) X13=t(rmultinom(1,size=nS[2],prob=P13)/nS[2]) X23=t(rmultinom(1,size=nS[3],prob=P23)/nS[3]) pSh=cbind(X12,X13,X23) p0=array(0.125,dim=c(2,2,2)) p0h=array(rmultinom(1,n0,p0),dim=M)/n0 loglik0(p0,p0h,n0,pSh,nS,bS,M)
Compute the log likelihood of a probability mass function, without assuming MCAR, given complete and incomplete data
loglik1(p0, pS, p0h, n0, pSh, nS, bS, M)
loglik1(p0, pS, p0h, n0, pSh, nS, bS, M)
p0 |
A probability mass function on the joint space. |
pS |
A sequence of probability mass functions on the marginal spaces. |
p0h |
An empirical mass function calculated using complete observations. |
n0 |
An integer giving the number of complete observations used to calculate |
pSh |
A sequence of empirical mass functions calculated using incomplete observations. |
nS |
A sequence of integers giving the numbers of incomplete observations used to calculate |
bS |
A binary matrix specifying the set of observation patterns. Each row encodes a single pattern. |
M |
A vector of positive integers giving the alphabet sizes of the discrete variables. |
The value of the log likelihood.
bS=matrix(c(1,1,0, 1,0,1, 0,1,1),byrow=TRUE,ncol=3) # Our canonical 3d example M=c(2,2,2) n0=200 nS=c(200,200,200) pS=c(0.125,0.375,0.375,0.125,0.250,0.250,0.250,0.250,0.100,0.400,0.400,0.100) P12=pS[1:4]; P13=pS[5:8]; P23=pS[9:12] X12=t(rmultinom(1,size=nS[1],prob=P12)/nS[1]) X13=t(rmultinom(1,size=nS[2],prob=P13)/nS[2]) X23=t(rmultinom(1,size=nS[3],prob=P23)/nS[3]) pSh=cbind(X12,X13,X23) p0=array(0.125,dim=c(2,2,2)) p0h=array(rmultinom(1,n0,p0),dim=M)/n0 loglik1(p0,pS,p0h,n0,pSh,nS,bS,M)
bS=matrix(c(1,1,0, 1,0,1, 0,1,1),byrow=TRUE,ncol=3) # Our canonical 3d example M=c(2,2,2) n0=200 nS=c(200,200,200) pS=c(0.125,0.375,0.375,0.125,0.250,0.250,0.250,0.250,0.100,0.400,0.400,0.100) P12=pS[1:4]; P13=pS[5:8]; P23=pS[9:12] X12=t(rmultinom(1,size=nS[1],prob=P12)/nS[1]) X13=t(rmultinom(1,size=nS[2],prob=P13)/nS[2]) X23=t(rmultinom(1,size=nS[3],prob=P23)/nS[3]) pSh=cbind(X12,X13,X23) p0=array(0.125,dim=c(2,2,2)) p0h=array(rmultinom(1,n0,p0),dim=M)/n0 loglik1(p0,pS,p0h,n0,pSh,nS,bS,M)
A function that computes the inconsistency index for a sequence of
means, as defined in Section 5 in Bordino and Berrett (2024).
M(mu_S, patterns)
M(mu_S, patterns)
mu_S |
The sequence of means |
patterns |
A vector with all the patterns in |
The value of , in the interval
.
Bordino A, Berrett TB (2024). “Tests of Missing Completely At Random based on sample covariance matrices.” arXiv preprint arXiv:2401.05256.
library(MASS) d = 3 n = 200 SigmaS=list() #Random 2x2 correlation matrices (necessarily consistent) for(j in 1:d){ x=runif(2,min=-1,max=1); y=runif(2,min=-1,max=1); SigmaS[[j]]=cov2cor(x%*%t(x) + y%*%t(y)) } X = data.frame(matrix(nrow = 3*n, ncol = 3)) X[1:n, c(1,2)] = mvrnorm(n, c(0,0), SigmaS[[1]]) X[(n+1):(2*n), c(2, 3)] = mvrnorm(n, c(0,0), SigmaS[[2]]) X[(2*n+1):(3*n), c(1, 3)] = mvrnorm(n, c(0,0), SigmaS[[3]]) X = as.matrix(X) xxx = get_SigmaS(X)$patterns M(get_SigmaS(X)$muS, xxx)
library(MASS) d = 3 n = 200 SigmaS=list() #Random 2x2 correlation matrices (necessarily consistent) for(j in 1:d){ x=runif(2,min=-1,max=1); y=runif(2,min=-1,max=1); SigmaS[[j]]=cov2cor(x%*%t(x) + y%*%t(y)) } X = data.frame(matrix(nrow = 3*n, ncol = 3)) X[1:n, c(1,2)] = mvrnorm(n, c(0,0), SigmaS[[1]]) X[(n+1):(2*n), c(2, 3)] = mvrnorm(n, c(0,0), SigmaS[[2]]) X[(2*n+1):(3*n), c(1, 3)] = mvrnorm(n, c(0,0), SigmaS[[3]]) X = as.matrix(X) xxx = get_SigmaS(X)$patterns M(get_SigmaS(X)$muS, xxx)
Computes the minimal halfspace representation of the marginal polytope defined, for example, in Berrett and Samworth (2023).
MargPolyHrep(bS, M, round = FALSE)
MargPolyHrep(bS, M, round = FALSE)
bS |
A binary matrix specifying the set of observation patterns. Each row encodes a single pattern. |
M |
A vector of positive integers giving the alphabet sizes of the discrete variables. |
round |
A logical value indicating whether or not to round coefficients to 15 significant figures.
The function |
A halfspace representation object as used by the rcdd
package. See Geyer and Meeden (2021) for more detail.
Berrett TB, Samworth RJ (2023). “Optimal nonparametric testing of Missing Completely At Random, and its connections to compatibility.” Ann. Statist., 51, 2170–2193.
Geyer CJ, Meeden GD (2021). rcdd: Computational Geometry. https://CRAN.R-project.org/package=rcdd.
bS=matrix(c(1,1,0, 1,0,1, 0,1,1),byrow=TRUE,ncol=3) MargPolyHrep(bS,c(2,2,2))
bS=matrix(c(1,1,0, 1,0,1, 0,1,1),byrow=TRUE,ncol=3) MargPolyHrep(bS,c(2,2,2))
Internal function multiplying a mass function by the sparse matrix A.
margProj(p, bS, M)
margProj(p, bS, M)
p |
A subprobability mass function on the full joint space. |
bS |
A binary matrix specifying the set of observation patterns. Each row encodes a single pattern. |
M |
A vector of positive integers giving the alphabet sizes of the discrete variables. |
A collection of marginal mass functions.
This is the implementation of Algorithm 1 in Bordino and Berrett (2024).
MCAR_meancovTest(X, alpha, B)
MCAR_meancovTest(X, alpha, B)
X |
The dataset with incomplete data. |
alpha |
The nominal level of the test. |
B |
The bootstrap sample |
A Boolean, where TRUE stands for reject MCAR. This is found as outlined in Section 5.2 in Bordino and Berrett (2024).
Bordino A, Berrett TB (2024). “Tests of Missing Completely At Random based on sample covariance matrices.” arXiv preprint arXiv:2401.05256.
library(MASS) alpha = 0.05 B = 20 m = 500 SigmaS=list() #Random 2x2 correlation matrices (necessarily consistent) for(j in 1:3){ x=runif(2,min=-1,max=1); y=runif(2,min=-1,max=1) SigmaS[[j]]=cov2cor(x%*%t(x) + y%*%t(y)) } X1 = mvrnorm(m, c(0,0), SigmaS[[1]]) X2 = mvrnorm(m, c(0,0), SigmaS[[2]]) X3 = mvrnorm(m, c(0,0), SigmaS[[3]]) columns = c("X1","X2","X3") X = data.frame(matrix(nrow = 3*m, ncol = 3)) X[1:m, c("X1", "X2")] = X1 X[(m+1):(2*m), c("X2", "X3")] = X2 X[(2*m+1):(3*m), c("X1", "X3")] = X3 X = as.matrix(X) MCAR_meancovTest(X, alpha, B)
library(MASS) alpha = 0.05 B = 20 m = 500 SigmaS=list() #Random 2x2 correlation matrices (necessarily consistent) for(j in 1:3){ x=runif(2,min=-1,max=1); y=runif(2,min=-1,max=1) SigmaS[[j]]=cov2cor(x%*%t(x) + y%*%t(y)) } X1 = mvrnorm(m, c(0,0), SigmaS[[1]]) X2 = mvrnorm(m, c(0,0), SigmaS[[2]]) X3 = mvrnorm(m, c(0,0), SigmaS[[3]]) columns = c("X1","X2","X3") X = data.frame(matrix(nrow = 3*m, ncol = 3)) X[1:m, c("X1", "X2")] = X1 X[(m+1):(2*m), c("X2", "X3")] = X2 X[(2*m+1):(3*m), c("X1", "X3")] = X3 X = as.matrix(X) MCAR_meancovTest(X, alpha, B)
Compute the MLE under MCAR in a contingency table using the EM algorithm, given complete and incomplete observations.
MLE(p0h, n0, pSh, nS, bS, M, Niter, loglik = FALSE)
MLE(p0h, n0, pSh, nS, bS, M, Niter, loglik = FALSE)
p0h |
An empirical mass function calculated using complete observations. |
n0 |
An integer giving the number of complete observations used to calculate |
pSh |
A sequence of empirical mass functions calculated using incomplete observations. |
nS |
A sequence of integers giving the numbers of incomplete observations used to calculate |
bS |
A binary matrix specifying the set of observation patterns. Each row encodes a single pattern. |
M |
A vector of positive integers giving the alphabet sizes of the discrete variables. |
Niter |
An integer giving the number of iterations to be used in the EM algorithm. |
loglik |
A logical value indicating whether or not the log likelihoods at each step of the EM algorithm should be an output. Defaults to FALSE. |
The output of the EM algorithm, approximating the MLE for the probability mass function on the joint space.
bS=matrix(c(1,1,0, 1,0,1, 0,1,1),byrow=TRUE,ncol=3) # Our canonical 3d example M=c(2,2,2) n0=200 nS=c(200,200,200) pS=c(0.125,0.375,0.375,0.125,0.250,0.250,0.250,0.250,0.100,0.400,0.400,0.100) P12=pS[1:4]; P13=pS[5:8]; P23=pS[9:12] X12=t(rmultinom(1,size=nS[1],prob=P12)/nS[1]) X13=t(rmultinom(1,size=nS[2],prob=P13)/nS[2]) X23=t(rmultinom(1,size=nS[3],prob=P23)/nS[3]) pSh=cbind(X12,X13,X23) p0=array(0.125,dim=c(2,2,2)) p0h=array(rmultinom(1,n0,p0),dim=M)/n0 MLE(p0h,n0,pSh,nS,bS,M,50) trace=MLE(p0h,n0,pSh,nS,bS,M,50,loglik=TRUE)[[2]] plot(1:50,trace,type="l")
bS=matrix(c(1,1,0, 1,0,1, 0,1,1),byrow=TRUE,ncol=3) # Our canonical 3d example M=c(2,2,2) n0=200 nS=c(200,200,200) pS=c(0.125,0.375,0.375,0.125,0.250,0.250,0.250,0.250,0.100,0.400,0.400,0.100) P12=pS[1:4]; P13=pS[5:8]; P23=pS[9:12] X12=t(rmultinom(1,size=nS[1],prob=P12)/nS[1]) X13=t(rmultinom(1,size=nS[2],prob=P13)/nS[2]) X23=t(rmultinom(1,size=nS[3],prob=P23)/nS[3]) pSh=cbind(X12,X13,X23) p0=array(0.125,dim=c(2,2,2)) p0h=array(rmultinom(1,n0,p0),dim=M)/n0 MLE(p0h,n0,pSh,nS,bS,M,50) trace=MLE(p0h,n0,pSh,nS,bS,M,50,loglik=TRUE)[[2]] plot(1:50,trace,type="l")
Carry out a test of MCAR in a contingency table, given incomplete observations.
ProjectionTest(pSh, nS, bS, M, B)
ProjectionTest(pSh, nS, bS, M, B)
pSh |
A sequence of empirical mass functions calculated using incomplete observations. |
nS |
A sequence of integers giving the numbers of incomplete observations used to calculate |
bS |
A binary matrix specifying the set of observation patterns. Each row encodes a single pattern. |
M |
A vector of positive integers giving the alphabet sizes of the discrete variables. |
B |
An integer giving the number of bootstrap samples to be used to calibrate the test. |
The p-value the Monte Carlo test described in Berrett and Samworth (2023).
The value of the test statistic .
Berrett TB, Samworth RJ (2023). “Optimal nonparametric testing of Missing Completely At Random, and its connections to compatibility.” Ann. Statist., 51, 2170–2193.
bS=matrix(c(1,1,0, 1,0,1, 0,1,1),byrow=TRUE,ncol=3) # Our canonical 3d example M=c(2,2,2) nS=c(200,200,200) pS=c(0.125,0.375,0.375,0.125,0.250,0.250,0.250,0.250,0.100,0.400,0.400,0.100) P12=pS[1:4]; P13=pS[5:8]; P23=pS[9:12] X12=t(rmultinom(1,size=nS[1],prob=P12)/nS[1]) X13=t(rmultinom(1,size=nS[2],prob=P13)/nS[2]) X23=t(rmultinom(1,size=nS[3],prob=P23)/nS[3]) pSh=cbind(X12,X13,X23) ProjectionTest(pSh,nS,bS,M,99)
bS=matrix(c(1,1,0, 1,0,1, 0,1,1),byrow=TRUE,ncol=3) # Our canonical 3d example M=c(2,2,2) nS=c(200,200,200) pS=c(0.125,0.375,0.375,0.125,0.250,0.250,0.250,0.250,0.100,0.400,0.400,0.100) P12=pS[1:4]; P13=pS[5:8]; P23=pS[9:12] X12=t(rmultinom(1,size=nS[1],prob=P12)/nS[1]) X13=t(rmultinom(1,size=nS[2],prob=P13)/nS[2]) X23=t(rmultinom(1,size=nS[3],prob=P23)/nS[3]) pSh=cbind(X12,X13,X23) ProjectionTest(pSh,nS,bS,M,99)
A function solving a linear program to compute the incompatibility index defined in Berrett and Samworth (2023),
in the case of having discrete random variables.
Uses
Amatrix
to define to constraint matrix and lpSolve
to implement the linear optimisation.
Rindex(pS, bS, M)
Rindex(pS, bS, M)
pS |
A sequence of probability mass functions on the marginal spaces. |
bS |
A binary matrix specifying the set of observation patterns. Each row encodes a single pattern. |
M |
A vector of positive integers giving the alphabet sizes of the discrete variables. |
The value of , in the interval
.
Berrett TB, Samworth RJ (2023). “Optimal nonparametric testing of Missing Completely At Random, and its connections to compatibility.” Ann. Statist., 51, 2170–2193.
bS=matrix(c(1,1,0, 1,0,1, 0,1,1),byrow=TRUE,ncol=3) M=c(2,2,2) pS=rep(0.25,12) Rindex(pS,bS,M) pS=c(0.125,0.375,0.375,0.125,0.250,0.250,0.250,0.250,0.100,0.400,0.400,0.100) Rindex(pS,bS,M)
bS=matrix(c(1,1,0, 1,0,1, 0,1,1),byrow=TRUE,ncol=3) M=c(2,2,2) pS=rep(0.25,12) Rindex(pS,bS,M) pS=c(0.125,0.375,0.375,0.125,0.250,0.250,0.250,0.250,0.100,0.400,0.400,0.100) Rindex(pS,bS,M)
A function solving a linear program to compute the incompatibility index defined in Berrett and Samworth (2023),
in the case of having discrete random variables.
Uses
Amatrix
to define to constraint matrix and lpSolve
to implement the linear optimisation.
RindexDual(pS, bS, M, lp_solver = "default", simplex_strategy = 4)
RindexDual(pS, bS, M, lp_solver = "default", simplex_strategy = 4)
pS |
A sequence of probability mass functions on the marginal spaces. |
bS |
A binary matrix specifying the set of observation patterns. Each row encodes a single pattern. |
M |
A vector of positive integers giving the alphabet sizes of the discrete variables. |
lp_solver |
An argument passed to HiGHS specifying which solver to use. |
simplex_strategy |
An argument passed to HiGHS specifying which solver to use. |
The value of , in the interval
.
The optimal solution to the linear program
Berrett TB, Samworth RJ (2023). “Optimal nonparametric testing of Missing Completely At Random, and its connections to compatibility.” Ann. Statist., 51, 2170–2193.
bS=matrix(c(1,1,0, 1,0,1, 0,1,1),byrow=TRUE,ncol=3) M=c(2,2,2) A=Amatrix(bS,M) pS=rep(0.25,12) linprog=RindexDual(pS,bS,M) rbind(pS,as.vector(A%*%linprog[[2]])/(1-linprog[[1]])) pS=c(0.125,0.375,0.375,0.125,0.250,0.250,0.250,0.250,0.100,0.400,0.400,0.100) linprog=RindexDual(pS,bS,M) rbind(pS,as.vector(A%*%linprog[[2]])/(1-linprog[[1]]))
bS=matrix(c(1,1,0, 1,0,1, 0,1,1),byrow=TRUE,ncol=3) M=c(2,2,2) A=Amatrix(bS,M) pS=rep(0.25,12) linprog=RindexDual(pS,bS,M) rbind(pS,as.vector(A%*%linprog[[2]])/(1-linprog[[1]])) pS=c(0.125,0.375,0.375,0.125,0.250,0.250,0.250,0.250,0.100,0.400,0.400,0.100) linprog=RindexDual(pS,bS,M) rbind(pS,as.vector(A%*%linprog[[2]])/(1-linprog[[1]]))
Round errors in halfspace representations
RoundErrors(X, digits = 15)
RoundErrors(X, digits = 15)
X |
A halfspace representation to be rounded. |
digits |
An integer giving the number of significant figures to be kept. |
A rounded halfspace representation.
bS=matrix(c(1,1,1,0, 1,0,0,1, 0,1,0,1, 0,0,1,1),byrow=TRUE,ncol=4) RoundErrors("9007199254740992/6004799503160661") #Occurs in ConsMinkSumHrep(bS,c(2,2,2,2))
bS=matrix(c(1,1,1,0, 1,0,0,1, 0,1,0,1, 0,0,1,1),byrow=TRUE,ncol=4) RoundErrors("9007199254740992/6004799503160661") #Occurs in ConsMinkSumHrep(bS,c(2,2,2,2))
A map from the observation space to an index set.
row_index(bS, M, S, xS)
row_index(bS, M, S, xS)
bS |
A binary matrix specifying the set of observation patterns. Each row encodes a single pattern. |
M |
A vector of positive integers giving the alphabet sizes of the discrete variables. |
S |
An integer indicating which observation pattern is of interest. |
xS |
An element of the observation space of the specified observation pattern. |
A positive integer no larger than the cardinality of the joint space uniquely identifying x
.
bS=matrix(c(1,1,0, 1,0,1, 0,1,1),byrow=TRUE,ncol=3) M=c(2,2,2) row_index(bS,M,1,c(1,1)) row_index(bS,M,2,c(1,1)) row_index(bS,M,3,c(1,1)) bS=matrix(c(1,1,0, 1,0,1, 0,1,1),byrow=TRUE,ncol=3) M=c(4,3,2) row_index(bS,M,1,c(1,1)) row_index(bS,M,1,c(2,1)) row_index(bS,M,1,c(3,1)) row_index(bS,M,1,c(4,1)) row_index(bS,M,1,c(1,2)) row_index(bS,M,1,c(2,2))
bS=matrix(c(1,1,0, 1,0,1, 0,1,1),byrow=TRUE,ncol=3) M=c(2,2,2) row_index(bS,M,1,c(1,1)) row_index(bS,M,2,c(1,1)) row_index(bS,M,3,c(1,1)) bS=matrix(c(1,1,0, 1,0,1, 0,1,1),byrow=TRUE,ncol=3) M=c(4,3,2) row_index(bS,M,1,c(1,1)) row_index(bS,M,1,c(2,1)) row_index(bS,M,1,c(3,1)) row_index(bS,M,1,c(4,1)) row_index(bS,M,1,c(1,2)) row_index(bS,M,1,c(2,2))
A function that computes the inconsistency index for a sequence of
variances as defined in Section 2 in Bordino and Berrett (2024), given the fact that
.
V(sigma_squared_S, patterns)
V(sigma_squared_S, patterns)
sigma_squared_S |
The sequence of variances |
patterns |
A vector with all the patterns in |
The value of , in the interval
.
Bordino A, Berrett TB (2024). “Tests of Missing Completely At Random based on sample covariance matrices.” arXiv preprint arXiv:2401.05256.
library(MASS) d = 3 n = 200 SigmaS=list() #Random 2x2 correlation matrices (necessarily consistent) for(j in 1:d){ x=runif(2,min=-1,max=1); y=runif(2,min=-1,max=1); SigmaS[[j]]=cov2cor(x%*%t(x) + y%*%t(y)) } X = data.frame(matrix(nrow = 3*n, ncol = 3)) X[1:n, c(1,2)] = mvrnorm(n, c(0,0), SigmaS[[1]]) X[(n+1):(2*n), c(2, 3)] = mvrnorm(n, c(0,0), SigmaS[[2]]) X[(2*n+1):(3*n), c(1, 3)] = mvrnorm(n, c(0,0), SigmaS[[3]]) X = as.matrix(X) xxx = get_SigmaS(X)$patterns av_sigma = compute_av("var", X) X_new = X for (j in 1:3){ X_new[,j] = X[,j]/sqrt(av_sigma[j]) } V(get_SigmaS(X_new)$sigma_squared_S, xxx)
library(MASS) d = 3 n = 200 SigmaS=list() #Random 2x2 correlation matrices (necessarily consistent) for(j in 1:d){ x=runif(2,min=-1,max=1); y=runif(2,min=-1,max=1); SigmaS[[j]]=cov2cor(x%*%t(x) + y%*%t(y)) } X = data.frame(matrix(nrow = 3*n, ncol = 3)) X[1:n, c(1,2)] = mvrnorm(n, c(0,0), SigmaS[[1]]) X[(n+1):(2*n), c(2, 3)] = mvrnorm(n, c(0,0), SigmaS[[2]]) X[(2*n+1):(3*n), c(1, 3)] = mvrnorm(n, c(0,0), SigmaS[[3]]) X = as.matrix(X) xxx = get_SigmaS(X)$patterns av_sigma = compute_av("var", X) X_new = X for (j in 1:3){ X_new[,j] = X[,j]/sqrt(av_sigma[j]) } V(get_SigmaS(X_new)$sigma_squared_S, xxx)