### Install smcfcs Package Source: https://jwb133.r-universe.dev/smcfcs Commands to install the package from CRAN or the development repository. ```r install.packages("devtools") # devtools::install_github("jwb133/smcfcs") install.packages("smcfcs", repos = c('https://jwb133.r-universe.dev', 'https://cloud.r-project.org')) ``` -------------------------------- ### Inspect example data frame Source: https://jwb133.r-universe.dev/smcfcs/doc/smcfcs-vignette.Rmd Load the smcfcs package and display the first 10 rows of the ex_linquad dataset. ```r library(smcfcs) ex_linquad[1:10, ] ``` -------------------------------- ### Load and inspect the coarsening example dataset Source: https://jwb133.r-universe.dev/smcfcs/doc/coarsening.Rmd Load the smcfcs library and examine the structure of the ex_coarsening dataset. ```r library(smcfcs) summary(ex_coarsening) head(ex_coarsening) ``` -------------------------------- ### Advanced smcfcs Usage Examples Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html Examples demonstrating convergence monitoring, custom predictor matrices, interaction terms, and various regression types. ```R #examining convergence, using 100 iterations, setting m=1 imps <- smcfcs(ex_linquad, smtype="lm", smformula="y~z+x+I(x^2)", method=c("","","norm",""),m=1,numit=100) #convergence plot from first imputation for third coefficient of substantive model plot(imps$smCoefIter[1,3,]) #include auxiliary variable assuming it is conditionally independent of Y (which it is here) predMatrix <- array(0, dim=c(ncol(ex_linquad),ncol(ex_linquad))) predMatrix[3,] <- c(0,1,0,1) imps <- smcfcs(ex_linquad, smtype="lm", smformula="y~z+x+I(x^2)", method=c("","","norm",""),predictorMatrix=predMatrix) #impute missing x1 and x2, where they interact in substantive model imps <- smcfcs(ex_lininter, smtype="lm", smformula="y~x1+x2+x1*x2", method=c("","norm","logreg")) #logistic regression substantive model, with quadratic covariate effects imps <- smcfcs(ex_logisticquad, smtype="logistic", smformula="y~z+x+I(x^2)", method=c("","","norm","")) #Poisson regression substantive model imps <- smcfcs(ex_poisson, smtype="poisson", smformula="y~x+z", method=c("","norm","")) if (requireNamespace("mitools", quietly = TRUE)) { library(mitools) impobj <- imputationList(imps$impDatasets) models <- with(impobj, glm(y~x+z,family=poisson)) summary(MIcombine(models)) } ``` -------------------------------- ### Install smcfcs in R Source: https://jwb133.r-universe.dev/smcfcs Use this command to install the smcfcs package from the r-universe repository along with the standard CRAN repository. ```R install.packages('smcfcs', repos = c('https://jwb133.r-universe.dev', 'https://cloud.r-project.org')) ``` -------------------------------- ### Perform Nested Case-Control Imputation Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html Example workflow for setting up a predictor matrix, running the imputation, and combining results using mitools. ```R predictorMatrix <- matrix(0,nrow=dim(ex_ncc)[2],ncol=dim(ex_ncc)[2]) predictorMatrix[which(colnames(ex_ncc)=="x"),c(which(colnames(ex_ncc)=="z"))] <- 1 imps <- smcfcs.nestedcc(originaldata=ex_ncc,set="setno",nrisk="numrisk",event="d", smformula="Surv(t,case)~x+z+strata(setno)", method=c("", "", "logreg", "", "", "", "", ""), predictorMatrix=predictorMatrix) library(mitools) impobj <- imputationList(imps$impDatasets) models <- with(impobj, clogit(case~x+z+strata(setno))) summary(MIcombine(models)) ``` -------------------------------- ### Execute parallel imputation Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html Example usage of smcfcs.parallel to perform imputation on a competing risks dataset using multiple cores. ```R # Detect number of cores parallel::detectCores() imps <- smcfcs.parallel( smcfcs_func = "smcfcs", seed = 2021, n_cores = 2, originaldata = smcfcs::ex_compet, m = 10, smtype = "compet", smformula = list( "Surv(t, d == 1) ~ x1 + x2", "Surv(t, d == 2) ~ x1 + x2" ), method = c("", "", "norm", "norm") ) ``` -------------------------------- ### Perform Discrete Time Survival Analysis Imputation Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html Example of using smcfcs.dtsam to impute missing values and manually fit a discrete time survival model to the resulting datasets. ```R #the following example is not run when the package is compiled on CRAN #(to keep computation time down), but it can be run by package users ## Not run: #discrete time survival analysis example M <- 5 imps <- smcfcs.dtsam(ex_dtsam, "Surv(failtime,d)~x1+x2", method=c("logreg","", "", ""),m=M) #fit dtsam model to each dataset manually, since we need #to expand to person-period data form first ests <- vector(mode = "list", length = M) vars <- vector(mode = "list", length = M) for (i in 1:M) { longData <- survSplit(Surv(failtime,d)~x1+x2, data=imps$impDatasets[[i]], cut=unique(ex_dtsam$failtime[ex_dtsam$d==1])) mod <- glm(d~-1+factor(tstart)+x1+x2, family="binomial", data=longData) ests[[i]] <- coef(mod) vars[[i]] <- diag(vcov(mod)) } library(mitools) summary(MIcombine(ests,vars)) ## End(Not run) ``` ```R #the following example is not run when the package is compiled on CRAN #(to keep computation time down), but it can be run by package users ## Not run: #discrete time survival analysis example M <- 5 imps <- smcfcs.dtsam(ex_dtsam, "Surv(failtime,d)~x1+x2", method=c("logreg","", "", ""),m=M) #fit dtsam model to each dataset manually, since we need #to expand to person-period data form first ests <- vector(mode = "list", length = M) vars <- vector(mode = "list", length = M) for (i in 1:M) { longData <- survSplit(Surv(failtime,d)~x1+x2, data=imps$impDatasets[[i]], cut=unique(ex_dtsam$failtime[ex_dtsam$d==1])) mod <- glm(d~-1+factor(tstart)+x1+x2, family="binomial", data=longData) ests[[i]] <- coef(mod) vars[[i]] <- diag(vcov(mod)) } library(mitools) summary(MIcombine(ests,vars)) ## End(Not run) ``` -------------------------------- ### Access ex_finegray dataset Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html Load the simulated competing risks dataset. ```R ex_finegray ex_finegray ``` -------------------------------- ### Access ex_flexsurv dataset Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html Load the simulated time-to-event dataset with Weibull distribution and two covariates. ```R ex_flexsurv ex_flexsurv ``` -------------------------------- ### plot(x, include = "all", ...) Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html Visualises the contents of smCoefIter by plotting parameter estimates of the substantive model against the number of iterations from the imputation procedure. ```APIDOC ## plot(x, include = "all", ...) ### Description Visualises the contents of smCoefIter. Specifically, it plots the parameter estimates of the substantive model against the number of iterations from the imputation procedure. This is done for each regression coefficient, and each line corresponds to an imputed dataset. ### Arguments - **x** (smcfcs object) - Required - An object of class 'smcfcs'. - **include** (character vector) - Optional - Coefficient names for which to return the convergence plot. Default is "all". - **...** (dots) - Optional - Additional parameters to pass on to ggplot2::facet_wrap(). ### Value A ggplot2 object, containing the convergence plots, facetted per covariate in the substantive model. ``` -------------------------------- ### Examine Convergence and Auxiliary Variables Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html Checks convergence of the imputation process and demonstrates the use of auxiliary variables. ```R #examining convergence, using 100 iterations, setting m=1 imps <- smcfcs(ex_linquad, smtype="lm", smformula="y~z+x+I(x^2)", method=c("","","norm",""),m=1,numit=100) #convergence plot from first imputation for third coefficient of substantive model plot(imps$smCoefIter[1,3,]) #include auxiliary variable assuming it is conditionally independent of Y (which it is here) predMatrix <- array(0, dim=c(ncol(ex_linquad),ncol(ex_linquad))) predMatrix[3,] <- c(0,1,0,1) imps <- smcfcs(ex_linquad, smtype="lm", smformula="y~z+x+I(x^2)", method=c("","","norm",""),predictorMatrix=predMatrix) ``` -------------------------------- ### Access ex_coxquad dataset Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html Load the simulated dataset containing time-to-event outcomes with quadratic covariate effects. ```R ex_coxquad ex_coxquad ``` -------------------------------- ### Perform Multiple Imputation with smcfcs.flexsurv Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html Demonstrates how to use smcfcs.flexsurv for covariate imputation, combining results with mitools, and imputing event times with optional censoring constraints. ```R set.seed(63213) imps <- smcfcs.flexsurv(ex_flexsurv, k=2, smformula="Surv(t,d)~x+z", method=c("","","logreg","")) library(mitools) impobj <- imputationList(imps$impDatasets) models <- with(impobj, flexsurvspline(Surv(t,d)~x+z, k=2)) summary(MIcombine(models)) # now impute event times as well as missing covariates imps <- smcfcs.flexsurv(ex_flexsurv, k=2, smformula="Surv(t,d)~x+z", method=c("","","logreg",""), imputeTimes=TRUE) # now impute event times as well as missing covariates, # but setting max observed event time to 2 imps <- smcfcs.flexsurv(ex_flexsurv, k=2, smformula="Surv(t,d)~x+z", method=c("","","logreg",""), imputeTimes=TRUE, censtime=2) ``` -------------------------------- ### Simulate replication dataset Source: https://jwb133.r-universe.dev/smcfcs/doc/coverror.Rmd Generates a dataset where a covariate x is unobserved, but two error-prone measurements w1 and w2 are available for a subset of subjects. ```r x <- rnorm(n) w1 <- x + rnorm(n) w2 <- x + rnorm(n) w2[(n * 0.1):n] <- NA y <- x + rnorm(n) x <- rep(NA, n) simData <- data.frame(x, w1, w2, y) ``` -------------------------------- ### Access ex_dtsam dataset Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html Load the simulated discrete time survival dataset. ```R ex_dtsam ex_dtsam ``` -------------------------------- ### Access ex_lininter dataset Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html Load the simulated continuous outcome dataset featuring interaction between two partially observed covariates. ```R ex_lininter ex_lininter ``` -------------------------------- ### Perform Cox and Competing Risks Imputation Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html Demonstrates imputation for Cox regression and competing risks models. Requires the survival package. ```R if (requireNamespace("survival", quietly = TRUE)) { imps <- smcfcs(ex_coxquad, smtype="coxph", smformula="Surv(t,d)~z+x+I(x^2)", method=c("","","","norm","")) #competing risks substantive model, with only main covariate effects imps <- smcfcs(ex_compet, smtype="compet", smformula=c("Surv(t,d==1)~x1+x2", "Surv(t,d==2)~x1+x2"), method=c("","","logreg","norm")) } ``` -------------------------------- ### Verify imputed datasets Source: https://jwb133.r-universe.dev/smcfcs/doc/coarsening.Rmd Inspect the first imputed dataset and validate that the imputed values respect the coarsening constraints. ```r head(imps$impDatasets[[1]]) ``` ```r table(imps$impDatasets[[1]]$x,imps$impDatasets[[1]]$xobs,useNA = "ifany") ``` -------------------------------- ### Access ex_linquad dataset Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html Load the simulated continuous outcome dataset with quadratic effects from a partially observed covariate. ```R ex_linquad ex_linquad ``` -------------------------------- ### Pool results using mitools Source: https://jwb133.r-universe.dev/smcfcs/doc/coverror.Rmd Fits the substantive model to the imputed datasets and pools the results using Rubin's rules via the mitools package. ```r library(mitools) impobj <- imputationList(imps$impDatasets) models <- with(impobj, lm(y ~ x)) summary(MIcombine(models)) ``` -------------------------------- ### Assess convergence with smcfcs Source: https://jwb133.r-universe.dev/smcfcs/doc/smcfcs-vignette.Rmd Run the imputation model with a single imputation and increased iterations to visualize parameter convergence. ```r # impute once with a larger number of iterations than the default 10 imps <- smcfcs(ex_linquad, smtype = "lm", smformula = "y~z+x+I(x^2)", method = c("", "", "norm", ""), predictorMatrix = predMatrix, m = 1, numit = 100) # plot estimates of the parameters of the substantive model against iteration number plot(imps) ``` -------------------------------- ### Combine Imputed Models with mitools Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html Uses the mitools package to combine results from imputed datasets for survival models. ```R if (requireNamespace("mitools", quietly = TRUE)) { library(mitools) impobj <- imputationList(imps$impDatasets) models <- with(impobj, coxph(Surv(t,d==1)~x1+x2)) summary(MIcombine(models)) } ``` -------------------------------- ### Examine convergence with iteration plots Source: https://jwb133.r-universe.dev/smcfcs/doc/coverror.Rmd Re-runs imputation with a higher number of iterations to check for convergence stability. ```r imps <- smcfcs(simData, smtype = "lm", smformula = "y~x", method = c("latnorm", "", "", ""), m = 1, numit = 100, errorProneMatrix = errMat ) plot(imps) ``` -------------------------------- ### Access Case Cohort Data Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html Load the simulated case cohort dataset containing 1571 rows and 7 variables. ```R ex_cc ex_cc ``` -------------------------------- ### Perform initial imputation with smcfcs Source: https://jwb133.r-universe.dev/smcfcs/doc/coverror.Rmd Attempts to impute missing x values using smcfcs without explicitly conditioning on the error-prone measurement w. ```r library(smcfcs) imps <- smcfcs(simData, smtype = "lm", smformula = "y~x", method = c("norm", "", ""), m = 5 ) ``` -------------------------------- ### Plot convergence for smcfcs object Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html Visualizes parameter estimates from the substantive model across imputation iterations. Requires the ggplot2 library. ```R ## Not run: # Use simulated competing risks example in package imps <- smcfcs( originaldata = ex_compet, smtype = "compet", smformula = list( "Surv(t, d == 1) ~ x1 + x2", "Surv(t, d == 2) ~ x1 + x2" ), method = c("", "", "norm", "norm") ) plot(imps) plot(imps, include = c("x1-cause1", "x2-cause2")) ## End(Not run) ``` ```R ## Not run: # Use simulated competing risks example in package imps <- smcfcs( originaldata = ex_compet, smtype = "compet", smformula = list( "Surv(t, d == 1) ~ x1 + x2", "Surv(t, d == 2) ~ x1 + x2" ), method = c("", "", "norm", "norm") ) plot(imps) plot(imps, include = c("x1-cause1", "x2-cause2")) ## End(Not run) ``` -------------------------------- ### Access nested case-control simulation data Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html Load the dataset containing simulated nested case-control data for survival analysis. ```R ex_ncc ex_ncc ``` -------------------------------- ### Access Poisson regression simulation data Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html Load the dataset containing a count outcome modeled using Poisson regression. ```R ex_poisson ex_poisson ``` -------------------------------- ### Analyze imputed datasets Source: https://jwb133.r-universe.dev/smcfcs/doc/coverror.Rmd Combines results from multiple imputed datasets using MIcombine. ```r impobj <- imputationList(imps$impDatasets) models <- with(impobj, lm(y ~ x)) summary(MIcombine(models)) ``` -------------------------------- ### Perform Fine-Gray model imputation Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html Imputes missing data using the Fine-Gray model and pools results using mitools. ```R ## Not run: library(survival) library(kmi) imps <- smcfcs.finegray( originaldata = ex_finegray, smformula = "Surv(times, d) ~ x1 + x2", method = c("", "", "logreg", "norm"), cause = 1, kmi_args = list("formula" = ~ 1) ) if (requireNamespace("mitools", quietly = TRUE)) { library(mitools) impobj <- imputationList(imps$impDatasets) # Important: use Surv(newtimes, newevent) ~ ... when pooling # (respectively: subdistribution time and indicator for cause of interest) models <- with(impobj, coxph(Surv(newtimes, newevent) ~ x1 + x2)) summary(MIcombine(models)) } ## End(Not run) ``` -------------------------------- ### Access Competing Risks Data Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html Load the simulated competing risks dataset with partially observed covariates. ```R ex_compet ex_compet ``` -------------------------------- ### Access logistic quadratic simulation data Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html Load the dataset containing a binary outcome with quadratic covariate effects. ```R ex_logisticquad ex_logisticquad ``` -------------------------------- ### Simulate dataset with internal validation data Source: https://jwb133.r-universe.dev/smcfcs/doc/coverror.Rmd Generates a dataset where the true covariate x is partially observed and an error-prone measurement w is available for all subjects. ```r set.seed(1234) n <- 1000 x <- rnorm(n) w <- x + rnorm(n) y <- x + rnorm(n) x[(n * 0.1):n] <- NA simData <- data.frame(x, w, y) ``` -------------------------------- ### smcfcs.parallel(smcfcs_func, seed, m, n_cores, cl_type, outfile, ...) Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html Executes substantive model compatible imputation using parallel processing. ```APIDOC ## smcfcs.parallel ### Description Runs substantive model compatible imputation using parallel cores to reduce computation time. ### Arguments - **smcfcs_func** (string) - Optional - Specifies which base smcfcs function to call (e.g., 'smcfcs', 'smcfcs.casecohort', 'smcfcs.dtasam', 'smcfcs.nestedcc'). Defaults to 'smcfcs'. - **seed** (integer) - Optional - Seed for random number generation. - **m** (integer) - Optional - Number of imputed datasets to generate. Defaults to 5. - **n_cores** (integer) - Optional - Number of cores to use. Defaults to parallel::detectCores() - 1. - **cl_type** (string) - Optional - Cluster type, either "PSOCK" or "FORK". Defaults to "PSOCK". - **outfile** (string) - Optional - Path to a file for worker output. - **...** (any) - Optional - Additional arguments passed to the underlying smcfcs function. ### Value Returns an object of type "smcfcs". ``` -------------------------------- ### Access Coarsened Factor Data Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html Load the simulated dataset featuring a factor covariate subject to coarsening. ```R ex_coarsening ex_coarsening ``` -------------------------------- ### Define imputation restrictions Source: https://jwb133.r-universe.dev/smcfcs/doc/coarsening.Rmd Create a list of restriction rules mapping coarsened observations to valid factor levels for the smcfcs algorithm. ```r restrictionsX = c("xobs = a/c ~ a + c", "xobs = b/c ~ b + c") restrictions = append(list(restrictionsX), as.list(c("", "", ""))) ``` -------------------------------- ### BibTeX Citation for smcfcs Source: https://jwb133.r-universe.dev/smcfcs/citation Use this BibTeX entry to cite the smcfcs package in LaTeX documents. ```bibtex @Manual{, title = {smcfcs: Multiple Imputation of Covariates by Substantive Model Compatible Fully Conditional Specification}, author = {Jonathan Bartlett and Ruth Keogh and Edouard F. Bonneville and Lars {van der Burg}}, year = {2026}, note = {R package version 2.0.2}, url = {https://github.com/jwb133/smcfcs}, } ``` -------------------------------- ### smcfcs.finegray(originaldata, smformula, method, cause, m, numit, rjlimit, kmi_args, ...) Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html Multiply imputes missing covariate values using substantive model compatible fully conditional specification for competing risks outcomes. ```APIDOC ## smcfcs.finegray ### Description Multiply imputes missing covariate values using substantive model compatible fully conditional specification for competing risks outcomes, when the substantive model is a Fine-Gray model for the subdistribution hazard of one event. ### Parameters - **originaldata** (data.frame) - Required - The original data frame with missing values. - **smformula** (string) - Required - The formula of the substantive model, e.g., "Surv(t, d) ~ x1 + x2". - **method** (vector of strings) - Required - Specifies the type of regression model to be used to impute each variable (e.g., "norm", "logreg", "poisson"). - **cause** (numeric) - Optional - Designating the competing event of interest (default is 1). - **m** (numeric) - Optional - The number of imputed datasets to generate (default is 5). - **numit** (numeric) - Optional - The number of iterations to run when generating each imputation (default is 10). - **rjlimit** (numeric) - Optional - Maximum number of attempts for rejection sampling (default is 5000). - **kmi_args** (list) - Optional - Arguments passed to kmi for imputing potential censoring times. - **...** - Optional - Additional arguments to pass on to smcfcs. ### Returns - **object** - An object of type "smcfcs". ``` -------------------------------- ### Impute multiple error-prone covariates Source: https://jwb133.r-universe.dev/smcfcs/doc/coverror.Rmd Demonstrates handling multiple covariates, each with its own set of error-prone measurements and distinct error variances. ```r x <- rnorm(n) x1 <- x + rnorm(n) x2 <- x + rnorm(n) w2[(n * 0.1):n] <- NA z <- x + rnorm(n) z1 <- z + 0.1 * rnorm(n) z2 <- z + 0.1 * rnorm(n) y <- x - z + rnorm(n) x <- rep(NA, n) z <- rep(NA, n) simData <- data.frame(x, x1, x2, z, z1, z2, y) errMat <- array(0, dim = c(7, 7)) errMat[1, c(2, 3)] <- 1 errMat[4, c(5, 6)] <- 1 imps <- smcfcs(simData, smtype = "lm", smformula = "y~x+z", method = c("latnorm", "", "", "latnorm", "", "", ""), m = 5, errorProneMatrix = errMat ) ``` -------------------------------- ### Define smcfcs.nestedcc Usage Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html Standard function signature for initializing the nested case-control imputation process. ```R smcfcs.nestedcc(originaldata, smformula, method, set, event, nrisk, ...) smcfcs.nestedcc(originaldata, smformula, method, set, event, nrisk, ...) ``` -------------------------------- ### Perform multiple imputation with restrictions Source: https://jwb133.r-universe.dev/smcfcs/doc/coarsening.Rmd Run the smcfcs imputation model using the defined restrictions to account for partial information. ```r set.seed(68204812) imps <- smcfcs(originaldata=ex_coarsening, smtype="lm", smformula = "y~z+x", method = c("mlogit","", "", ""), restrictions = restrictions ) ``` -------------------------------- ### Inspect imputed data Source: https://jwb133.r-universe.dev/smcfcs/doc/coverror.Rmd Summarizes the first imputed dataset to verify that missing values in both the covariate and the error-prone replicate have been filled. ```r summary(imps$impDatasets[[1]]) ``` -------------------------------- ### smcfcs.casecohort(originaldata, smformula, method, sampfrac, in.subco, ...) Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html Multiply imputes missing covariate values using substantive model compatible fully conditional specification for case cohort studies. ```APIDOC ## smcfcs.casecohort ### Description Multiply imputes missing covariate values using substantive model compatible fully conditional specification for case cohort studies. ### Parameters - **originaldata** (data.frame) - Required - The case-cohort data set. - **smformula** (formula) - Required - A formula of the form "Surv(entertime,t,d)~x". - **method** (vector of strings) - Required - Specifies the regression model type for each variable (e.g., "norm", "logreg", "poisson"). - **sampfrac** (numeric) - Required - The proportion of individuals from the underlying full cohort who are in the subcohort. - **in.subco** (string) - Required - The name of a column in the dataset with 0/1s that indicates whether the subject is in the subcohort. - **...** (dots) - Optional - Additional arguments to pass on to smcfcs. ``` -------------------------------- ### Perform imputation with predictor matrix Source: https://jwb133.r-universe.dev/smcfcs/doc/coverror.Rmd Runs the smcfcs imputation process while incorporating the custom predictor matrix to condition on w. ```r imps <- smcfcs(simData, smtype = "lm", smformula = "y~x", method = c("norm", "", ""), m = 5, predictorMatrix = predMat ) ``` -------------------------------- ### Perform Discrete Time Survival Analysis Imputation Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html Imputes missing data for discrete time survival analysis and manually fits models to expanded person-period data. ```R M <- 5 imps <- smcfcs(ex_dtsam, "dtsam", "Surv(failtime,d)~x1+x2", method=c("logreg","", "", ""),m=M) #fit dtsam model to each dataset manually, since we need #to expand to person-period data form first ests <- vector(mode = "list", length = M) vars <- vector(mode = "list", length = M) for (i in 1:M) { longData <- survSplit(Surv(failtime,d)~x1+x2, data=imps$impDatasets[[i]], cut=unique(ex_dtsam$failtime[ex_dtsam$d==1])) mod <- glm(d~-1+factor(tstart)+x1+x2, family="binomial", data=longData) ests[[i]] <- coef(mod) vars[[i]] <- diag(vcov(mod)) } summary(MIcombine(ests,vars)) ``` -------------------------------- ### Perform Logistic and Poisson Regression Imputation Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html Imputes missing data for logistic and Poisson regression models. ```R #logistic regression substantive model, with quadratic covariate effects imps <- smcfcs(ex_logisticquad, smtype="logistic", smformula="y~z+x+I(x^2)", method=c("","","norm","")) #Poisson regression substantive model imps <- smcfcs(ex_poisson, smtype="poisson", smformula="y~x+z", method=c("","norm","")) if (requireNamespace("mitools", quietly = TRUE)) { library(mitools) impobj <- imputationList(imps$impDatasets) models <- with(impobj, glm(y~x+z,family=poisson)) summary(MIcombine(models)) } ``` -------------------------------- ### Perform multiple imputation with smcfcs Source: https://jwb133.r-universe.dev/smcfcs/doc/smcfcs-vignette.Rmd Impute missing values in a dataset using a linear regression substantive model with quadratic covariate effects. ```r set.seed(123) # impute missing values in x, compatibly with quadratic substantive model imps <- smcfcs(originaldata=ex_linquad, smtype = "lm", smformula = "y~z+x+I(x^2)", method = c("", "", "norm", "")) ``` -------------------------------- ### Perform multiple imputation with smcfcs Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html The function requires the original data, substantive model type, formula, and imputation methods for each variable. ```R smcfcs( originaldata, smtype, smformula, method, predictorMatrix = NULL, m = 5, numit = 10, rjlimit = 1000, noisy = FALSE, errorProneMatrix = NULL, restrictions = NULL ) ``` -------------------------------- ### smcfcs.dtsam(originaldata, smformula, method, timeEffects = "factor", ...) Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html Multiply imputes missing covariate values using substantive model compatible fully conditional specification for discrete time survival analysis. ```APIDOC ## smcfcs.dtsam ### Description Multiply imputes missing covariate values using substantive model compatible fully conditional specification for discrete time survival analysis. ### Parameters - **originaldata** (data.frame) - Required - The data in wide form (one row per subject). - **smformula** (formula) - Required - A formula of the form "Surv(t,d)~x1+x2+x3", where t is the discrete time variable, d is the binary event indicator, and the covariates should not include time. - **method** (vector of strings) - Required - Specifies for each variable either that it does not need to be imputed (" "), or the type of regression model to be used (e.g., "norm", "logreg", "brlogreg", "poisson", "podds", "mlogit"). - **timeEffects** (string) - Optional - Specifies how the effect of time is modelled. Defaults to "factor". Options include "factor", "linear", or "quad". - **...** (dots) - Optional - Additional arguments to pass on to smcfcs. ### Usage Example ```R imps <- smcfcs.dtsam(ex_dtsam, "Surv(failtime,d)~x1+x2", method=c("logreg","", "", ""), m=5) ``` ``` -------------------------------- ### smcfcs(originaldata, smtype, smformula, method, ...) Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html Multiply imputes missing covariate values using substantive model compatible fully conditional specification. ```APIDOC ## smcfcs ### Description Multiply imputes missing covariate values using substantive model compatible fully conditional specification. ### Parameters - **originaldata** (data.frame) - Required - The original data frame with missing values. - **smtype** (string) - Required - Type of substantive model ("lm", "logistic", "brlogistic", "poisson", "weibull", "coxph", "compet"). - **smformula** (formula) - Required - The formula of the substantive model. - **method** (vector) - Required - Vector of strings specifying imputation models for each variable. - **predictorMatrix** (matrix) - Optional - Defines which covariates are used as predictors in imputation models. - **m** (integer) - Optional - Number of imputed datasets to generate (default 5). - **numit** (integer) - Optional - Number of iterations to run (default 10). - **rjlimit** (integer) - Optional - Maximum attempts for rejection sampling (default 1000). - **noisy** (logical) - Optional - Whether output should be noisy (default FALSE). - **errorProneMatrix** (matrix) - Optional - Matrix indicating variables measured with classical measurement error. - **restrictions** (string) - Optional - Specifies restrictions for handling coarsened factor level covariates. ``` -------------------------------- ### Discrete Time Survival Analysis Imputation Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html Performs multiple imputation for discrete time survival data and manually fits models to each imputed dataset. ```R M <- 5 imps <- smcfcs(ex_dtsam, "dtsam", "Surv(failtime,d)~x1+x2", method=c("logreg","", "", ""),m=M) #fit dtsam model to each dataset manually, since we need #to expand to person-period data form first ests <- vector(mode = "list", length = M) vars <- vector(mode = "list", length = M) for (i in 1:M) { longData <- survSplit(Surv(failtime,d)~x1+x2, data=imps$impDatasets[[i]], cut=unique(ex_dtsam$failtime[ex_dtsam$d==1])) mod <- glm(d~-1+factor(tstart)+x1+x2, family="binomial", data=longData) ests[[i]] <- coef(mod) vars[[i]] <- diag(vcov(mod)) } summary(MIcombine(ests,vars)) ``` -------------------------------- ### Impute using predictorMatrix for auxiliary variables Source: https://jwb133.r-universe.dev/smcfcs/doc/smcfcs-vignette.Rmd Uses a predictor matrix to include auxiliary variables in the imputation process without adding them to the substantive model formula. ```r predMatrix <- array(0, dim = c(ncol(ex_linquad), ncol(ex_linquad))) predMatrix[3, ] <- c(0, 1, 0, 1) imps <- smcfcs(ex_linquad, smtype = "lm", smformula = "y~z+x+I(x^2)", method = c("", "", "norm", ""), predictorMatrix = predMatrix) impobj <- imputationList(imps$impDatasets) models <- with(impobj, lm(y ~ z + x + I(x^2))) summary(MIcombine(models)) ``` -------------------------------- ### smcfcs.nestedcc(originaldata, smformula, method, set, event, nrisk, ...) Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html Multiply imputes missing covariate values using substantive model compatible fully conditional specification for nested case control studies. ```APIDOC ## smcfcs.nestedcc ### Description Multiply imputes missing covariate values using substantive model compatible fully conditional specification for nested case control studies. ### Parameters - **originaldata** (data.frame) - Required - The nested case-control data set. - **smformula** (string) - Required - A formula of the form "Surv(t,case)~x+strata(set)", where case is case-control indicator, t is the event or censoring time. - **method** (vector) - Required - A vector of strings specifying for each variable the type of regression model to be used to impute (e.g., "norm", "logreg", "poisson"). - **set** (string) - Required - Variable identifying matched sets in nested case-control study. - **event** (string) - Required - Variable which indicates who is a case/control in the nested case-control sample. - **nrisk** (string) - Required - Variable which is the number at risk in the underlying full cohort at the event time for the case in each matched set. - **...** (dots) - Optional - Additional arguments to pass on to smcfcs. ``` -------------------------------- ### Define flexible parametric survival model imputation Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html Specifies the function signature for imputing missing covariates and event times using Royston-Parmar models. ```R smcfcs.flexsurv( originaldata, smformula, method, k = 2, imputeTimes = FALSE, censtime = NULL, originalKnots = TRUE, ... ) ``` -------------------------------- ### Case-Cohort Study Imputation Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html Imputes missing covariates in case-cohort studies and pools results using mitools. ```R imps <- smcfcs.casecohort(ex_cc, smformula="Surv(entertime, t, d)~x+z", sampfrac=0.1, in.subco="in.subco", method=c("", "", "norm", "", "", "", "")) library(mitools) impobj <- imputationList(imps$impDatasets) models <- with(impobj, coxph(Surv(entertime,t,d)~x+z+cluster(id))) summary(MIcombine(models)) ``` -------------------------------- ### smcfcs.flexsurv Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html Multiply imputes missing covariate values and event times using substantive model compatible fully conditional specification with a Royston-Parmar flexible parametric survival model. ```APIDOC ## smcfcs.flexsurv ### Description Multiply imputes missing covariate values and event times using substantive model compatible fully conditional specification with a Royston-Parmar flexible parametric survival model. ### Arguments - **originaldata** (data.frame) - The original data frame with missing values. - **smformula** (formula) - A formula of the form "Surv(t,d)~x+z". - **method** (vector) - A required vector of strings specifying for each variable either that it does not need to be imputed (" "), or the type of regression model to be used to impute (e.g., "norm", "logreg", "brlogreg", "poisson", "podds", "mlogit"). - **k** (numeric) - Number of knots to use in the flexible parametric survival model (default: 2). - **imputeTimes** (boolean) - If set to TRUE, smcfcs.flexsurv will impute censored survival times, as well as any missing covariates. - **censtime** (numeric/vector) - Value(s) to use for censoring of imputed event times. - **originalKnots** (boolean) - If imputing censored event times, setting originalKnots=TRUE means the automatically chosen knot locations from the model fitted to the observed times are used throughout. - **...** - Additional arguments to pass on to smcfcs. ``` -------------------------------- ### Fit substantive model with quadratic effect Source: https://jwb133.r-universe.dev/smcfcs/doc/smcfcs-vignette.Rmd Fits a linear regression model including a quadratic term using the imputed datasets generated by smcfcs. ```r # fit substantive model impobj <- imputationList(imps$impDatasets) models <- with(impobj, lm(y ~ z + x + I(x^2))) summary(MIcombine(models)) ``` -------------------------------- ### Analyze multiple covariate model Source: https://jwb133.r-universe.dev/smcfcs/doc/coverror.Rmd Performs analysis on datasets imputed with multiple error-prone covariates. ```r impobj <- imputationList(imps$impDatasets) models <- with(impobj, lm(y ~ x + z)) summary(MIcombine(models)) ``` -------------------------------- ### Examine coarsened variable distribution Source: https://jwb133.r-universe.dev/smcfcs/doc/coarsening.Rmd Cross-tabulate the true factor variable with the observed coarsened information. ```r table(ex_coarsening$x,ex_coarsening$xobs,useNA = "ifany") ``` -------------------------------- ### smcfcs.flexsurv Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html Fits a flexible parametric proportional hazards survival model to impute missing data for time-to-event outcomes. ```APIDOC ## smcfcs.flexsurv ### Description Performs multiple imputation for time-to-event outcomes using a flexible parametric proportional hazards survival model. It allows for the imputation of missing covariates and optionally event times. ### Parameters - **originaldata** (data.frame) - Required - The dataset containing missing values. - **k** (integer) - Required - The number of knots for the flexible parametric model. - **smformula** (formula) - Required - The survival formula for the model. - **method** (vector) - Required - A vector of imputation methods for each variable. - **imputeTimes** (boolean) - Optional - Whether to impute event times for censored individuals. - **censtime** (numeric/vector) - Optional - A potential censoring time or vector of times for imputation. ### Usage Example ```r imps <- smcfcs.flexsurv(ex_flexsurv, k=2, smformula="Surv(t,d)~x+z", method=c("","","logreg",""), imputeTimes=TRUE, censtime=2) ``` ``` -------------------------------- ### Impute using auxiliary variables in substantive model Source: https://jwb133.r-universe.dev/smcfcs/doc/smcfcs-vignette.Rmd Includes an auxiliary variable in the imputation model by adding it to the substantive model formula, then omitting it for the final analysis. ```r # impute, including v as a covariate in the substantive/outcome model imps <- smcfcs(ex_linquad, smtype = "lm", smformula = "y~z+x+I(x^2)+v", method = c("", "", "norm", "")) # fit substantive model, which omits v impobj <- imputationList(imps$impDatasets) models <- with(impobj, lm(y ~ z + x + I(x^2))) summary(MIcombine(models)) ``` -------------------------------- ### Impute covariate with error using smcfcs Source: https://jwb133.r-universe.dev/smcfcs/doc/coverror.Rmd Configures the errorProneMatrix and performs imputation using the latnorm method. ```r errMat <- array(0, dim = c(4, 4)) errMat[1, c(2, 3)] <- 1 imps <- smcfcs(simData, smtype = "lm", smformula = "y~x", method = c("latnorm", "", "", ""), m = 5, errorProneMatrix = errMat ) ``` -------------------------------- ### Define smcfcs.parallel function signature Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html The function signature for configuring parallel imputation parameters including core count and cluster type. ```R smcfcs.parallel( smcfcs_func = "smcfcs", seed = NULL, m = 5, n_cores = parallel::detectCores() - 1, cl_type = "PSOCK", outfile = "", ... ) ``` -------------------------------- ### Define predictor matrix for imputation Source: https://jwb133.r-universe.dev/smcfcs/doc/coverror.Rmd Creates a predictor matrix to ensure the error-prone measurement w is used as a predictor when imputing the true covariate x. ```r predMat <- array(0, dim = c(3, 3)) predMat[1, 2] <- 1 ``` -------------------------------- ### Impute missing covariates with smcfcs.finegray Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html Use this function to impute missing covariate values for competing risks outcomes when the substantive model is a Fine-Gray model. ```R smcfcs.finegray( originaldata, smformula, method, cause = 1, m = 5, numit = 10, rjlimit = 5000, kmi_args = list(formula = ~1, bootstrap = FALSE, nboot = 10), ... ) ``` -------------------------------- ### Perform Linear Substantive Model Imputation Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html Imputes missing values for a linear model with quadratic effects and combines results using Rubin's rules. ```R set.seed(123) #linear substantive model with quadratic covariate effect imps <- smcfcs(ex_linquad, smtype="lm", smformula="y~z+x+I(x^2)", method=c("","","norm","")) #if mitools is installed, fit substantive model to imputed datasets #and combine results using Rubin's rules if (requireNamespace("mitools", quietly = TRUE)) { library(mitools) impobj <- imputationList(imps$impDatasets) models <- with(impobj, lm(y~z+x+I(x^2))) summary(MIcombine(models)) } ``` -------------------------------- ### Impute missing values with linear model Source: https://jwb133.r-universe.dev/smcfcs/doc/smcfcs-vignette.Rmd Performs multiple imputation for a dataset where the substantive model is linear, but the imputation model may be misspecified regarding non-linear effects. ```r # impute missing values in x, compatibly with model for y which omits the quadratic effect imps <- smcfcs(ex_linquad, smtype = "lm", smformula = "y~z+x", method = c("", "", "norm", "")) ``` -------------------------------- ### Impute Interacting Covariates Source: https://jwb133.r-universe.dev/smcfcs/doc/manual.html Handles missing values for covariates that interact within the substantive model. ```R #impute missing x1 and x2, where they interact in substantive model imps <- smcfcs(ex_lininter, smtype="lm", smformula="y~x1+x2+x1*x2", method=c("","norm","logreg")) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.