Skip to content

Commit

Permalink
R package
Browse files Browse the repository at this point in the history
  • Loading branch information
lizhongc committed Nov 18, 2021
1 parent 0397e92 commit befbc99
Show file tree
Hide file tree
Showing 29 changed files with 475 additions and 5 deletions.
1 change: 1 addition & 0 deletions .Rproj.user/3992D3D4/sources/prop/4F1C9B07
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
5 changes: 5 additions & 0 deletions .Rproj.user/3992D3D4/sources/prop/AA8888B6
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"tempName": "Untitled1",
"cursorPosition": "3,7",
"scrollLine": "0"
}
6 changes: 6 additions & 0 deletions .Rproj.user/3992D3D4/sources/prop/D1765424
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"tempName": "Untitled1",
"cursorPosition": "75,17",
"scrollLine": "48",
"last_setup_crc32": "9F8C73CAbb338d19"
}
4 changes: 2 additions & 2 deletions .Rproj.user/3992D3D4/sources/prop/FCD88EE5
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"tempName": "Untitled1",
"cursorPosition": "23,27",
"scrollLine": "1"
"cursorPosition": "23,8",
"scrollLine": "0"
}
3 changes: 3 additions & 0 deletions .Rproj.user/3992D3D4/sources/prop/INDEX
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ H%3A%2FUbuntuRv2%2FIBGS%2FIBGS%2Fman%2Fac.ratio.Rd="EF1AA608"
H%3A%2FUbuntuRv2%2FIBGS%2FIBGS%2Fman%2FexBIC.Rd="BAAC6886"
H%3A%2FUbuntuRv2%2FIBGS%2Ftest.R="FCD88EE5"
H%3A%2FUbuntuRv2%2FIBGS%2Ftest.Rout="43B20C7B"
H%3A%2FUbuntuRv2%2FTCGS%2FIBGS.Rmd="D1765424"
H%3A%2FUbuntuRv2%2FTCGS%2FIBGS.tex="4F1C9B07"
H%3A%2FUbuntuRv2%2FTCGS%2Ftcg_ar.R="AA8888B6"
Empty file.
25 changes: 25 additions & 0 deletions .Rproj.user/3992D3D4/sources/s-9F8C73CA/254CACE3
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"id": "254CACE3",
"path": "H:/UbuntuRv2/IBGS/IBGS/R/blockgibbs.R",
"project_path": "R/blockgibbs.R",
"type": "r_source",
"hash": "0",
"contents": "",
"dirty": false,
"created": 1637272959110.0,
"source_on_save": false,
"relative_order": 6,
"properties": {
"tempName": "Untitled2",
"cursorPosition": "46,37",
"scrollLine": "25"
},
"folds": "",
"lastKnownWriteTime": 1637239897,
"encoding": "UTF-8",
"collab_server": "",
"source_window": "",
"last_content_update": 1637239897,
"read_only": false,
"read_only_alternatives": []
}
49 changes: 49 additions & 0 deletions .Rproj.user/3992D3D4/sources/s-9F8C73CA/254CACE3-contents
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#' The iterated block Gibbs sampler algorithm
#'
#' @param y the response variable
#' @param x the predictors
#' @param H the number of predictors in small groups, default is 30
#' @param kapp the number of selected predictors in first step, default is 20
#' @param tau the threshold to select the important predictors in second step, default is 0.9
#' @param len the half number of generated samples, default is 200
#' @param k the tuning parameter, default is 1
#' @param gamma the parameter for extended BIC, default is 0.5
#' @param info the selected model selection criterion from AIC, AICc, BIC and exBIC
#' @param family the type of model from linear, logistic, poisson
#' @param n.iter the number of iterations
#' @param n.models the number of top selected models
#'
#' @return a list contains a summary of final result
#' @export

