MyNixOS website logo
Description

Functions for Stochastic Search Variable Selection (SSVS).

Functions for performing stochastic search variable selection (SSVS) for binary and continuous outcomes and visualizing the results. SSVS is a Bayesian variable selection method used to estimate the probability that individual predictors should be included in a regression model. Using MCMC estimation, the method samples thousands of regression models in order to characterize the model uncertainty regarding both the predictor set and the regression parameters. For details see Bainter, McCauley, Wager, and Losin (2020) Improving practices for selecting a subset of important predictors in psychology: An application to predicting pain, Advances in Methods and Practices in Psychological Science 3(1), 66-80 <DOI:10.1177/2515245919885617>.

SSVS

R-CMD-check

The goal of {SSVS} is to provide functions for performing stochastic search variable selection (SSVS) for binary and continuous outcomes and visualizing the results. SSVS is a Bayesian variable selection method used to estimate the probability that individual predictors should be included in a regression model. Using MCMC estimation, the method samples thousands of regression models in order to characterize the model uncertainty regarding both the predictor set and the regression parameters.

Installation

You can install the development version of {SSVS} from GitHub with:

# install.packages("remotes")
remotes::install_github("sabainter/SSVS")

Example 1 - continuous response variable

Consider a simple example using SSVS on the mtcars dataset to predict quarter mile times. We first specify our response variable (“qsec”), then choose our predictors and run the ssvs() function.

library(SSVS)
set.seed(1000)
outcome <- 'qsec'
predictors <- c('cyl', 'disp', 'hp', 'drat', 'wt',
 'vs', 'am', 'gear', 'carb','mpg')

results <- ssvs(data = mtcars, x = predictors, y = outcome, prior.probs = .5, progress = FALSE)

The results can be summarized and printed using the summary() function. This will display the MIP for each predictor, the average coefficients including and excluding zeros, and credible intervals for each coefficient.

summary_results <- summary(results, interval = 0.9, ordered = TRUE)
VariableMIPAvg BetaAvg Nonzero BetaLower CI (90%)Upper CI (90%)
wt0.86101.08671.26210.00001.9848
vs0.78110.67580.86520.00001.2105
hp0.5624-0.5301-0.9425-1.35030.0000
disp0.4148-0.4669-1.1257-1.84660.0013
carb0.4077-0.3031-0.7435-1.02440.0000
cyl0.4030-0.4390-1.0894-1.71330.0000
am0.3878-0.2710-0.6988-1.00900.0000
gear0.1889-0.0804-0.4256-0.49250.0000
mpg0.16750.06230.37210.00000.4615
drat0.0881-0.0134-0.15230.00000.0000

The MIPs for each predictor can then be visualized using the plot() function.

plot(results)

Example 2 - binary response variable

In the example above, the response variable was a continuous variable. The same workflow can be used for binary variables by specifying continuous = FALSE to the ssvs() function.

As an example, let’s create a binary variable:

library(AER)
#> Warning: package 'AER' was built under R version 4.5.2
data(Affairs)
Affairs$hadaffair[Affairs$affairs > 0] <- 1
Affairs$hadaffair[Affairs$affairs == 0] <- 0

Then define the outcome and predictors.

outcome <- "hadaffair"
predictors <- c("gender", "age", "yearsmarried", "children", "religiousness", "education", "occupation", "rating")

And finally run the model:

set.seed(1000)
results <- ssvs(data = Affairs, x = predictors, y = outcome, continuous = FALSE, progress = FALSE)

Now the results can be summarized or visualized in the same manner.

summary_results <- summary(results, interval = 0.9, ordered = TRUE)
VariableMIPAvg BetaAvg Nonzero BetaLower CI (90%)Upper CI (90%)
rating1.0000-0.5521-0.5521-0.7095-0.3866
religiousness0.4698-0.1588-0.3379-0.42680.0000
yearsmarried0.14770.04730.32060.00000.2665
children0.09530.02700.28330.00000.0000
age0.0147-0.0029-0.19900.00000.0000
gender0.01150.00150.13060.00000.0000
occupation0.00950.00090.09540.00000.0000
education0.00610.00080.13470.00000.0000
plot(results)

Example 3 - SSVS with multiple imputation (MI)

First, we will use the mice() function from the {mice} package to perform multiple imputation.

library(mice)
#> 
#> Attaching package: 'mice'
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> The following objects are masked from 'package:base':
#> 
#>     cbind, rbind

# Load the mtcars dataset
data <- mtcars

# Introduce random missingness in 10% of the data
set.seed(123)  
n <- nrow(data) * ncol(data)
missing_indices <- sample(n, size = 0.1 * n, replace = FALSE)

# Convert missing indices to row-column positions
rows <- (missing_indices - 1) %% nrow(data) + 1
cols <- (missing_indices - 1) %/% nrow(data) + 1

# Assign NA to the identified positions
for (i in seq_along(rows)) {
  data[rows[i], cols[i]] <- NA
}

# Perform multiple imputation using mice
imputed_data <- mice(data, m = 5, maxit = 50, seed = 123)

# Display the results of the imputation
summary(imputed_data)

# Extract and show the first completed dataset
imputed_mtcars <- complete(imputed_data, "long")
head(imputed_mtcars)

We will use this multiply imputed data set for SSVS, using the ssvs_mi() function.

outcome <- 'qsec'
predictors <- c('cyl', 'disp', 'hp', 'drat', 'wt', 'vs', 'am', 'gear', 'carb','mpg')
imputation <- '.imp'
results <- ssvs_mi(data = imputed_mtcars, y = outcome, x = predictors, imp = imputation)

The results of SSVS with MI can be summarized with the summary() and plot() functions. This will summarize across imputations for each predictor: the average MIP and the mean, minimum, maximum, and average nonzero beta coefficients.

Interactive version

You can launch an interactive (shiny) web application that lets you run SSVS analyses without programming. Simply install this package and run SSVS::launch() in an R console.

Metadata

Version

2.2.0

License

Unknown

Platforms (80)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    uefi
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-uefi
  • aarch64-windows
  • aarch64_be-none
  • arc-linux
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-linux
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • sh4-linux
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-uefi
  • x86_64-windows