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

Description Usage Arguments Details See Also Examples

View source: R/best.model_functions.R

Description

http://www.indiana.edu/~kruschke/BEST/

Bayesian estimation for two groups provides complete distributions of credible values for the effect size, group means and their difference, standard deviations and their difference, and the normality of the data. The method handles outliers. The decision rule can accept the null value (unlike traditional t tests) when certainty in the estimate is high (unlike Bayesian model comparison using Bayes factors). The method also yields precise estimates of statistical power for various research goals

Usage

1
2
3
4
5
best.robust_t_test(y, grp, stanDso, meanY=mean(y), sdY=sd(y),
                   unifLo=sdY/1000, unifHi=sdY*1000, 
                   normalSigma=sdY*100,
                   expLambda=1/abs(meanY-1.0),
                   parameters = c('mu', 'sigma', 'nu', 'mu_diff', 'sigma_diff', 'nu_diff'), ...)

Arguments

y

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. 1 indicators this 1st group, 2 for the 2nd group.

stanDso

An object of class stanmodel.

unifLo

unifHi

A hyper-parameter for prior distribution for sigma.

unifHi

A hyper-parameter for prior distribution for sigma.

normalSigma

A hyper-parameter for prior distribution for mu.

expLambda

A hyper-parameter for prior distribution for nu. Must be > 0.

parameters

Parameters to be monitored.

...

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.

See Also

best.student_t,diag.mcmc,stanfit2mcmc.list,DbdaDensPlot2,HPDinterval

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
library(methods)
library(utils)
library(Rcpp)
library(lxctk)
library(rstan)

## Step 1: compile the model
# Translate to C++ and compile to DSO.
model_code = best.robust_t_test_model2() #### loading model code
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
options(mc.cores = parallel::detectCores())
data(TwoGroupIQ)
vals <- TwoGroupIQ$Score
grp <- as.integer(TwoGroupIQ$Group)
fit <- best.robust_t_test(vals, grp, stanDso)

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

## Important note 1:
## If best.robust_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 <- best.robust_t_test(...)
##	  showConnections(all=TRUE) or ## print(showConnections(all=TRUE))
## }

## Important note 2:
## If the following error occurs, my recipe is to set 'unifLo' to small value and 
##+'unifHi' to very large number.
#SAMPLING FOR MODEL 'ede2285102cdfeb5c5e92e95855f30cc' NOW (CHAIN 4).
# [1] "  Log probability evaluates to log(0), i.e. negative infinity."                                       
# [2] "  Stan can't start sampling from this initial value."                                                 
# [3] "Rejecting initial value:"                                                                             
# [4] "  Log probability evaluates to log(0), i.e. negative infinity."                                       
# [5] "  Stan can't start sampling from this initial value."                                                 
# [6] "Rejecting initial value:"                                                                             
# [7] "  Log probability evaluates to log(0), i.e. negative infinity."                                       
# [8] "  Stan can't start sampling from this initial value."                                                 
# [9] "Initialization between (-2, 2) failed after 100 attempts. "                                           
#[10] " Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model."
#error occurred during calling the sampler; sampling not done

fit <- best.robust_t_test(vals, grp, stanDso, unifLo=0, unifHi=.Machine$double.xmax)

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