BlockGibbsSampler <- function(y, x, n.iter = 3, n.models =10, H = 30, kapp = 20,
tau = 0.9, len = 200, k = 1, gamma = 0.5,
info = c("AIC", "BIC", "AICc", "exBIC"),
family = c("gaussian","poisson", "binomial")){
p <- dim(x)[2]
x.predictors <- colnames(x)
colnames(x) <- 1:p

x1 <- x
x2 <- vector()

if(n.iter < 2){
result <- BlockGibbsSampler.step3(y, x1, x2, n.models, x.predictors, H,
kapp, tau, len, k, gamma, p, info, family)
}
else{
j <- 1
while(j < n.iter){
v.select <- BlockGibbsSampler.step2(y, x1, x2, H, kapp, tau, len, k,
gamma, p, info, family)
x1 <- x[, -v.select]
x2 <- x[, v.select]

j <- j+1
}
result <- BlockGibbsSampler.step3(y, x1, x2, n.models, x.predictors, H,
kapp, tau, len, k, gamma, p, info, family)
}
result$x.predictors <- x.predictors
return(result)
}
26 changes: 26 additions & 0 deletions .Rproj.user/3992D3D4/sources/s-9F8C73CA/462FD7FF
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"id": "462FD7FF",
"path": "H:/UbuntuRv2/TCGS/IBGS.Rmd",
"project_path": null,
"type": "r_markdown",
"hash": "2363041877",
"contents": "",
"dirty": false,
"created": 1637269087029.0,
"source_on_save": false,
"relative_order": 5,
"properties": {
"tempName": "Untitled1",
"cursorPosition": "75,17",
"scrollLine": "48",
"last_setup_crc32": "9F8C73CAbb338d19"
},
"folds": "",
"lastKnownWriteTime": 1637272918,
"encoding": "UTF-8",
"collab_server": "",
"source_window": "",
"last_content_update": 1637272918280,
"read_only": false,
"read_only_alternatives": []
}
78 changes: 78 additions & 0 deletions .Rproj.user/3992D3D4/sources/s-9F8C73CA/462FD7FF-contents
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
---
title: "An introduction to IBGS"
author: "Lizhong Chen"
date: "11/19/2021"
output:
pdf_document: default
html_document:
df_print: paged
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Introduction

Here is a brief introduction to the R package "IBGS". The iterated block Gibbs sampler is a powerful variable selection method in high dimensions. It is important to identify the potential predictors which has significant influence on the response variable.

The "IBGS" package works for the generalized linear models including linear, logistic and poisson models and uses AIC, AICc, BIC, and exBIC as the model selection criterion.

There are two main goals for "IBGS" package:

* Identify the important predictors with the marginal probability

* Make a prediction based on the best selected model or model averaging using top selected models

## Install

To install and load the "IBGS" package, use this command
```{r, echo=FALSE}
cat("install.packages(\"IBGS_0.1.0.tar.gz\", repos = NULL)\nlibrary(IBGS)")
```

The "IBGS" package requires "doParallel" package. Please ensure that "doParallel" package is installed and loaded, then set up the register-cores.
```{r, echo=FALSE}
cat("library(doParallel)\nregisterDoParallel(ncores)\n")
```
where ncores is the number of CPU cores used in the calculation.

## Main function

The main function is
```{r, echo=FALSE}
cat("BlockGibbsSampler <- function(y, x, n.iter = 3, n.models =10, H = 30, kapp = 20,
tau = 0.9, len = 200, k = 1, gamma = 0.5,
info = c(\"AIC\", \"BIC\", \"AICc\", \"exBIC\"),
family = c(\"gaussian\",\"poisson\", \"binomial\"))")
```
For details, please use help document.
```{r, echo=FALSE}
cat("?BlockGibbsSampler")
```

## Example

Here is an example to show how "IBGS" works. We use the tropical cyclone genesis data.
```{r, echo=FALSE}
load("tcg_ar.RData")
```

It has 1856 samples and 149 predictors.
```{r, collapse=TRUE}
dim(x)
```
We divide the data into train and test set and use the train set ($\approx 80\%$ of data) to run "IBGS" algorithm. Then we use the test set ($\approx 20\%$ of data) to verify the results.

The simple following command is used
```{r}
cat("model <- BlockGibbsSampler(y_train, x_train, info = \"exBIC\", family = \"binomial\")")
```
We only need to specify the response, predictors, the model selection criterion and the model type.
### Variable ranking

We can plot the marginal probabilities for those important predictors using plots.Gibbs command.
```{r, collapse=TRUE}
plots.Gibbs(model)
```

Loading

0 comments on commit befbc99

Please sign in to comment.