R: Normal Suite: One Sample, Two Sided, Known Variance
norm1KV.2sided
R Documentation
Normal Suite: One Sample, Two Sided, Known Variance
Description
Generates the suite of functions related to the one sample normal experiment
with a two-sided alternative hypothesis of interest when the variance is
known.
Usage
norm1KV.2sided(sigma, theta0, prob, mu, tau)
Arguments
sigma
Scalar. The known standard deviation of the population of interest.
theta0
Scalar. The critical value of the mean under the null hypothesis:
theta==theta0.
prob
Scalar. The prior probability of the null hypothesis. Must be a value
between 0 and 1.
mu
Scalar. The mean of the normal prior density on theta under the alternative
hypothesis. See documentation for dnorm.
tau
Scalar. The standard deviation for the normal prior density on theta under
the alternative hypothesis. See documentation for dnorm.
Details
norm1KV.2sided is used to generate a suite of functions for a
one-sample normal experiment with a two-sided alternative hypothesis when the
variance is known. That is, when
where Normal(mu,tau2) is Normal density with mean mu and variance
tau2 and u is the prior probability of the null hypothesis
(prob).
The functions that are generated are useful in examining the prior and
posterior densities of the parameter theta, as well as constructing the
Bayes Factor and determining the sample size via an average error based
approach.
The arguments of norm1KV.2sided are passed to each of the additional
functions upon their creation as default values. That is, if mu is
set to 1 in the call to norm1KV.2sided, each of the functions returned
will have the defaualt value of 1 for mu. If an argument is not
specified in the call to norm1KV.2sided, then it remains a required
parameter in all functions created.
Value
norm1KV.2sided returns a list of 5 functions:
logm
Returns a list of three vectors: the log marginal density under
the null hypothesis (logm0), the log marginal density under the
alternative hypothesis (logm1), the log marginal density
(logm). Each are evaluated at the observed data provided. The
function takes the following usage:
logm(xbar, n, sigma, theta0, prob, mu, tau)
xbar: Vector. Observed sample mean from the experiment.
n: Scalar. Sample Size.
Remaining parameters described above for norm1KV.2sided.
logbf
Returns a vector. The value of the log Bayes Factor given the observed
data provided and the prior parameters specified. The function has the
following usage:
logbf(xbar, n, sigma, theta0, prob, mu, tau)
See logm above for details on the parameters of the function.
prior
Returns a vector. The value of the prior density. The function has the
following usage:
prior(theta, theta0, prob, mu, tau)
theta: Vector. The quantiles at which to evaluate the prior.
Remaining parameters described above for norm1KV.2sided.
post
Returns a vector. The value of the posterior density. The function has
the following usage:
post(theta, xbar, n, sigma, theta0, prob, mu, tau)
theta: Vector. The quantiles at which to evaluate the prior.
xbar: Vector. Observed sample mean from the experiment.
n: Scalar. Sample Size.
Remaining parameters described above for norm1KV.2sided.
ssd.norm1KV.2sided
Sample size calculations for this particular set-up. The function has the
following usage:
ssd.norm1KV.2sided(alpha, w, sigma, theta0, prob, mu, tau,
minn = 2, maxn = 1000, all = FALSE)
See ssd for more details. The suite-specific parameters are
described above for norm1KV.2sided.
############################################################
# Generate the suite of functions for a one-sample normal
# with a two-sided test. Consider the hypothesis
# H0: theta==0 vs. H1: theta!=0
#
# with a normal prior for theta with prior mean 2 and
# prior standard deviation 1. The known standard
# deviation for the population is 5. The prior proability
# of the null hypothesis is set to 0.5.
# generate suite
f6 <- norm1KV.2sided(sigma=5,theta0=0,prob=0.5,mu=2,tau=1)
# attach suite
attach(f6)
# plot the prior and posterior density when the observed data is
# n = 30, xbar = 1.
mus <- seq(-5,5,0.01)
mu1 <- prior(mus)
mu2 <- post(mus,xbar=1,n=30)
plot(c(mu1,mu2)~rep(mus,2),type="n",ylab="Density",xlab="mu",main="")
lines(mu1~mus,lty=1,lwd=2)
lines(mu2~mus,lty=2,lwd=2)
# perform sample size calculation with TE bound of 0.25 and weight 0.5
# using both the example-specific function as well as the general.
ssd.norm1KV(alpha=0.25,w=0.5,logm=logm)
ssd.norm1KV.2sided(alpha=0.25,w=0.5)
# detain suite
detach(f6)
Results
R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(BAEssd)
Loading required package: mvtnorm
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/BAEssd/norm1KV.2sided.Rd_%03d_medium.png", width=480, height=480)
> ### Name: norm1KV.2sided
> ### Title: Normal Suite: One Sample, Two Sided, Known Variance
> ### Aliases: norm1KV.2sided
>
> ### ** Examples
>
> ############################################################
> # Generate the suite of functions for a one-sample normal
> # with a two-sided test. Consider the hypothesis
> # H0: theta==0 vs. H1: theta!=0
> #
> # with a normal prior for theta with prior mean 2 and
> # prior standard deviation 1. The known standard
> # deviation for the population is 5. The prior proability
> # of the null hypothesis is set to 0.5.
>
> # generate suite
> f6 <- norm1KV.2sided(sigma=5,theta0=0,prob=0.5,mu=2,tau=1)
Loading the 'norm1KV.2sided' suite...
This suite contains functions pertaining to one-sample experiment
involving a normally distributed response with known variance. The
hypothesis of interest has a two-sided alternative.
>
> # attach suite
> attach(f6)
>
> # plot the prior and posterior density when the observed data is
> # n = 30, xbar = 1.
> mus <- seq(-5,5,0.01)
> mu1 <- prior(mus)
> mu2 <- post(mus,xbar=1,n=30)
>
> plot(c(mu1,mu2)~rep(mus,2),type="n",ylab="Density",xlab="mu",main="")
> lines(mu1~mus,lty=1,lwd=2)
> lines(mu2~mus,lty=2,lwd=2)
>
> # perform sample size calculation with TE bound of 0.25 and weight 0.5
> # using both the example-specific function as well as the general.
> ssd.norm1KV(alpha=0.25,w=0.5,logm=logm)
Bayesian Average Error Sample Size Determination
Call: ssd.norm1KV(alpha = 0.25, w = 0.5, logm = logm)
Sample Size: 65
Total Average Error: 0.2482418
Acceptable sample size determined!
> ssd.norm1KV.2sided(alpha=0.25,w=0.5)
Bayesian Average Error Sample Size Determination
Call: ssd.norm1KV.2sided(alpha = 0.25, w = 0.5)
Sample Size: 65
Total Average Error: 0.2482418
Acceptable sample size determined!
>
> # detain suite
> detach(f6)
>
>
>
>
>
> dev.off()
null device
1
>