library(MASS)
# Number of subjects
n <- 30
# Number of total covariates
p <- 4
# Number of missing groups of subjects
ngroup <- 2
# Number of data sources
nsource <- 2
# Starting indexes of covariates in data sources
cov_index=c(1, 3)
# Starting indexes of subjects in missing groups
sub_index=c(1, 16)
# Indexes of missing data sources in missing groups, respectively ('NULL' represents no missing)
miss_source=list(NULL, 1)
# Indicator of whether there is a group of complete cases. If there is a group of complete cases,
# it should be the first group.
complete=TRUE
# Create a block-wise missing design matrix X and response vector y
set.seed(1)
sigma=diag(1-0.4,p,p)+matrix(0.4,p,p)
X <- mvrnorm(n,rep(0,p),sigma)
beta_true <- c(2.5, 0, 3, 0)
y <- rnorm(n) + X%*%beta_true
for (i in 1:ngroup) {
if (!is.null(miss_source[[i]])) {
if (i==ngroup) {
if (miss_source[[i]]==nsource) {
X[sub_index[i]:n, cov_index[miss_source[[i]]]:p] = NA
} else {
X[sub_index[i]:n, cov_index[miss_source[[i]]]:(cov_index[miss_source[[i]]+1]-1)] = NA
}
} else {
if (miss_source[[i]]==nsource) {
X[sub_index[i]:(sub_index[i+1]-1), cov_index[miss_source[[i]]]:p] = NA
} else {
X[sub_index[i]:(sub_index[i+1]-1), cov_index[miss_source[[i]]]:
(cov_index[miss_source[[i]]+1]-1)] = NA
}
}
}
}
# Now we can use the function with this simulated data
#start.time = proc.time()
result <- MBI(X=X, y=y, cov_index=cov_index, sub_index=sub_index, miss_source=miss_source,
complete=complete, nlam = 15, eps2 = 1e-3, h1=2^(-(8:20)))
#time = proc.time() - start.time
theta=result$beta
bic1=result$bic1
best=which.min(bic1[bic1!=0])
beta_est=theta[best,]
Run the code above in your browser using DataLab