bayes.t.test: Robust student's t-test based on Bayesian statistics.

Description Usage Arguments Details See Also Examples

View source: R/bayes.t.test.R

Description

Bayesian statistics analysis of metric variable on two groups.

Usage

1
bayes.t.test(vals, grp, stanDso, ...)

Arguments

vals

A vector of metric values assumed to be generated by student's t distribution.

grp

The groups of each elements in vals. Must be integers, either 1 or 2.

stanDso

An object of class stanmodel.

...

Parameters passed to sampling(...) in package rstan.

Details

Robust student's t-test based on Bayesian statistics provided by rstan package.

Please refer to: Stan-Ymet-Xnom2grp-MrobustHet.R and Stan-Ymet-Xnom2grp-MrobustHet-Example.R. Accompanies the book: Kruschke, J. K. (2014). Doing Bayesian Data Analysis: A Tutorial with R and JAGS, 2nd Edition. Academic Press / Elsevier.

Depreated, use best.robust_t_test as it gives more flexibility to control model parameters.

See Also

...

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
library(lxctk)

## Step 1: compile the model
library(rstan)

# Translate to C++ and compile to DSO. This will take a few mins. Select one of the model_code.
model_code = original.model()  #### loading model code: use the original model
model_code = modified.model()  #### loading model code: use modified model. The difference
## is: vals shared the same normality in t distirbution in the original;
## whereas vals was modeled by different normalities in t distirbution.
stanDso <- stan_model( model_code=model_code ) 
## save(stanDso, file='stanDso.RData')  ## For later use

## If you have already had a stanmodel object stanDso saved as a RData file, 
## e.g. stanDso.RData, just load in R with load("stanDso.RData").

## Step 2: running an example
##rstan_options(auto_write = TRUE)
options(mc.cores = 4)
data(TwoGroupIQ)
vals <- TwoGroupIQ$Score
grp <- as.integer(TwoGroupIQ$Group)
fit <- bayes.t.test(vals, grp, stanDso)

## user summary to extract info from fit
x <- summary(fit)
print(x$summary)

## Important notes:
## If bayes.t.test was run in a for-loop (e.g. >128), there will be ERROR:
## "all connections are in use Stan model  does not contain samples."
## Because "A maximum of 128 connections can be allocated..." (?close.connection)
##+for more info. In this case, we need to close connections by hand. For example:

## for (i in 1000)
## {
##	  fit <- bayes.t.test(...)
##	  showConnections(all=TRUE) or ## print(showConnections(all=TRUE))
## }

lixiangchun/lxctk documentation built on May 21, 2019, 6:44 a.m.